【CF933E】A Preponderant Reunion（动态规划）

题解

\begin{aligned} f[l][r]&=f[l][r-2]+c_{r-1}+c_r\\ &=f[l][r-2]+c_{r-1}+\max\{p_r-c_{r-1},0\}\\ &=f[l][r-2]+\max\{p_r,c_{r-1}\}\\ &\ge f[l][r-2]+p_r\\ &=f[l][r-2]+f[r][r] \end{aligned}

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define MAX 300300
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int n,p[MAX],g[MAX];ll f[MAX];
vector<int> Ans;
void Work(int i){int x=min(p[i],p[i+1]);if(!x)return;Ans.push_back(i);p[i]-=x;p[i+1]-=x;}
int main()
{
memset(f,63,sizeof(f));f[0]=0;
for(int i=1;i<=n;++i)
{
f[i]=min(f[max(i-2,0)]+p[i],f[max(i-3,0)]+max(p[i],p[i-1]));
if(f[i]==f[max(i-3,0)]+max(p[i],p[i-1]))g[i]=1;
}
ll ans=min(f[n],f[n-1]);
for(int i=n-(ans==f[n-1]);i>0;i=i-2-g[i])
{
Work(i-1);if(g[i])Work(i-2);Work(i);
}
printf("%d\n",(int)Ans.size());
for(int v:Ans)printf("%d\n",v);
return 0;
}
posted @ 2019-07-09 17:25  小蒟蒻yyb  阅读(688)  评论(0编辑  收藏  举报