Codeforces Round #437 (Div. 2) - C. Ordering Pizza(贪心)

探讨在限定条件下如何通过购买最少数量的两种披萨来达到最大化的满足度。使用贪心算法进行最优分配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


题意:

有两种披萨,给你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 long
const 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);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值