18CCPC网赛A 贪心

Posted mj-liylho

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了18CCPC网赛A 贪心相关的知识,希望对你有一定的参考价值。

Buy and Resell

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


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 (T250), indicating the number of test cases. For each test case:
The first line has an integer n. (1n105)
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. (1ai109)
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
题意:给出 n ,表示 n 天。给出 n 个数,a[i] 表示第 i 天,物品的价格是多少。每天可以选择买一个物品,或者卖一个已有物品,也可以什么都不做,问最后最大能赚多少钱,最少操作次数是多少?
思路:要想收入最高一定是在高价卖,低价买,如果我们在遇见一个高价就卖了,但是后面可能会有更高的价格,所以每当遇到一个高价,我就直接卖,等后面遇到更高的价格在更新。
代码:
 1 #include"bits/stdc++.h"
 2 
 3 #define db double
 4 #define ll long long
 5 #define vl vector<ll>
 6 #define ci(x) scanf("%d",&x)
 7 #define cd(x) scanf("%lf",&x)
 8 #define cl(x) scanf("%lld",&x)
 9 #define pi(x) printf("%d
",x)
10 #define pd(x) printf("%f
",x)
11 #define pl(x) printf("%lld
",x)
12 #define rep(i, a, n) for (int i=a;i<n;i++)
13 #define per(i, a, n) for (int i=n-1;i>=a;i--)
14 #define fi first
15 #define se second
16 using namespace std;
17 typedef pair<int, int> pii;
18 const int N = 1e5 + 5;
19 const int mod = 1e9 + 5;
20 const int MOD = 992353;
21 const db PI = acos(-1.0);
22 const db eps = 1e-10;
23 const int inf = 0x3f3f3f3f;
24 const ll INF = 0x3fffffffffffffff;
25 int n;
26 struct P {
27     int x, id;
28     P(int a, int b) : x(a), id(b) {};
29     bool operator<(const P &a) const {
30         if (x == a.x) return id < a.id;//反向重载
31         return x > a.x;
32     }
33 };
34 
35 int main() {
36     int T;
37     ci(T);
38     while (T--) {
39         ci(n);
40         priority_queue<P> q;
41         ll ans = 0, cnt = 0;
42         for (int i = 0, x; i < n; i++) {
43             ci(x);
44             if (!q.empty() && q.top().x < x) {
45                 P tmp = q.top();q.pop();
46                 ans += x - tmp.x;
47                 if (!tmp.id) cnt++;
48                 q.push(P(x, 1));//以x的价格卖出过,以后可能还会以更高的价格卖
49             }
50             q.push(P(x, 0));//要以x的价格买入
51         }
52         printf("%lld %lld
", ans, 2 * cnt);
53     }
54     return 0;
55 }

 

 















以上是关于18CCPC网赛A 贪心的主要内容,如果未能解决你的问题,请参考以下文章

HDU_6709 CCPC网络赛H 优先队列 贪心

hdu - 6277,2018CCPC湖南全国邀请赛B题,找规律,贪心找最优.

[CCPC] 2017秦皇岛 NumbersI | Java BigInteger | 贪心

2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)

HDU 6438 Buy and Resell (CCPC网络赛) 堆 贪心

2016 CCPC 杭州站 小结