[题解]AT_abc240_f [ABC240F] Sum Sum Max
思路
题目要求的是 ,所以我们将 化简一下,得:
在 每增加 时,这个和 将会变为 。
- 如果在 时,显然对于答案是有贡献的,全部加上即可。
- 如果在 时,显然当 没有被减下 时,对于答案都是有贡献的,加上即可。但是在后面需要将剩下的都要加上。
Code
#include <bits/stdc++.h>
#define re register
#define int long long
using namespace std;
const int N = 2e5 + 10,inf = 1e18 + 10;
int T,n,m;
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 1) + (r << 3) + (c ^ 48);
c = getchar();
}
return r * w;
}
signed main(){
T = read();
while (T--){
int Max = -inf,sum = 0,res = 0;
n = read();
m = read();
for (re int i = 1;i <= n;i++){
int x,y;
x = read();
y = read();
if (x >= 0){
res += sum * y + x * (y + 1) * y / 2;
Max = max(Max,res);
}
else{
int l = max(1ll,min(sum / (-x),y));//注意这里需要与 1 取 max,避免 sum / (-x) 为负数
int del = sum * l + x * (l + 1) * l / 2;
Max = max(Max,res + del);
res += sum * y + x * (y + 1) * y / 2;
Max = max(Max,res);
}
sum += x * y;
}
printf("%lld\n",Max);
}
return 0;
}
作者:WaterSun
出处:https://www.cnblogs.com/WaterSun/p/18261969
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
分类:
题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统