多校练习赛2 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<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<queue>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define maxn 100005
#define MAX 500005
#define ms memset
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") ///在c++中是防止暴栈用的
/*
题目大意:给定一个序列,从1到n,可以买可以卖,
最多一样物品,问最多获利多少和最少交易次数多少。

维护一个单调区间即可,一旦递减就把前一个递增价值累计
*/
ll seq[maxn];
int t,n;
ll lb,ub,ans,mint;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
       ans=0,mint=0;
       scanf("%d",&n);
       for(int i=1;i<=n;i++) scanf("%lld",&seq[i]);
       seq[n+1]=-1,lb=seq[1],ub=lb;
       for(int i=2;i<=n+1;i++)
       {
           if(seq[i]>=seq[i-1]) ub=seq[i];
           else
           {
               if(ub-lb>0)
               {
                   ans+=(ub-lb);
                   mint+=2;
               }
               lb=ub=seq[i];
           }
        }
       printf("%lld %lld\n",ans,mint);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81149935