Okay... I thought about it for a long time... Although I know it is greedy...
Each time we look for the two types of cattle that have not been bought with the minimum price, compare a = the largest current price difference + the lowest price of the current coupon, and B = the lowest price of the current non-coupon
So... We want
First, maintain two small-rooted heaps, indicating the price of cattle bought with coupons and the price of cattle bought without coupons.
There is also a big root heap called recover, which indicates the price difference between the current several cows using coupons (the price difference is defined as the difference between the non-preferential price and the preferential price)
A and B can be bought if they are small...
1 /************************************************************** 2 Problem: 2590 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:140 ms 7 Memory:3332 kb 8 ****************************************************************/ 9 10 #include <cstdio>11 #include <algorithm>12 #include <queue>13 14 using namespace std;15 typedef long long ll;16 const int N = 50005;17 struct data{18 int w, v;19 data(void){}20 data(int x, int y) : w(x), v(y) {}21 };22 inline bool operator < (const data a, const data b){23 return a.v > b.v;24 }25 int cnt, n, k;26 int p[N], c[N];27 bool vis[N];28 ll m;29 priority_queue <data> h, H;30 priority_queue <ll, vector<ll>, greater<ll> > Re;31 32 inline int read(){33 int x = 0;34 char ch = getchar();35 while (ch < ‘0‘ || ch > ‘9‘)36 ch = getchar();37 while (ch >= ‘0‘ && ch <= ‘9‘){38 x = x * 10 + ch - ‘0‘;39 ch = getchar();40 }41 return x;42 }43 44 int main(){45 int i;46 ll cost;47 data T;48 n = read(), k = read();49 scanf("%lld", &m);50 for (i = 1; i <= n; ++i){51 p[i] = read(), c[i] = read();52 h.push(data(i, c[i]));53 H.push(data(i, p[i]));54 }55 while (!Re.empty()) Re.pop();56 for (i = 1; i <= k; ++i)57 Re.push(0);58 while (m > 0 && cnt < n){59 while (vis[h.top().w])60 h.pop();61 while (vis[H.top().w])62 H.pop();63 if (Re.top() + h.top().v < H.top().v){64 T = h.top(), cost = Re.top() + T.v;65 if (m < cost) break;66 m -= cost;67 Re.pop();68 Re.push(p[T.w] - c[T.w]);69 vis[T.w] = 1;70 }else{71 T = H.top(), cost = T.v;72 if (m < cost) break;73 m -= cost;74 vis[T.w] = 1;75 }76 ++cnt;77 }78 printf("%d\n", cnt);79 return 0;80 }
View code
Bzoj2590 [usaco2012 Feb] cow coupons