牛客网暑期ACM多校训练营(第二场) - D money (贪心)

https://www.nowcoder.com/acm/contest/140/D

题目描述 

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

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL a[1000000];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        int n;scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        LL ans=0;
        int step=0;
        int flag=0;
        LL now=0;
        for(int i=1;i<n;i++){
            if(a[i]<a[i+1]&&flag==0){
                now=a[i];
                flag=1;
                step++;
            }else if(a[i]>a[i+1]&&flag==1){
                ans+=a[i]-now;
                flag=0;
                step++;
            }
        }
        if(flag){
            ans+=a[n]-now;
            step++;
        }
        printf("%lld %d\n",ans,step);
    }
    return 0;
 }

猜你喜欢

转载自blog.csdn.net/mr_treeeee/article/details/81183103
今日推荐