2018牛客多校训练---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

输出描述:

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

 n个商店,从1到n访问,每个商店对应的a[i]表示在这个商店买卖商品都是这个价格,假设这个人一开始身上有无限多的钱,每次手中只可以拿一件商品。问在最大利润的前提下最小交易次数。

思路:

贪心,之前做过类似的问题,模拟买卖过程,找递增区间。

#include
using namespace std;

typedef long long ll;

const int N=1e5+100;

ll a[N];

int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        a[n+1]=0;
        ll p=0,f=0,cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]a[i+1]&&f==1)
            {
                p+=a[i];
                f=0;
                cnt++;
            }
        }
        cout<

 

你可能感兴趣的:(思维or贪心)