codeforces 339C Xenia and Weights(dp或暴搜)
2015-03-30 00:53
302 查看
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Xenia and Weights
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
output
input
output
div2的C题,果断选择暴搜,搜到不符合的就往回跳,搜到正解就结束。。。
dp的话用dp[i][j][k]表示取到第i个的时候前一个重的一头比轻的重j且取的为k是否满足
代码君
Xenia and Weights
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
div2的C题,果断选择暴搜,搜到不符合的就往回跳,搜到正解就结束。。。
dp的话用dp[i][j][k]表示取到第i个的时候前一个重的一头比轻的重j且取的为k是否满足
//##################### //Author:fraud //Blog: http://www.cnblogs.com/fraud/ //##################### #include <iostream> #include <sstream> #include <ios> #include <iomanip> #include <functional> #include <algorithm> #include <vector> #include <string> #include <list> #include <queue> #include <deque> #include <stack> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <climits> #include <cctype> using namespace std; #define XINF INT_MAX #define INF 0x3FFFFFFF #define MP(X,Y) make_pair(X,Y) #define PB(X) push_back(X) #define REP(X,N) for(int X=0;X<N;X++) #define REP2(X,L,R) for(int X=L;X<=R;X++) #define DEP(X,R,L) for(int X=R;X>=L;X--) #define CLR(A,X) memset(A,X,sizeof(A)) #define IT iterator typedef long long ll; typedef pair<int,int> PII; typedef vector<PII> VII; typedef vector<int> VI; string str; int a[110]; int b[100010]; bool flag=0; int n; int tot; int dfs(int d,int lx,int rx){ if(d==n){ flag=1; return 1; } int x; x=upper_bound(a,a+tot,lx-rx)-a; for(int i=x;i<tot;i++){ if(a[i]==b[d-1])continue; b[d]=a[i]; if(dfs(d+1,rx+a[i],lx)){ return 1; } } return 0; } int main() { ios::sync_with_stdio(false); cin>>str; cin>>n; tot=0; fill(a,a+110,INF); for(int i=0;i<str.length();i++) if(str[i]=='1')a[tot++]=i+1; ll lx=0,rx=0; int ans=0; int last=0; int i; int x=0; for(int i=0;i<tot;i++){ b[0]=a[i]; if(dfs(1,a[i],0))break; } if(flag){ cout<<"YES"<<endl; for(int i=0;i<n;i++){ if(i)cout<<" "; cout<<b[i]; } cout<<endl; } else cout<<"NO"<<endl; return 0; }
代码君
相关文章推荐
- CodeForces 456C - Boredom(DP)
- Codeforces 149D Coloring Brackets (不错的区间DP)
- codeforces 337D D. Book of Evil (树形 dp)
- CodeForces - 812B Sagheer, the Hausmeister (dp)
- Codeforces 484D Kindergarten(dp)
- codeforces 484D Kindergarten (dp、贪心)
- Codeforces 373E Watching Fireworks is Fun【思维+单调队列优化Dp+滚动数组】
- codeforces 766 c Mahmoud and a Message(dp)
- codeforces 788C The Great Mixing( BFS / dp+bitset优化 )
- CodeForces - 919D Substring (DP 记忆化搜索)
- CodeForces 149D Coloring Brackets(区间DP)
- CodeForces 219D Choosing Capital for Treeland (树形dp)
- codeforces 55D Beautiful numbers[数位dp]
- codeforces 55D Beautiful numbers(数位dp)
- codeforces 883 I. Photo Processing(dp+二分)
- Codeforces 369D Valera and Fools【思维+dp】
- Codeforces 570E,DP+滚动数组
- codeforces 540D 概率dp
- codeforces-55D-Beautiful numbers(数位DP)
- [数位DP][线性基]Codeforces 388D. Fox and Perfect Sets