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 put m 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.
Examples
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
思路:这个数据量比较小,可以直接搜索。
我们记录一下上一个数字是什么,以及计算一下两边的差值。然后二分找出第一个符合的条件,之后再遍历就可以了。如果有一个符合的,就直接返回。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=3e3+100;
vector<int> p;
string s;
int n;
inline int dfs(int id,int sum1,int sum2,int flag,int pre,vector<int> &a1,vector<int> &a2)
{
if(id==n+1) return 1;
if(flag==0)
{
int pos=upper_bound(p.begin(),p.end(),sum2-sum1)-p.begin();
for(int i=pos;i<p.size();i++)
{
if(p[i]!=pre)
{
a1.push_back(p[i]);
if(dfs(id+1,sum1+p[i],sum2,flag^1,p[i],a1,a2)) return 1;
a1.pop_back();
}
}
}
else
{
int pos=upper_bound(p.begin(),p.end(),sum1-sum2)-p.begin();
for(int i=pos;i<p.size();i++)
{
if(p[i]!=pre)
{
a2.push_back(p[i]);
if(dfs(id+1,sum1,sum2+p[i],flag^1,p[i],a1,a2)) return 1;
a2.pop_back();
}
}
}
return 0;
}
int main()
{
cin>>s>>n;
p.clear();
for(int i=0;i<s.length();i++) if(s[i]=='1') p.push_back(i+1);
vector<int> ans1,ans2;
if(!dfs(1,0,0,0,0,ans1,ans2)) cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl;
int i=0,j=0;
while(1)
{
if(i<ans1.size()) cout<<ans1[i]<<" ";
if(j<ans2.size()) cout<<ans2[j]<<" ";
i++,j++;
if(i>=ans1.size()&&j>=ans2.size()) break;
}
cout<<endl;
}
return 0;
}
努力加油a啊,(o)/~