Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
【额~水dp】 dp[i][j] 把i号单词放入后,i单词所在的行还可以放j个字符的情况下的最小花费#include <iostream> #include <stdio.h> #include <string.h> using namespace std; const int inf=0x3f3f3f3f; const int N=1e4+9; int dp[N][110]; int n,m,a[N]; void init() { scanf("%d%d",&m,&n); for(int i=0;i<n;i++) scanf("%d",&a[i]); memset(dp,0x3f,sizeof(dp)); dp[0][m-a[0]]=(m-a[0])*(m-a[0]); int ans=inf; for(int i=1;i<n;i++) { int tmp=inf; for(int j=0;j<=m;j++) tmp=min(tmp,dp[i-1][j]); dp[i][m-a[i]]=tmp+(m-a[i])*(m-a[i]); if(i==n-1) ans=dp[i][m-a[i]]; for(int j=a[i]+1;j<=m;j++) if(dp[i-1][j]!=inf) { dp[i][j-a[i]-1]=dp[i-1][j]-j*j+(j-a[i]-1)*(j-a[i]-1); if(i==n-1) ans=min(ans,dp[i][j-a[i]-1]); } } printf("%d\n",ans); } int main() { //freopen("in.txt","r",stdin); int w;cin>>w; while(w--) { init(); } // cout << "Hello world!" << endl; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator