题解:[Algo Beat Contest 002.5 B] 草莓小蛋糕 (cakes)
SANJIAOJIE · · 题解
题目大意
有共计
分析
通过观察,我们可以发现,在第
代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
__int128 n,res;
struct Cake{
__int128 ci,di,xi;
}a[N]; //定义结构体存储蛋糕信息
bool cmp(Cake A,Cake B){return A.xi>B.xi;} //定义比较规则
// 读入 __int128 的函数,会返回读入的数。
__int128 read() {
char c;
bool isf = 0;
while (!isdigit(c = getchar())) {
isf = (c == '-');
}
__int128 res = (c ^ 48);
while (isdigit(c = getchar())) {
res = (res << 3) + (res << 1) + (c ^ 48);
}
return isf ? -res : res;
}
// 输出 __int128 的函数。需传入需要输出的数作为参数。
void write(__int128 x) {
if (x < 0) {
putchar('-'), x = -x;
}
if (x >= 10) {
write(x / 10);
}
putchar('0' + x % 10);
}
int main(){
n=read();
for(int i=1;i<=n;i++){a[i].ci=read();a[i].di=read();a[i].xi=read();}
sort(a+1,a+n+1,cmp); //排序
__int128 m=0; //存储时间
for(int i=1;i<=n;i++){
res=res+a[i].ci*a[i].di; //先加上第1天的蛋糕美味值
res=res-a[i].xi*(m*a[i].ci+a[i].ci*(a[i].ci-1)/2); //扣除变质值
m=m+a[i].ci; //推进时间
}
write(res);
return 0;
}
::::info[变质值计算方式]{open}
先扣除第
接着根据等差数列求和,得到吃的过程中扣除的变质值
| 由上得到扣除的总变质值为 |
|---|
总结
综合思维与计算复杂度,本题符合中等偏下难度,特征为:
-
贪心策略简单易发现
-
数学推导步骤少但易因符号等原因混淆出错
综上所述,建议评为橙题。
::::info[声明]{open}
- 代码中快读快写使用作者提供的代码。
-
总结部分使用 Deepseek 进行语言优化。