Codeforce 867 C. Ordering Pizza (思维题)

              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);
}

  

原文地址:https://www.cnblogs.com/ZGQblogs/p/9483848.html