CF1083E The Fair Nut and Rectangles 题解
hongshixiaobai · · 题解
首先显而易见有
注意到可以斜率优化,然后就做完了
接下来讲怎么推斜率优化式子
首先考虑何时会从
显然的,由上面的转移式有
然后开始推式子
观察发现左边式子很像一个斜率的形式,于是考虑以
然后对于三个点
然后发现删去所有不优的转移后剩下的点构成一个上凸壳,由于是单调栈维护,每次二分找最优的转移位置即可。
同时由于这题
AC 代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,dp[1000005],st[1000005],top,ans,q;
struct _
{
int x,y,a;
}r[1000005];
bool cmp(_ a,_ b)
{
return a.x<b.x;
}
double xl(int i,int j)
{
return (double)(dp[i]-dp[j])/(r[i].x-r[j].x);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n;
for(int i = 1;i<=n;i++)
{
cin>>r[i].x>>r[i].y>>r[i].a;
}
sort(r+1,r+n+1,cmp);
st[++top] = 0;
st[++top] = 1;
dp[1] = r[1].x*r[1].y-r[1].a;
ans = max(ans,dp[1]);
q = 1;
for(int i = 2;i<=n;i++)
{
while(q<top&&xl(st[q],st[q+1])>=r[i].y)q++;
int j = st[q];
dp[i] = max(r[1].x*r[1].y-r[1].a,dp[j]+(r[i].x-r[j].x)*r[i].y-r[i].a);
while(top>q&&xl(st[top-1],st[top])<=xl(st[top],i))top--;
st[++top] = i;
ans = max(ans,dp[i]);
}
cout<<ans;
}