牛客模拟买卖东西

链接: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\n",p,cnt);
    }
    return 0;
}
View Code

 




posted @ 2020-01-03 14:03  哎呦哎(iui)  阅读(198)  评论(0编辑  收藏  举报