Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) C. Ordering Pizza
题意:有2种pizzaA和B,有n个人,每块pizz分为S片,给出每个人需要吃s片,吃一片A获得x的幸福,吃一片B获得y的幸福,问在购买最少的Pizza的前提下获得的最大幸福值
思路:注意每个人可同时吃A和B,当x>=y时,我们选择A,否则选B,看这个时候买的pizza,和所有片数买的pizza,否则的话,我们分开,对于多余的A,我们买B,用x-y最小的去换,B也同理
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=1e5+100; 5 6 struct node{ 7 ll s,x; 8 node(ll xx,ll yy){ 9 s=xx;x=yy; 10 } 11 }; 12 ll n,S; 13 vector<node> a,b; 14 ll sum1=0,sum2=0,sum=0; 15 bool cmp(node p,node q){ 16 return p.x<q.x; 17 } 18 19 bool check(){ 20 ll x1=sum1/S,x2=sum2/S,x3=(sum1+sum2)/S; 21 if(sum1%S) x1++; 22 if(sum2%S) x2++; 23 if((sum1+sum2)%S) x3++; 24 if(x1+x2==x3) return true ; 25 else return false; 26 } 27 28 int main(){ 29 cin>>n>>S; 30 ll x,y,z; 31 for(int i=1;i<=n;i++){ 32 scanf("%lld%lld%lld",&x,&y,&z); 33 if(y>=z){ 34 a.push_back({x,y-z}); 35 sum1+=x*1LL; 36 sum+=(x*y)*1LL; 37 } 38 else { 39 b.push_back({x,z-y}); 40 sum2+=x*1LL; 41 sum+=(1LL)*x*z; 42 } 43 } 44 if(check()){ 45 cout<<sum<<endl;return 0; 46 } 47 sort(a.begin(),a.end(),cmp); 48 sort(b.begin(),b.end(),cmp); 49 // cout<<sum<<" "<<sum1<<" "<<sum2<<endl; 50 ll ans1=sum,ans2=sum; 51 ll xx=sum1%S; 52 for(int i=0;i<a.size();i++){ 53 ll Min=min(xx,a[i].s); 54 ans1-=Min*a[i].x; 55 xx-=Min; 56 if(xx==0) break; 57 } 58 ll yy=sum2%S; 59 for(int i=0;i<b.size();i++){ 60 ll Min=min(yy,b[i].s); 61 ans2-=Min*b[i].x; 62 yy-=Min; 63 if(yy==0) break; 64 } 65 cout<<max(ans1,ans2)<<endl; 66 }