Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

【额~水dp】 dp[i][j] 把i号单词放入后,i单词所在的行还可以放j个字符的情况下的最小花费

Posted by WilliamACM at 2012-12-30 21:16:41 on Problem 3390
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator