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

题意:

在一个地方购买另一个地方卖,从1到N,不能返回,问在赚钱最多的基础上,最少交易几次

分析:

贪心,从前往后,下降子序列的最低点购买,在上升子序列的最高点卖

代码:

#include<bits/stdc++.h>
using namespace std;
long long  a[100015],sum[100015];
long long dp[100015][2];
#define mod 1000000007
int main()
{
    long long int t,i,j,n,sum,maxn,minx,b,now,now2,num;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&n);
        bool flag=0;
        now=2147483649;
        now2=0;
        num=0;
        sum=0;
        int x;
        for(i=0;i<n;i++)
        {
            scanf("%d",&x);
            if(!flag)
            {
                if(x<=now)now=x;
                else
                {
                    flag=1;
                    now2=x;
                }
            }
            else
            {
                if(x>=now2)
                {
                    now2=x;
                }
                else
                {
                    flag=0;
                    if(now2>now){
                    sum+=now2-now;
                    now=x;
                    num++;
                    }
                    else
                    {
                        now2=0;
                    }
 
                }
            }
        }
        if(flag&&now2>now)
        {
              sum+=now2-now;
              num++;
        }
        printf("%lld %lld\n",sum,num*2);
    }
}

猜你喜欢

转载自blog.csdn.net/lml11111/article/details/81149230