soj#547 bzoj5046 分糖果游戏
分析
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e9+7;
int f[160][160],s[160],p[160],v[160];
signed main(){
int n,m,i,j,k,A,B;
scanf("%lld%lld%lld",&n,&A,&B);
for(i=1;i<=n;i++)scanf("%lld%lld",&p[i],&v[i]);
for(i=n;i>0;i--)s[i]=s[i+1]+v[i];
for(i=0;i<=v[n];i++)f[n][i]=-inf;
for(i=v[n]+1;i<=s[n];i++)f[n][i]=inf;
for(i=n-1;i>0;i--)
for(j=0;j<=s[i];j++){
if(v[i]>=j)f[i][j]=-inf;
else f[i][j]=-(f[i+1][s[i]-j+1]-1)-p[i];
int res=max(1ll,f[i+1][j]+p[i]+1);
if(j<=s[i+1]&&f[i][j]>res)f[i][j]=res;
}
int Ans;
for(i=0;i<=s[1];i++)
if(A-B>=f[1][i])Ans=i;
else break;
cout<<Ans<<' '<<s[1]-Ans<<endl;
return 0;
}