codeforces 339C Xenia and Weights(dp或暴搜)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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.
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).
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.
0000000101
3
YES
8 10 8
1000000000
2
NO
div2的C题,果断选择暴搜,搜到不符合的就往回跳,搜到正解就结束。。。
dp的话用dp[i][j][k]表示取到第i个的时候前一个重的一头比轻的重j且取的为k是否满足
1 //##################### 2 //Author:fraud 3 //Blog: http://www.cnblogs.com/fraud/ 4 //##################### 5 #include <iostream> 6 #include <sstream> 7 #include <ios> 8 #include <iomanip> 9 #include <functional> 10 #include <algorithm> 11 #include <vector> 12 #include <string> 13 #include <list> 14 #include <queue> 15 #include <deque> 16 #include <stack> 17 #include <set> 18 #include <map> 19 #include <cstdio> 20 #include <cstdlib> 21 #include <cmath> 22 #include <cstring> 23 #include <climits> 24 #include <cctype> 25 using namespace std; 26 #define XINF INT_MAX 27 #define INF 0x3FFFFFFF 28 #define MP(X,Y) make_pair(X,Y) 29 #define PB(X) push_back(X) 30 #define REP(X,N) for(int X=0;X<N;X++) 31 #define REP2(X,L,R) for(int X=L;X<=R;X++) 32 #define DEP(X,R,L) for(int X=R;X>=L;X--) 33 #define CLR(A,X) memset(A,X,sizeof(A)) 34 #define IT iterator 35 typedef long long ll; 36 typedef pair<int,int> PII; 37 typedef vector<PII> VII; 38 typedef vector<int> VI; 39 string str; 40 int a[110]; 41 int b[100010]; 42 bool flag=0; 43 int n; 44 int tot; 45 int dfs(int d,int lx,int rx){ 46 if(d==n){ 47 flag=1; 48 return 1; 49 } 50 int x; 51 x=upper_bound(a,a+tot,lx-rx)-a; 52 for(int i=x;i<tot;i++){ 53 if(a[i]==b[d-1])continue; 54 b[d]=a[i]; 55 if(dfs(d+1,rx+a[i],lx)){ 56 return 1; 57 } 58 } 59 return 0; 60 } 61 62 63 int main() 64 { 65 ios::sync_with_stdio(false); 66 cin>>str; 67 cin>>n; 68 tot=0; 69 fill(a,a+110,INF); 70 for(int i=0;i<str.length();i++) 71 if(str[i]=='1')a[tot++]=i+1; 72 ll lx=0,rx=0; 73 int ans=0; 74 int last=0; 75 int i; 76 int x=0; 77 for(int i=0;i<tot;i++){ 78 b[0]=a[i]; 79 if(dfs(1,a[i],0))break; 80 } 81 if(flag){ 82 cout<<"YES"<<endl; 83 for(int i=0;i<n;i++){ 84 if(i)cout<<" "; 85 cout<<b[i]; 86 } 87 cout<<endl; 88 } 89 else cout<<"NO"<<endl; 90 91 92 93 94 return 0; 95 }