B. Ordering Pizza
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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 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.
Output
Print the maximum total happiness that can be achieved.
Examples
inputCopy
3 12
3 5 7
4 6 7
5 9 5
outputCopy
84
inputCopy
6 10
7 4 7
5 8 8
12 5 8
6 11 6
3 3 7
5 9 6
outputCopy
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.
题目大意:
每个人都需要吃s[i]块的披萨,吃第一种有ai的快乐值,吃第二种有bi的快乐值,问在保证最少的pizza数量最少的情况下,总的快乐值 如何最大。
题目分析
先按照每个人最喜欢的那种算一个快乐值,然后就是考虑一下吃ai的有多少人转化成吃bi,bi的同理,最后ans-min(s1,s2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Maxn 100005
#define LL long long
using namespace std;
LL a[Maxn],b[Maxn],s[Maxn];
struct Node{
LL x;
int s;
}c1[Maxn],c2[Maxn];
inline bool cmp(Node A,Node B) { return A.x < B.x; }
int main(int argc,char *argv[]) {
LL S,n,ans = 0,sum = 0,s1 = 0,s2 = 0,cnt1 = 0,cnt2 = 0;
cin >> n >> S;
for(int i=1; i<=n; i++) {
cin >> s[i] >> a[i] >> b[i];
if(a[i] > b[i]) s1 += s[i];
else if(a[i] < b[i]) s2 += s[i];
ans += max(a[i],b[i]) * s[i];
sum += s[i];
if(a[i] > b[i]) c1[++cnt1].s = s[i],c1[cnt1].x = a[i] - b[i];// 选了 a 再次选择b的损失
else if(a[i] < b[i]) c2[++cnt2].s = s[i],c2[cnt2].x = b[i] - a[i];//
}
LL cnt = (sum + S - 1) / S;
if(((s1 + S - 1) / S + (s2 + S - 1) / S) <= cnt ) {
cout << ans << endl;
}else {
LL summary1 = 0,summary2 = 0;
s1 %= S; s2 %= S;
sort(c1 + 1,c1 + cnt1 + 1,cmp);
sort(c2 + 1,c2 + cnt2 + 1,cmp);
for(int i=1; i<=cnt1; i++) {
if(s1 == 0) break;
LL num = min((LL)c1[i].s,s1);
summary1 += num * c1[i].x;
s1 -= num;
}
for(int i=1; i<=cnt2; i++) {
if(s2 == 0) break;
LL num = min((LL)c2[i].s,s2);
summary2 += num * c2[i].x;
s2 -= num;
}
cout << ans - min(summary1,summary2) << endl;
}
return 0;
}