C - Ordering Pizza CodeForces - 867C(贪心,好题!)

2024-04-16 02:58

本文主要是介绍C - Ordering Pizza CodeForces - 867C(贪心,好题!),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 exactly S slices.

It is known that the i-th contestant will eat si slices of pizza, and gain ai happiness for each slice of type 1 pizza they eat, and bi 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?

Input
The first line of input will contain integers N and S (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.

Output
Print the maximum total happiness that can be achieved.

Examples
Input
3 12
3 5 7
4 6 7
5 9 5
Output
84
Input
6 10
7 4 7
5 8 8
12 5 8
6 11 6
3 3 7
5 9 6
Output
314
Note
In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3·7 + 4·7 + 5·5 = 74.

题意: 2种匹萨,每个人吃不同种类的匹萨有不同的欢乐值。匹萨可以分成很多块。但只能买整数个匹萨,问买刚好够的匹萨,使得欢乐值总和最高。

思路: 假设匹萨可以分开买,那就没有关系了,每个人吃最高换了值得那一份匹萨就可以了。关键这里匹萨是正数的,类似01背包和部分背包的区别,这就牵扯到用不完的问题了。

  • 有两种解法,都是从不完整匹萨这个角度开始考虑的
  • 1.先考虑每个人吃的匹萨的整数部分,这部分直接买就可以了,可以吃自己最喜欢的种类。剩下的部分加起来,如果加起来的部分大于一整个匹萨,就可以买两个,使得剩余部分都可以分配到最高的欢乐值。不然的话只能选一个匹萨,总有一种人的匹萨要改变,算出改变的值即可。
  • 2.还有一种方法,每次只考虑单个匹萨,然后取这个匹萨能够产生的最大价值。两种方法本质上都是从整数上着手,去除确定答案的部分简化问题。

第一种方法

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>using namespace std;
typedef long long ll;
vector<pair<ll,ll> >s1;
vector<pair<ll,ll> >s2;int main()
{ll n,sum;scanf("%lld%lld",&n,&sum);ll ans = 0;ll sum1 = 0,sum2 = 0;for(ll i = 1;i <= n;i++){ll s,a,b;scanf("%lld%lld%lld",&s,&a,&b);if(a > b){ans += a * s;s1.push_back(make_pair(a - b,s));sum1 += s;}else{ans += b * s;s2.push_back(make_pair(b - a,s));sum2 += s;}}sum1 %= sum;sum2 %= sum;if(sum1 + sum2 > sum){printf("%lld\n",ans);}else{sort(s1.begin(),s1.end());sort(s2.begin(),s2.end());ll num1 = 0,num2 = 0;for(ll i = 0;i < s1.size();i++){num1 += min(s1[i].second,sum1) * s1[i].first;sum1 -= min(s1[i].second,sum1);}for(ll i = 0;i < s2.size();i++){num2 += min(s2[i].second,sum2) * s2[i].first;sum2 -= min(s2[i].second,sum2);}ans -= min(num1,num2);printf("%lld\n",ans);}return 0;
}

第二种方法

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>using namespace std;
typedef long long ll;
vector<pair<ll,ll> >S;int main()
{int n,sum;scanf("%d%d",&n,&sum);ll ans = 0;ll csum = 0;for(int i = 1;i <= n;i++){ll s,a,b;scanf("%lld%lld%lld",&s,&a,&b);ans += (ll)b * s;S.push_back(make_pair(a - b,s));csum += s;}if(csum % sum){S.push_back(make_pair(0,sum - csum % sum));}sort(S.begin(),S.end());ll ccnt = 0;csum = 0;for(int i = 0;i < S.size();i++){ll a = S[i].first, s = S[i].second;if(ccnt + s >= sum){//上一个整数匹萨的决策csum += a * (sum - ccnt);ans += max(csum,(ll)0);//当前整数部分匹萨的决策s -= (sum - ccnt);ans += (s / sum) * max(a * sum,(ll)0);//多余f匹萨留给下一步的决策ccnt = s % sum;csum = ccnt * a;}else{//非整数时候的决策,为凑成一个整数匹萨做准备。ccnt += s;csum += s * a;}}printf("%lld\n",ans);return 0;
}

这篇关于C - Ordering Pizza CodeForces - 867C(贪心,好题!)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907634

相关文章

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

usaco 1.3 Barn Repair(贪心)

思路:用上M块木板时有 M-1 个间隙。目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的M-1个作为间隙,其余地方用木板盖住。 做法: 1.若,板(M) 的数目大于或等于 牛棚中有牛的数目(C),则 目测 给每个牛牛发一个板就为最小的需求~ 2.否则,先对 牛牛们的门牌号排序,然后 用一个数组 blank[ ] 记录两门牌号之间的距离,然后 用数组 an

poj 3190 优先队列+贪心

题意: 有n头牛,分别给他们挤奶的时间。 然后每头牛挤奶的时候都要在一个stall里面,并且每个stall每次只能占用一头牛。 问最少需要多少个stall,并输出每头牛所在的stall。 e.g 样例: INPUT: 51 102 43 65 84 7 OUTPUT: 412324 HINT: Explanation of the s

poj 2976 分数规划二分贪心(部分对总体的贡献度) poj 3111

poj 2976: 题意: 在n场考试中,每场考试共有b题,答对的题目有a题。 允许去掉k场考试,求能达到的最高正确率是多少。 解析: 假设已知准确率为x,则每场考试对于准确率的贡献值为: a - b * x,将贡献值大的排序排在前面舍弃掉后k个。 然后二分x就行了。 代码: #include <iostream>#include <cstdio>#incl

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

POJ2010 贪心优先队列

c头牛,需要选n头(奇数);学校总共有f的资金, 每头牛分数score和学费cost,问合法招生方案中,中间分数(即排名第(n+1)/2)最高的是多少。 n头牛按照先score后cost从小到大排序; 枚举中间score的牛,  预处理左边与右边的最小花费和。 预处理直接优先队列贪心 public class Main {public static voi