codeforces1107G Vasya and Maximum Profit 【模拟】

本文解析一道算法竞赛题目,通过前缀和与模拟的方法求解最大值问题。利用C++实现,详细展示了读取输入、计算前缀和、维护单调栈以及更新答案的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目分析:

前缀和啥的模拟一下就行了。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn = 302000;
 5 
 6 int n,x,d[maxn],sta[maxn],top;
 7 long long minn[maxn],c[maxn],maxx[maxn];
 8 
 9 void read(){
10     scanf("%d%d",&n,&x);
11     for(int i=1;i<=n;i++) scanf("%d%lld",&d[i],&c[i]),c[i]=x-c[i];
12     for(int i=n;i>=1;i--) d[i] -= d[i-1];
13 }
14 
15 void work(){
16     long long ans = 0;
17     for(int i=1;i<=n;i++) ans = max(ans,c[i]);
18     for(int i=1;i<=n;i++) c[i] = c[i-1]+c[i];
19     maxx[0] = 3e18;
20     for(int i=2;i<=n;i++){
21     long long now = 2e18;
22     while(d[sta[top]] <= d[i]&&top>0) {
23         now = min(minn[top],now);
24         top--;
25     }
26     sta[++top] = i; minn[top] = min(now,c[i-2]);
27     maxx[top] = min(1ll*d[i]*d[i]+minn[top],maxx[top-1]);
28     ans = max(ans,c[i]-maxx[top]);
29     }
30     printf("%lld\n",ans);
31 }
32 
33 int main(){
34     read();
35     work();
36     return 0;
37 }

 

转载于:https://www.cnblogs.com/Menhera/p/10351009.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值