Codeforces Round #437 (Div. 2)

来源:互联网 发布:知轩藏书网 编辑:程序博客网 时间:2024/06/04 23:20

C. Ordering Pizza
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 bihappiness 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
3 123 5 74 6 75 9 5
output
84
input
6 107 4 75 8 812 5 86 11 63 3 75 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.


题意:

有两种披萨,给你n个人和他们要吃多少片披萨,和他们分别吃到两种披萨的满足度,在给你一个披萨有s片,让你求:

前提是买最少的披萨,可以获得的最大满足度。

POINT:

贪心,每个人总会比较喜欢吃某一个披萨,记录下来,记录a披萨有几个人喜欢吃的总片数ca,b披萨也一样cb。

ans+=num*max(aa,bb);

那么可以贪心的给他们吃披萨,就会剩下来ca%s个人喜欢吃a披萨,cb%s个人喜欢吃b披萨。

如果这剩下来的总和大于s,说明一块披萨满足不了,那么给他们各自喜欢的,答案就是ans。

如果小于等于s,说明必须有一边的人要不吃他喜欢的披萨,那么就讨论一下,如果ca%s个人吃b披萨会减少多少满足度,

如果cb%s个人吃a披萨会减少多少满足度,取一个较小值,ans-=他  就行了。


所以我们要记录的是,喜欢吃a的差值和对应的片数,b也一样。差值从小到大排序。


#include <iostream>#include <string.h>#include <stdio.h>#include <vector>#include <algorithm>using namespace std;#define LL long longconst LL maxn = 100100;struct node{    LL num,cha;    node(LL n,LL c):    num(n),cha(c){}};vector<node>a,b;bool cmd(node aa,node bb){    return aa.cha<bb.cha;}int main(){    LL n,s;    scanf("%lld %lld",&n,&s);    LL numa=0,numb=0;    LL ans=0;    for(LL i=1;i<=n;i++){        LL num,aa,bb;        scanf("%lld %lld %lld",&num,&aa,&bb);        if(aa>=bb){            a.push_back(node(num,aa-bb));            numa+=num;            ans+=aa*num;        }else{            b.push_back(node(num,bb-aa));            numb+=num;            ans+=bb*num;        }    }    numa%=s;    numb%=s;    if(numa+numb>s){        printf("%lld\n",ans);        return 0;    }    LL ansa=0;    sort(a.begin(),a.end(),cmd);    for(LL i=0;i<a.size();i++){        if(numa==0) break;        else if(numa>=a[i].num){            ansa+=a[i].num*a[i].cha;            numa-=a[i].num;        }        else{            ansa+=numa*a[i].cha;            numa=0;        }    }    LL ansb=0;    sort(b.begin(),b.end(),cmd);    for(LL i=0;i<b.size();i++){        if(numb==0) break;        else if(numb>=b[i].num){            ansb+=b[i].num*b[i].cha;            numb-=b[i].num;        }        else{            ansb+=numb*b[i].cha;            numb=0;        }    }    ans-=min(ansb,ansa);    printf("%lld\n",ans);}



原创粉丝点击