Buy and Resell(贪心好题!)

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/xianpingping/article/details/82079847

这个贪心可以说是很巧妙了。

Buy and Resell

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1024    Accepted Submission(s): 318

Problem Description

The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed to trade it. The trading price of the Power Cube in the i-th city is ai 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 i-th city and choose exactly one of the following three options on the i-th day:

1. spend ai dollars to buy a Power Cube
2. resell a Power Cube and get ai 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 n 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 T (T≤250), indicating the number of test cases. For each test case:
The first line has an integer n. (1≤n≤105)
The second line has n integers a1,a2,…,an where ai means the trading price (buy or sell) of the Power Cube in the i-th city. (1≤ai≤109)
It is guaranteed that the sum of all n is no more than 5×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

Hint

In the first case, he will buy in 1, 2 and resell in 3, 4. profit = - 1 - 2 + 10 + 9 = 16 In the second case, he will buy in 2 and resell in 4. profit = - 5 + 10 = 5 In the third case, he will do nothing and earn nothing. profit = 0

Source

2018中国大学生程序设计竞赛 - 网络选拔赛

Recommend

chendu   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443

Statistic | Submit | Discuss | Note

这道题的难点就在于怎么搞这个买和卖的关系。

假设每一个点我都买了。当后来的数字大于前边的我就要买,这个买是真的买,就是这个物品被买了之后就不在我选择要买商品的管辖范围之内了。而被卖的,只是假装前边以这个价值卖掉,但是可能这个点也有可能买,所以这种的还要把这个价值加到queue里,做待定,如果下次被买了,那么就真的被买了。

其实我上面说的话无非可以根据样例进行理解:

1 2 10 9

把1放进优先队列里。然后把2放进去。发现2>1,这个时候就可以考虑买1,1就是被真的买了(所谓真的买就是移出优先队列,从此就没有这个选项了,但是它所贡献的价值不一定是当前被卖获得的价值),ans+=(2-1),2就是没有被买,但是2可能被之后的买,所以2放进队列。(这时候有人发现2实际上放进了队列两次)。。。为什么呢?是因为2这个位置是被假卖的,可能实际上2这个位置买会比较好一点。现在为止,队列里的数字是(2,2)然后继续,10入队列。显然10>2,这个时候把ans+=(10-2).就是说实际上2这个位置是没有操作的。当时1在2的位置被“假卖”了,而2这个位置是应该真买的,又在10这个位置被卖了。所以这么一转化就是1这个东西在10这个位置被卖了(由加法拆分可知ans是对的。只是操作次数要减掉1对)。pop()后此时队列里还剩(2,10)。再加入9,发现9>2,此时ans+=(9-2)。嗯,现在知道了为什么在假卖的地方要再加一遍进队列了吧。

代码:

#include<bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> >qq;
///priority_queue<int, vector<int>, greater<int> > pq;
typedef long long LL;
int main()
{
   /// int vis[100010];
    int n,x;
    LL cnt=0;
    LL ans=0;
    int t;
    scanf("%d",&t);
    while(t--){
              map<int, int> vis;
            cnt=0;ans=0;
    scanf("%d",&n);
   /// memset(vis,0,sizeof(vis));
    while(!qq.empty()){
        qq.pop();
    }
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            qq.push(x);///假设该点是买了的。

            if(qq.top()<x){
                ans+=x-qq.top();
                cnt++;
                if(vis[qq.top()]>0)///之前那个位置已经卖过了
                {
                    vis[qq.top()]--;
                    cnt--;
                }
                vis[x]++;///标志一下该点已经卖了。
                qq.pop();///该点已经买了刚刚被卖了。
                qq.push(x);///在这个点卖,但也有可能在后边的点里面买。
            }
        }
        printf("%lld %lld\n",ans,cnt*2);

    }
    return 0;
}

猜你喜欢

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