当前位置: 代码迷 >> 综合 >> CodeForces 234F Fence (DP)
  详细解决方案

CodeForces 234F Fence (DP)

热度:68   发布时间:2023-11-15 10:53:04.0

题目链接:http://codeforces.com/problemset/status

#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first                                  
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=2e2+10;
const int maxm=4e4+100;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);
}
int n,a,b,INF;
int h[maxn],tot[maxn];
int dp[maxn][maxm][2];
int main(){ios::sync_with_stdio(false);freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);cin>>n>>a>>b;rep(i,1,n+1){cin>>h[i];tot[i]=tot[i-1]+h[i];}mst(dp,0xf),INF=dp[0][0][0];mst(dp[0][0],0);rep(i,0,n) rep(j,0,maxm){if(dp[i][j][0]!=INF){if(j+h[i+1]<=a){dp[i+1][j+h[i+1]][0]=min(dp[i+1][j+h[i+1]][0],dp[i][j][0]);}if(tot[i+1]-j<=b){dp[i+1][j][1]=min(dp[i+1][j][1],dp[i][j][0]+min(h[i],h[i+1]));}}if(dp[i][j][1]!=INF){if(j+h[i+1]<=a){dp[i+1][j+h[i+1]][0]=min(dp[i+1][j+h[i+1]][0],dp[i][j][1]+min(h[i],h[i+1]));}if(tot[i+1]-j<=b){dp[i+1][j][1]=min(dp[i+1][j][1],dp[i][j][1]);}}}int ans=INF;rep(i,0,a+1) rep(j,0,2) ans=min(ans,dp[n][i][j]);if(ans==INF) cout<<"-1";else cout<<ans;return 0;
}