1. 程式人生 > >18CCPC網賽A 貪心

18CCPC網賽A 貪心

我們 收入 min def bsp ini and begin imu

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 2018中國大學生程序設計競賽 - 網絡選拔賽 題意:給出 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\n",x)
10 #define pd(x) printf("%f\n",x)
11 #define pl(x) printf("%lld\n",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\n", ans, 2 * cnt);
53     }
54     return 0;
55 }

18CCPC網賽A 貪心