HDU6438(贪心技巧)

第一眼喜闻乐见的股票问题dp可以暴力,然鹅时间不允许。

于是考虑怎么贪。

这篇题解说得很生动了。

因为每支股票都有买入的潜力所以肯定都加在优先队列里。

然后考虑的是哪些需要加入两次。这是我第二次见到类似的手法。当它比优先队列队首要大,可以卖,但是因为后面的还没读所以不知道是不是最优。那就先卖着,钱拿了,然后标记上它可能是个轴承,等到它成了队首(显然这时更便宜的股票已经都跟“大客户”配对完走掉了,或者至少跟轴承嫁接了)且读到了更好的卖家,那它的轴承作用成功完成任务,当前这个更好的卖家作为接盘侠被标记为轴承。

注意标记不是bool的,因为同一个数字可能出现很多次,用int计数。

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <cctype>
 8 #include <climits>
 9 #include <iostream>
10 #include <iomanip>
11 #include <algorithm>
12 #include <string>
13 #include <sstream>
14 #include <stack>
15 #include <queue>
16 #include <set>
17 #include <map>
18 #include <vector>
19 #include <list>
20 #include <fstream>
21 #include <bitset>
22 #define init(a, b) memset(a, b, sizeof(a))
23 #define rep(i, a, b) for (int i = a; i <= b; i++)
24 #define irep(i, a, b) for (int i = a; i >= b; i--)
25 using namespace std;
26 
27 typedef double db;
28 typedef long long ll;
29 typedef unsigned long long ull;
30 typedef pair<int, int> P;
31 const int inf = 0x3f3f3f3f;
32 const ll INF = 1e18;
33 
34 template <typename T> void read(T &x) {
35     x = 0;
36     int s = 1, c = getchar();
37     for (; !isdigit(c); c = getchar())
38         if (c == '-')    s = -1;
39     for (; isdigit(c); c = getchar())
40         x = x * 10 + c - 48;
41     x *= s;
42 }
43 
44 template <typename T> void write(T x) {
45     if (x < 0)    x = -x, putchar('-');
46     if (x > 9)    write(x / 10);
47     putchar(x % 10 + '0');
48 }
49 
50 template <typename T> void writeln(T x) {
51     write(x);
52     puts("");
53 }
54 
55 const int maxn = 1e5 + 5;
56 int T, n, day;
57 ll ans;
58 map<int, int> mark;
59 
60 int main() {
61     for (read(T); T; T--) {
62         ans = day = 0;
63         mark.clear();
64         priority_queue<int, vector<int>, greater<int> > Q;
65 
66         read(n);
67         rep(i, 1, n) {
68             int x;
69             read(x);
70             if (!Q.empty() && Q.top() < x) {
71                 int cur = Q.top(); Q.pop();
72                 ans += x - cur;
73                 Q.push(x);
74                 mark[x]++;
75                 if (!mark[cur]) day++;
76                 else    mark[cur]--;
77             }
78             Q.push(x);
79         }
80         printf("%lld %d
", ans, day * 2);
81     }
82     return 0;
83 }
原文地址:https://www.cnblogs.com/AlphaWA/p/10596833.html