CF#196:DIV2:C-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 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 test(s)
Input
0000000101
3
Output
YES
8 10 8
Input
1000000000
2
Output
NO


 

题意:加砝码的条件1.这次加的和上次加的砝码质量不等

2.这次加的盘的总质量必须大于另一个盘

可以满足就输出方案,否则输出NO

思路:直接模拟即可,不会超时

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int main()
{
    char str[20];
    int hash[20],work[1005],m,i,j,l,r,len,flag,k,x,t;
    while(~scanf("%s%d",str,&m))
    {
        flag = 0;
        k = 1;
        memset(hash,0,sizeof(hash));
        for(i = 0; i<10; i++)
        {
            if(str[i] == '1')
                hash[i+1] = 1;//记录那个砝码是有的
        }
        for(x = 1; x<=10; x++)
        {
            len = 0;
            l = r = 0;
            if(hash[x])//找到存在的砝码,第一次操作放入左盘
            {
                work[len++] = x;//第一次方案
                t = x;//t记录上次存放砝码的质量
                l = x;
            }
            else//不存在
                continue;
            for(i = 1; i<m; i++)//剩下的m-1次操作
            {
                for(j = 1; j<=10; j++)//遍历砝码
                {
                    if(hash[j])//存在此砝码
                    {
                        if(i%2==0)
                        {
                            if(l+j>r && j!=t)//放到做盘比右盘大,且与上次不等
                            {
                                work[len++] = j;
                                l+=j;//累加
                                t = j;
                                break;
                            }
                        }
                        else
                        {
                            if(r+j>l && j!=t)
                            {
                                t = j;
                                work[len++] = j;
                                r+=j;
                                break;
                            }
                        }
                    }
                }
                if(j>10)
                    break;
            }
            if(len == m)//方案数与m相等
            {
                flag = 1;
                break;
            }
        }
        if(!flag)
        {
            printf("NO\n");
            continue;
        }
        printf("YES\n");
        printf("%d",work[0]);
        for(i = 1; i<len; i++)
            printf(" %d",work[i]);
        printf("\n");
    }

    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值