DP优化
单调队列优化
Watching Fireworks is Fun CF372C
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,d,i,j,k,l,r,ma,f[2][150005],g[150005],a[305],b[305],c[305];
int main()
{
cin>>n>>m>>d;
for (i=1;i<=m;i++)
cin>>a[i]>>b[i]>>c[i];
memset(f,207,sizeof(f));
for (i=1;i<=n;i++)
f[0][i]=0;
for (i=1;i<=m;i++)
{
l=1;r=0;k=1;
for (j=1;j<=n;j++)
{
for (;k<=min(n,j+d*(c[i]-c[i-1]));k++)
{
while ((l<=r)and(f[(i-1)%2][k]>=f[(i-1)%2][g[r]])) r--;
g[++r]=k;
}
while ((l<=r)and(g[l]<max(ll(1),j-d*(c[i]-c[i-1])))) l++;
f[i%2][j]=f[(i-1)%2][g[l]]+b[i]-abs(j-a[i]);
}
}
ma=-1e18;
for (i=1;i<=n;i++)
ma=max(ma,f[m%2][i]);
cout<<ma<<'\n';
}
标签:150005,ll,305,long,优化,DP
From: https://www.cnblogs.com/ShadowAA/p/17320827.html