zoukankan      html  css  js  c++  java
  • 牛客模拟买卖东西

    链接:https://ac.nowcoder.com/acm/challenge/terminal

            money
    White Cloud has built n stores numbered from 1 to n.White Rabbit wants to visit these stores in the order from 1 to n.The store numbered i has a price a[i] representing that White Rabbit can spend a[i] dollars to buy a product or sell a product to get a[i] dollars when it is in the i-th store.The product is too heavy so that White Rabbit can only take one product at the same time.White Rabbit wants to know the maximum profit after visiting all stores.Also, White Rabbit wants to know the minimum number of transactions while geting the maximum profit.
    Notice that White Rabbit has infinite money initially.

    输入描述:

    The first line contains an integer T(0<T<=5), denoting the number of test cases.
    In each test case, there is one integer n(0<n<=100000) in the first line,denoting the number of stores.
    For the next line, There are n integers in range [0,2147483648), denoting a[1..n].

    输出描述:

    For each test case, print a single line containing 2 integers, denoting the maximum profit and the minimum number of transactions.
    示例1

    输入

    复制
    1
    5
    9 10 7 6 8

    输出

    复制
    3 4

    题目解析:可以来模拟买卖东西
    #pragma GCC optimize(2)
    #include<bits/stdc++.h>
    using namespace std;
    inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
    typedef unsigned long long ll;
    const int maxn = 1e6+10;
    ll a[maxn];
    int main()
    {
        ll t;
        cin>>t;
        while(t--){
            ll n;
            cin>>n;
            for(int i=1;i<=n;i++){
                cin>>a[i];
            }
            a[n+1]=0;
            ll i,p=0,cnt=0,f=0;//p来模拟钱数//cnt来模拟交换次数//f来模拟交换 
            for(i=1;i<=n;i++){
                if(a[i]<a[i+1]&&f==0){
                    p-=a[i];
                    cnt++;
                    f=1; 
                }
                else if(a[i]>a[i+1]&&f==1){
                    p+=a[i];
                    cnt++;
                    f=0;
                }
            }
            printf("%lld %lld
    ",p,cnt);
        }
        return 0;
    }
    View Code
    
    
    



  • 相关阅读:
    JDBC加载各种数据库方法和URL
    javascript通用表格验证程序
    javascript缩放图片
    ASP.NET 2.0 中改进的缓存功能
    滚轮改变图片大小
    在IE7关闭窗口,上传图片无法预览,window.status无效问题解决办法
    Sqlserver存储过程和C#分页类简化你的代码
    C#cookie使用
    ASP.NET2.0中控件的简单异步回调
    JavaScript表单验证大全
  • 原文地址:https://www.cnblogs.com/lipu123/p/12144621.html
Copyright © 2011-2022 走看看