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?
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 si, ai, 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.
Print the maximum total happiness that can be achieved.
3 12 3 5 7 4 6 7 5 9 5
84
6 10 7 4 7 5 8 8 12 5 8 6 11 6 3 3 7 5 9 6
314
参考自:http://blog.csdn.net/egqawkq/article/details/78150444
#include<bits/stdc++.h>
using namespace std;
vector< pair<__int64,__int64> >a,b; //first存放两个产生幸福值的差,second存放吃的数量
__int64 n,s,ans,na,nb,ta,tb;
signed main()
{
__int64 ss,aa,bb;
pair<__int64,__int64>k;
scanf("%I64d%I64d",&n,&s);
for(int i=1;i<=n;i++)
{
scanf("%I64d%I64d%I64d",&ss,&aa,&bb);
//吃哪个产生幸福值大就放到哪个数组中 用ans存放最终幸福值,na和nb分别存放所需披萨片数
if(aa>bb)
{
ans+=ss*aa;
na+=ss;
k.first=aa-bb;
k.second=ss;
a.push_back(k);
}
else
{
ans+=ss*bb;
nb+=ss;
k.first=bb-aa;
k.second=ss;
b.push_back(k);
}
}
// cout<<na<<" "<<nb<<endl;
na%=s;nb%=s; //转换为不足一块的片数
// cout<<ans<<endl;
if(na+nb>s) //两个不同的片数加起来比一块大,则原方案正确。因为最终还是要多两块完整的
{
printf("%I64d\n",ans);
return 0;
}
//first升序排序,保证两个幸福值之差小的先操作
sort(a.begin(),a.end());
sort(b.begin(),b.end());
ta=0,tb=0; //幸福值改变值
for(int i=0;na;i++)
{
//如果 na>a[i].second 则把这个人要吃的全换为b披萨,否则把na部分换为b披萨
ta+=min(na,a[i].second)*a[i].first;
na-=min(na,a[i].second);
// cout<<"ta "<<ta<<" "<<"na "<<na<<endl;
}
for(int i=0;nb;i++)
{
tb+=min(nb,b[i].second)*b[i].first;
nb-=min(nb,b[i].second);
/// cout<<"tb "<<tb<<" "<<"nb "<<nb<<endl;
}
ans-=min(ta,tb);
printf("%I64d\n",ans);
return 0;
}