cf C. Xenia and Weights

http://codeforces.com/contest/339/problem/C

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <algorithm>

 4 using namespace std;

 5 

 6 int dp[1002][15][15];

 7 char str[100];

 8 int f[100];

 9 int m;

10 void dfs(int i,int j,int k)

11 {

12     if(i==0) return ;

13     dfs(i-1,k-j,dp[i][j][k]);

14     printf("%d ",k);

15 }

16 

17 int main()

18 {

19     scanf("%s",str);

20     int cnt=0;

21     for(int i=0; i<10; i++)

22     {

23         if(str[i]=='1')

24         {

25             f[++cnt]=i+1;

26         }

27     }

28     scanf("%d",&m);

29     memset(dp,-1,sizeof(dp));

30     dp[0][0][0]=0;

31     for(int i=1; i<=m; i++)

32     {

33         for(int j=0; j<=10; j++)

34         {

35             for(int k=0; k<=10; k++)

36             {

37                 if(dp[i-1][j][k]!=-1)

38                 {

39                     for(int c=1; c<=cnt; c++)

40                     {

41                         if(f[c]>j&&f[c]!=k)

42                         dp[i][f[c]-j][f[c]]=k;

43                     }

44                 }

45             }

46         }

47     }

48     for(int i=1; i<=10; i++)

49     {

50         for(int j=1; j<=10; j++)

51         {

52             if(dp[m][i][j]!=-1)

53             {

54                 printf("YES\n");

55                 dfs(m,i,j);

56                 return 0;

57             }

58         }

59     }

60     printf("NO\n");

61     return 0;

62 }
View Code

 

你可能感兴趣的:(xen)