Codeforces Round #197 (Div. 2) c. Xenia and Weights

发布时间:2017-1-23 22:38:01 编辑 分享查询网我要评论
本篇文章主要介绍了"Codeforces Round #197 (Div. 2) c. Xenia and Weights",主要涉及到Codeforces Round #197 (Div. 2) c. Xenia and Weights方面的内容,对于Codeforces Round #197 (Div. 2) c. Xenia and Weights感兴趣的同学可以参考一下。

C. Xenia and Weights time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Xenia has a set of weights and pan scales. Each weight has an integer weight from 1 to 10 kilos. Xenia is going to play with scales and weights a little. For this, she puts weights on the scalepans, one by one. The first weight goes on the left scalepan, the second weight goes on the right scalepan, the third one goes on the left scalepan, the fourth one goes on the right scalepan and so on. Xenia wants to put the total of m weights on the scalepans. Simply putting weights on the scales is not interesting, so Xenia has set some rules. First, she does not put on the scales two consecutive weights of the same weight. That is, the weight that goes i-th should be different from the (i + 1)-th weight for any i (1 ≤ i < m). Second, every time Xenia puts a weight on some scalepan, she wants this scalepan to outweigh the other one. That is, the sum of the weights on the corresponding scalepan must be strictly greater than the sum on the other pan. You are given all types of weights available for Xenia. You can assume that the girl has an infinite number of weights of each specified type. Your task is to help Xenia lay m weights on ​​the scales or to say that it can't be done. Input The first line contains a string consisting of exactly ten zeroes and ones: the i-th (i ≥ 1) character in the line equals "1" if Xenia has i kilo weights, otherwise the character equals "0". The second line contains integer m (1 ≤ m ≤ 1000). Output In the first line print "YES", if there is a way to put m weights on the scales by all rules. Otherwise, print in the first line "NO". If you can putm weights on the scales, then print in the next line m integers — the weights' weights in the order you put them on the scales. If there are multiple solutions, you can print any of them. Sample test(s) input 0000000101 3 output YES 8 10 8 input 1000000000 2 output NO 很简单的一个深搜就解决问题了!注意,题目意思,是每一次放上去,都要使两边不平衡,这样才能解决问题! #include <stdio.h> #include <string.h> #include <iostream> using namespace std; int m,ans,weight[11],re[10000]; int dfs(int step,int last,int left,int right,int goal) { int i; if(step==m) { if(left!=right) { ans=0; re[ans++]=weight[last]; return 1; } else return -1; } for(i=0;i<ans;i++) { if(i!=last) { if(goal) { if(left+weight[i]>right) { if(dfs(step+1,i,left+weight[i],right,goal^1)==1) { if(last!=-1) re[ans++]=weight[last]; return 1; } } } else { if(left<right+weight[i]) { if(dfs(step+1,i,left,right+weight[i],goal^1)==1) { if(last!=-1) re[ans++]=weight[last]; return 1; } } } } } return -1; } int main () { char str[11]; int i; while(scanf("%s",str)!=EOF) { for(ans=0,i=0;str[i]!='\0';i++) if(str[i]-'0') weight[ans++]=i+1; scanf("%d",&m); if(dfs(0,-1,0,0,0)==-1) printf("NO\n"); else { printf("YES\n"); for(i=ans-1;i>=0;i--) if(i) printf("%d ",re[i]); else printf("%d\n",re[i]); } } return 0; }