Ordering Pizza(贪心)
来源:互联网 发布:import java 编辑:程序博客网 时间:2024/06/04 22:37
It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactlyS slices.
It is known that the i-th contestant will eatsi slices of pizza, and gainai happiness for each slice of type 1 pizza they eat, andbi happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved?
The first line of input will contain integers N andS (1 ≤ N ≤ 105, 1 ≤ S ≤ 105), the number of contestants and the number of slices per pizza, respectively.N lines follow.
The i-th such line contains integers si, ai, and bi (1 ≤ si ≤ 105, 1 ≤ ai ≤ 105, 1 ≤ bi ≤ 105), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively.
Print the maximum total happiness that can be achieved.
3 123 5 74 6 75 9 5
84
6 107 4 75 8 812 5 86 11 63 3 75 9 6
314
In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be3·7 + 4·7 + 5·5 = 74.
题目大意:
懒得说了;
题目思路:
我们很容易想到如果某一个参赛者a>b就选第一个,否则就选第二个,但是这样连样例都过不去,可能很多人就会放弃这个想法,我也是,其实就是题写的不够多,这题就是顺着这个思路想下去,我们可以发现对于该选第一个的部分,能刚好整除s的部分那肯定是选第一个的,该选第二个的同理,该考虑的是余下的部分,如果余下的部分大于s,的话自然还需要两个,这样刚好可以分给两种选择一人一个,如果小于等于s,那证明只能再买一个,那说明两种选择中必须更改一种选择,这样的话,我们就去按照替换后价值损失最小排序,两种选择中选出一种最小的就行
ac代码:
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<sstream>#include<map>#include<vector>#define LL long long#define INF 0x3f3f3f3f#define LLINF 0x3f3f3f3f3f3f3fusing namespace std;vector<pair<LL,LL> >V1,V2;int n,s;int main(){ while(~scanf("%d%d",&n,&s)) { V1.clear(); V2.clear(); LL sum1 = 0; LL sum2 = 0; LL ans = 0; for(int i = 0;i<n;i++){ LL a,b,c; scanf("%lld%lld%lld",&c,&a,&b); if(a>b){ sum1 += c; ans += c*a; V1.push_back(make_pair(a-b,c)); } else{ sum2 += c; ans += c*b; V2.push_back(make_pair(b-a,c)); } } sum1 %= s; sum2 %= s; if(sum1+sum2>s){ cout<<ans<<endl; continue; } else{ sort(V1.begin(),V1.end()); sort(V2.begin(),V2.end()); LL tmp1 = 0; LL tmp2 = 0; int len = V1.size(); for(int i = 0;i<len;i++){ tmp1 += min(sum1,V1[i].second)*V1[i].first; sum1 -= min(sum1,V1[i].second); } len = V2.size(); for(int i = 0;i<len;i++){ tmp2 += min(sum2,V2[i].second)*V2[i].first; sum2 -= min(sum2,V2[i].second); } cout<<ans-min(tmp1,tmp2)<<endl; } }}
- Ordering Pizza(贪心)
- Codeforces Round #437-贪心-C. Ordering Pizza
- Codeforces Round #437 (Div. 2 C. Ordering Pizza 贪心 only two types of pizza
- codeforces 865B. Ordering Pizza
- CodeForces 867C Ordering Pizza
- codeforces 867 C Ordering Pizza
- Codeforces Round #437 Div. 2 C. Ordering Pizza
- PIZZA
- UVA10079 - Pizza Cutting(数论)
- Ordering Tasks(拓扑排序)
- uva 565 Pizza Anyone?(二进制+搜索)
- uva 10305 Ordering Tasks(拓扑排序)
- [UVA 10305] Ordering Tasks (拓扑排序)
- 10305 - Ordering Tasks(拓扑序列)
- (拓扑排序) uva Ordering Tasks
- UvaLive 6435 Network Packet Ordering(dp)
- UVA - 10305 Ordering Tasks (拓扑排序)
- UVa 10305 - Ordering Tasks(队列实现)
- HDU
- 手把手教你如何玩转SSM(Spring+SpringMVC+Mybatis)
- win10下安装Ubuntu后,启动时没有win10选项解决方法
- fatal error C1051: program database file
- 使用Markdown实现页内跳转
- Ordering Pizza(贪心)
- 【机房收费系统】——登录窗体
- 第12期 《多方涉猎,博采众长》10月刊
- 环境变量与文件查找【linux】
- 判断相同的子树--uva12219 Common Subexpression Elimination
- net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM.
- win7基础 cmd 查看字符编码格式
- 遇到枪击事件,你最该做的事儿
- HDU 5649 线段树+二分