codeforces1107G Vasya and Maximum Profit 【模拟】
题目分析:
前缀和啥的模拟一下就行了。
代码:
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 }