Buy and Resell HDU - 6438 (有更新的贪心策略)

The Power Cube is used as a stash of Exotic Power. There are nn cities numbered 1,2,…,n1,2,…,n where allowed to trade it. The trading price of the Power Cube in the ii-th city is aiai dollars per cube. Noswal is a foxy businessman and wants to quietly make a fortune by buying and reselling Power Cubes. To avoid being discovered by the police, Noswal will go to the ii-th city and choose exactly one of the following three options on the ii-th day: 

1. spend aiai dollars to buy a Power Cube 
2. resell a Power Cube and get aiai dollars if he has at least one Power Cube 
3. do nothing 

Obviously, Noswal can own more than one Power Cubes at the same time. After going to the nn cities, he will go back home and stay away from the cops. He wants to know the maximum profit he can earn. In the meanwhile, to lower the risks, he wants to minimize the times of trading (include buy and sell) to get the maximum profit. Noswal is a foxy and successful businessman so you can assume that he has infinity money at the beginning. 

Input

There are multiple test cases. The first line of input contains a positive integer TT (T≤250T≤250), indicating the number of test cases. For each test case: 
The first line has an integer nn. (1≤n≤1051≤n≤105) 
The second line has nn integers a1,a2,…,ana1,a2,…,an where aiai means the trading price (buy or sell) of the Power Cube in the ii-th city. (1≤ai≤1091≤ai≤109) 
It is guaranteed that the sum of all nn is no more than 5×1055×105. 

Output

For each case, print one line with two integers —— the maximum profit and the minimum times of trading to get the maximum profit.

Sample Input

3
4
1 2 10 9
5
9 5 9 10 5
2
2 1

Sample Output

16 4
5 2
0 0

思路:这样的贪心还是第一次遇见。要想收入最高一定是在高价卖,低价买,如果我们在遇见一个高价就卖了,但是后面可能会有更高的价格,所以每当遇到一个高价,我就直接卖,等后面遇到更高的价格在更新。

怎么实现这样的更新策略? 思路其实是这样 :假设现在有1 2 9这三个价格,第一天买入1,第二天价格为2,所以我考虑卖了,赚了1块钱,现在我把1 pop掉(因为已经卖了),但是9明显更大,最优策略一定是1 买 9卖,赚8块钱。现在把2 push两次进去,一次标记f=0(表示没有进行过交易),一次f=1(表示已经进行过交易,这时候它代表的就是1,因为9-2=7,7+1=8,这和直接进行9-1的交易是一样的),但f=1是表示我进行的是反悔的操作,交易次数已经在之前进行过,所以不用记录。

这样贪心的原理应该是 加减法运算 中,减去一个数等于加上一个数的相反数(9-1==9-2+(2-1))

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;
int n;
struct Point
{
    int w,d;
    Point(int a,int b):w(a),d(b){}
    bool operator<(const Point &a) const
    {
        if(w==a.w) return d<a.d;//优先队列重载的方向应该是反 的,代表先弹出w最小和d最大(反悔)
        return w>a.w;  
    }
};
int main()
{
    #ifndef ONLINE_JUDGE
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        priority_queue<Point> que;
        ll ans=0,cnt=0;
        for(int i=0,x;i<n;i++)
        {
            scanf("%d",&x);
            if(que.empty()==0&&que.top().w<x)
            {
                Point tmp=que.top();
                ans-=tmp.w;
                ans+=x;
                if(tmp.d==0) cnt+=2;
                que.pop();
                que.push({x,1});
                que.push({x,0});
            }
            else
            {
                que.push({x,0});
            }
        }
        printf("%lld %lld\n",ans,cnt);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/82078710