CodeForces 867C Ordering Pizza

来源:互联网 发布:软件模块接口参数 编辑:程序博客网 时间:2024/06/05 08:23

题目链接:http://codeforces.com/contest/867/problem/C
题意:有2种披萨,每次预定一份披萨,能把披萨分成S分,有n个人,第i个人要吃si块,吃第一种,每块披萨能增长ai的幸福值,吃第二种披萨,每块能增长bi的幸福值,现在问你能怎样预定披萨,能使得预定的披萨尽可能少,且还能满足所有人的胃口,还使得幸福值尽可能大
解析:首先贪心的来存答案,就是每个人都吃幸福值最高的披萨,这样预定的披萨可能不一定是最少的,但是可以通过一些转换,将一些人转为吃部分的另一种披萨,所以记录一下每种披萨,按贪心策略来说,会多买多少片披萨(c1,c2),如果c1+c2>S表示你就算不转换,也一定要多买一份披萨,如果小于,那么你就需要转换了,所以你可以把每个人两种披萨开心值的差值存下来,然后排个序,最后用之前记录的结果减去

#include <bits/stdc++.h>using namespace std;typedef long long ll;vector<pair<ll,ll> >t1;vector<pair<ll,ll> >t2;int main(void){    int n,s;    scanf("%d %d",&n,&s);    ll c1 = 0,c2 = 0,ans = 0;    for(int i=0;i<n;i++)    {        ll c,a,b;        scanf("%I64d %I64d %I64d",&c,&a,&b);        if(a>b)        {            ans += c*a;            c1 = (c1+c)%s;            t1.push_back(make_pair(a-b,c));        }        else        {            ans += c*b;            c2 = (c2+c)%s;            t2.push_back(make_pair(b-a,c));        }    }    if(c1+c2>s)    {        printf("%I64d\n",ans);        return 0;    }    sort(t1.begin(),t1.end());    sort(t2.begin(),t2.end());    ll cc1 = 0,cc2 = 0;    for(int i=0;i<(int)t1.size();i++)    {        //cout<<t1[i].first<<" "<<t1[i].second<<endl;        if(c1>t1[i].second)        {            cc1 += t1[i].second*t1[i].first;            c1 -= t1[i].second;        }        else        {            cc1 += c1*t1[i].first;            break;        }    }    for(int i=0;i<(int)t2.size();i++)    {        if(c2>t2[i].second)        {            cc2 += t2[i].second*t2[i].first;            c2 -= t2[i].second;        }        else        {            cc2 += c2*t2[i].first;            break;        }    }    ans -= min(cc1,cc2);    printf("%I64d\n",ans);    return 0;}
原创粉丝点击