C. Ordering Pizza

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 siai, 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

Copy
3 12
3 5 7
4 6 7
5 9 5
output

Copy
84
input

Copy
6 10
7 4 7
5 8 8
12 5 8
6 11 6
3 3 7
5 9 6
output

Copy
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.

思路:

先假设全部吃类型一的披萨,再假设有一部分吃下了类型二的披萨,会多出多少。

二者之和就是答案。

代码:

#include<iostream>
#include<algorithm>
#define fuck(x) cout<<"x="<<x<<endl;
using namespace std;
typedef long long ll;
const int maxn = 1e5+7;
struct node
{
ll x,s;
}t[maxn];; bool cmp(node a,node b)
{
if(a.x==b.x){return a.s>b.s;}
else return a.x>b.x;
} int main()
{
int n;
ll s;
scanf("%lld%lld",&n,&s);
int a,b;
ll ans=0;
ll sum=0;
for(int i=1;i<=n;i++){
scanf("%d%d%d",&t[i].s,&a,&b);
t[i].x=b-a;
ans+=t[i].s*a;sum+=t[i].s;
}
if(sum%s){
t[++n].s=s-sum%s;
t[n].x=0;
}
sort(t+1,t+1+n,cmp);
ll num=0,ans1=0;sum=0; for(int i=1;i<=n;i++){
if(num+t[i].s>=s){
ans1=max(ans1,(s-num)*t[i].x+sum);
ans1=max(ans1,((num+t[i].s)/s*s-num)*t[i].x+sum);
}
sum+=t[i].s*t[i].x;
num=(num+t[i].s)%s;
}
printf("%lld",ans+ans1);
}

  

Codeforce 867 C. Ordering Pizza (思维题)的更多相关文章

  1. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  2. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  3. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  4. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  5. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  6. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  8. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  9. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

随机推荐

  1. PLSQL过期:Your trial period for PL/SQL Developer is over .If you want to continue using this software ,you must purchase the retail version.

    PLSQL过期:Your trial period for PL/SQL Developer is over .If you want to continue using this software ...

  2. php2

    session   //将用户的会话数据存储在服务端,通过 session_start()开启session,通过$_SESSION读写session session_start(); //开启ses ...

  3. Vue学习目录

    前面的话 近年来,前端框架发展火热,新的框架和名词不停地出现在开发者眼前,而且开发模式也产生了一定的变化.目前来看,前端MVVM框架的出现给开发者带来了不小的便利,其中的代表就有Angular.js. ...

  4. Codeforces#543 div2 B. Mike and Children(暴力?)

    题目链接:http://codeforces.com/problemset/problem/1121/B 题意 给n个数 最多的对数 其中每一对(i,j)的ai+aj都相等(不知道怎么解释.... 判 ...

  5. FFmpeg 将YUV数据转RGB

    只要开始初始化一次,结束后释放就好,中间可以循环转码 AVFrame *m_pFrameRGB,*m_pFrameYUV; uint8_t *m_rgbBuffer,*m_yuvBuffer; str ...

  6. IntelliJ IDEA 导航的 20 大特性

    本文由 ImportNew - elviskang 翻译自 dzone.欢迎加入翻译小组.转载请见文末要求. 在前面的文章里,我介绍了IntelliJ IDEA(以下称IntelliJ)中与代码补全及 ...

  7. 炎黄流程中改流程节点颜色的js

  8. BZOJ3223文艺平衡树——非旋转treap

    此为平衡树系列第二道:文艺平衡树您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作: 翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...

  9. CodeForces 589F-Gourmet and Banquet-二分答案

    有m盘菜,每盘有一个开始时间和结束时间,必须每盘都吃同样的时间.问最多能吃多久. 二分答案,然后用一个优先队列维护当前时间内的菜,然后每次都吃结束时间最小的那盘. #include <cstdi ...

  10. Easy Finding POJ - 3740 (DLX)

    显然这是一道dfs简单题 或许匹配也能做 然而用了dancing links 显然这也是一道模板题 好的吧 调了一上午 终于弄好了模板 Easy Finding Time Limit: 1000MS ...