SDKD 2016 Summer Single Contest #03.G

Description
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.

Sample Input
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO
题目:
读题意,还是读题意,读不懂就做不了,好多次了.╮(╯▽╰)╭
首先给一个01字符串,这个意思代表了你拥有的砝码的种类,而且有且只有10种;
比如1000000000 代表只有 1 重量的砝码,这种砝码无数个,
再比如 0000010001 代表只有 6 和 10 重量的砝码 ;
然后 往两个 盘子里放砝码 , 放的时候 左一个右一个, 一个一个放,
并且 连续的两个不能相同,并且保证在一个盘子上放上砝码之后要比另一个重量大;
限定次数m…..
看了网上的代码.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1000 + 10;
int m;
bool flag = false;
vector<int > v;
string s;
//bool k代表 往哪一个盘子放, 0和1 代表左和右;
//left right 表示 分别左边和右边 的重量
//pre  代表 上一个 砝码的重量 cnt 表示次数;

void dfs(int cnt, int pre, int left, int right, bool k)
{
    if(flag == true)
        return ;
    if(cnt == m)
    {
        cout << "YES" << endl;
        for(int i = 0; i < m; i++)
        {
            if(i != 0)
                cout << " ";
            cout << v[i];
        }
        cout << endl;
        flag = true;
        return ;
    }
    for(int i = 0; i < 10; i++)
    {
        if(s[i] == '1' && (i+1) != pre)
        {
            if(k == true)//往一个盘子里放
            {
                if((left + i + 1) > right)
                {
                    v.push_back(i+1);
                    //k要变成!k;
                    dfs(cnt+1, i+1, left+i+1, right, !k);
                    //这里的pop()是为了:这样递归不能完成操作,但是还有其他的解.
                    //也就是说cnt没有到m但是两边放砝码不能连续两个不同,且不能使一边大于另一边;
                    //这样就需要把放上的去掉,换下一个然后继续操作,说的可能不清楚,慢慢理解吧.
                    v.pop_back();
                }
            }
            else if(k == false)
            {
                if((right + i + 1) > left)
                {
                    v.push_back(i+1);
                    dfs(cnt+1, i+1, left, right+i+1, !k);
                    v.pop_back();
                }
            }
        }
    }
}

int main()
{
    cin >> s >> m;
    v.clear();
    dfs(0, -1, 0, 0, true);
    if(flag == false)
    {
        cout << "NO" << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值