ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1167. Bicolored Horses

Why my solution WA#1? Help Me! Thanks!
Posted by pswgoo 15 Jan 2013 09:35
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int M1 = 555;
const int INF = 111111111;
int N,K;
int ans;
int w[M1][M1];//w[i][j] is the number of white horses in the last stable when the first i horses were placed in 1st j stables with minimum unhappiness
int b[M1][M1];//b[i][j] is the number of black horses in the last stable when the first i horses were placed in 1st j stables with minimum unhappiness
int f[M1][M1];//f[i][j] means the minimum unhappiness when the first i horses were placed in 1st j stables
//function: f[i][j] = min(f[i-1][j-1],f[i-1][j]+w[i-1][j] or b[i-1][j]);

int main()
{
    memset(w,0,sizeof(w));
    memset(b,0,sizeof(b));
    memset(f,0,sizeof(f));
    scanf("%d%d",&N,&K);
    for(int i=0;i!=M1;++i)
    {   for(int j=i+1;j!=M1;++j)
            f[i][j] = INF;
        f[i][0] = INF;
    }
    int tmp;
    scanf("%d",&tmp);
    if(tmp) ++b[1][1];
    else ++w[1][1];
    f[1][1] = 0;
    for(int i=2;i<=N;++i)
    {   scanf("%d",&tmp);
        for(int j=1;j<=min(i,K);++j)
        {   f[i][j] = f[i-1][j-1];
            b[i][j] = 0;
            w[i][j] = 0;
            if(tmp)
            {   ++b[i][j];
                if(f[i-1][j]+w[i-1][j]<f[i][j])
                {   b[i][j] = b[i-1][j]+1; w[i][j] = w[i-1][j];
                    f[i][j] = f[i-1][j]+w[i-1][j];
                }
            }
            else
            {   ++w[i][j];
                if(f[i-1][j]+b[i-1][j]<f[i][j])
                {   b[i][j] = b[i-1][j]; w[i][j] = w[i-1][j]+1;
                    f[i][j] = f[i-1][j]+b[i-1][j];
                }
            }
        }
    }
    ans = f[N][K];
    printf("%d",ans);
    return 0;
}