[USACO12FEB]牛券Cow Coupons

嘟嘟嘟

这其实是一道贪心题,而不是dp。

首先我们贪心的取有优惠券中价值最小的,并把这些东西都放在优先队列里,然后看[k + 1, n]中,有些东西使用了优惠券减的价钱是否比[1, k]中用了优惠券的物品更划算,是的话就更新。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cctype>
 8 #include<vector>
 9 #include<stack>
10 #include<queue>
11 using namespace std;
12 #define enter puts("")
13 #define space putchar(‘ ‘)
14 #define Mem(a, x) memset(a, x, sizeof(a))
15 #define rg register
16 typedef long long ll;
17 typedef double db;
18 const int INF = 0x3f3f3f3f;
19 const db eps = 1e-8;
20 const int maxn = 5e4 + 5;
21 inline ll read()
22 {
23   ll ans = 0;
24   char ch = getchar(), last = ‘ ‘;
25   while(!isdigit(ch)) {last = ch; ch = getchar();}
26   while(isdigit(ch)) {ans = ans * 10 + ch - ‘0‘; ch = getchar();}
27   if(last == ‘-‘) ans = -ans;
28   return ans;
29 }
30 inline void write(ll x)
31 {
32   if(x < 0) x = -x, putchar(‘-‘);
33   if(x >= 10) write(x / 10);
34   putchar(x % 10 + ‘0‘);
35 }
36
37 int n, k;
38 ll m;
39 struct Node
40 {
41   ll c, p;
42 }t[maxn];
43
44 bool cc(Node a, Node b) {return a.c < b.c;}
45 bool cp(Node a, Node b) {return a.p < b.p;}
46
47 priority_queue<ll, vector<ll>, greater<ll> > q;
48
49 int solve()
50 {
51   sort(t + 1, t + n + 1, cc);
52   ll sum = 0;
53   for(int i = 1; i <= k; ++i)
54     {
55       sum += t[i].c;
56       if(sum > m) return i - 1;
57       else if(i == n) return n;
58       q.push(t[i].p - t[i].c);
59     }
60   sort(t + k + 1, t + n + 1, cp);
61   int ans = k;
62   for(int i = k + 1; i <= n; ++i)
63     {
64       ll tp = q.empty() ? (ll)INF * (ll)INF : q.top();
65       if(t[i].p - t[i].c > tp)
66     {
67       sum += tp + t[i].c;
68       q.pop(); q.push(t[i].p - t[i].c);
69     }
70       else sum += t[i].p;
71       if(sum > m) return ans;
72       ans++;
73     }
74   return ans;
75 }
76
77 int main()
78 {
79   n = read(); k = read(); m = read();
80   for(int i = 1; i <= n; ++i) t[i].p = read(), t[i].c = read();
81   write(solve());
82   return 0;
83 }

原文地址:https://www.cnblogs.com/mrclr/p/9847742.html

时间: 2024-10-07 10:37:48

[USACO12FEB]牛券Cow Coupons的相关文章

洛谷P3045 [USACO12FEB]牛券Cow Coupons

P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget

[USACO12FEB]牛券Cow Coupons(堆,贪心)

[USACO12FEB]牛券Cow Coupons(堆,贪心) 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget of M units of money (1 <= M <= 10^14). Cow i costs P_i money (1 <= P_i <= 10^9), b

牛券Cow Coupons

USACO12FEB 久违的奶牛题. 题意: FJ准备买一些新奶牛,市场上有 $ N $ 头奶牛 $ (1 \leq N \leq 50000) $ ,第i头奶牛价格为 $ P_i (1 \leq P_i \leq 10^9) $ .FJ有K张优惠券,使用优惠券购买第i头奶牛时价格会降为 $ C_i(1\leq C_i \leq P_i) $ ,每头奶牛只能使用一次优惠券.FJ想知道花不超过 $ M(1 \leq M \leq 10^{14}) $ 的钱最多可以买多少奶牛? 解法: 在ZR时摸鱼

洛谷P3048 [USACO12FEB]牛的IDCow IDs

P3048 [USACO12FEB]牛的IDCow IDs 12通过 67提交 题目提供者lin_toto 标签USACO2012 难度普及/提高- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 谁能解释一下这个样例啊.... 题目描述 Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, he is a bit superstitiou

洛谷 P3014 [USACO11FEB]牛线Cow Line

P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Far

洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position on the track, and some cows jog at

P2419 [USACO08JAN]牛大赛Cow Contest

P2419 [USACO08JAN]牛大赛Cow Contest 海星 这题代码比较短 (哪题Floyd代码长的) 太真实了 直接上代码吧 这题就是一个经典的传递闭包问题 可以用拓扑排序啥的 不过还是Floyd简便一下 原文地址:https://www.cnblogs.com/qf-breeze/p/10473684.html

牛大赛Cow Contest

题目背景 [Usaco2008 Jan] 题目描述 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competito

P2952 [USACO09OPEN]牛线Cow Line

题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a line. The line begins with no cows and then, as time progresses, one by one, the cows join the line on the left or right side. Every once in a while, some number of cows on the left