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;
typedef long long ll;
const int maxn=100000+10;
ll a[maxn];
int main()
{
    ll t;
    ll n;
    ll ans;
    ll lala;
    ll i;
    ll biaozhi;
    scanf("%lld",&t);
    while(t--)
    {
        ans=1;
        scanf("%lld",&n);
        int kaka=0;
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        if(n==1)
        {
            cout<<0<<" "<<0<<endl;
            continue;
        }
        a[0]=-1;
        a[n+1]=-1;
        if(a[1]<a[2])
        {
            lala=a[1];
            biaozhi=2;
        }
        else
        {
            for(i=2;i<=n-1;i++)
            {
                if( a[i]<=a[i-1] && a[i]<a[i+1])
                {
                    lala=a[i];
                    biaozhi=i+1;
                    break;
                }
            }
            if(i>=n)
            {
                cout<<0<<" "<<0<<endl;continue;
            }
        }


        int xpp=0;///作为找到第一个谷峰的标记

        ll profit=-lala;
        ll logal=1;///找最大
        ll last=-1;
        for(i=biaozhi;i<=n;i++)
        {
            if(logal==1)///这个地方要找最大
            {
                if(a[i]>=a[i-1] && a[i]>a[i+1]  )
                {
                    if(xpp==0)
                    {
                        xpp=1;
                    }
                    else
                    {
                        ///money+=a[i];
                    }
           ///         cout<<"max"<<a[i]<<endl;
                    ans++;
                    profit+=a[i];
                    logal=0;
                    last=a[i];

                }
            }
            else///这个地方要找最小
            {
                if(a[i]<=a[i-1] &&a[i]<a[i+1])
                {
      ///              cout<<"min"<<a[i]<<endl;
                    ans++;
                    profit-=a[i];
                    logal=1;
                    last=a[i];

                }
            }
        }
        if(xpp==0)///一个谷峰也没有
        {
            cout<<0<<" "<<0<<endl;
            continue;
        }

        if(logal==1)///最后一个是 买
        {
            profit+=last;
            ans--;
        }
        if(profit<0 &&kaka==0)
            profit=0,ans=0;
        cout<<profit<<" "<<ans<<endl;
    }
}
/*
2
5
9 10 7 6 8

*/

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/81149566