Xenia and Weights
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

数据量小,直接搜索即可

 #include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring> using namespace std; char s[];
vector<int> ans;
int m; bool dfs(int n, int rw, int lw, bool rig)
{
if(n == ) return true;
for(int i = ; i <= ; i++){
if(s[i] == ''){
if(!ans.empty() && ans.back() == i) continue;
else{
if(rig){
if(rw + i > lw){
ans.push_back(i);
if(dfs(n - , rw + i, lw, false)) return true;
ans.pop_back();
}
} else {
if(lw + i > rw){
ans.push_back(i);
if(dfs(n - , rw, lw + i, true)) return true;
ans.pop_back();
}
}
}
}
}
return false;
} int main()
{
while(scanf("%s", s + ) != EOF){
scanf("%d", &m);
ans.clear();
if(dfs(m, , , true)){
puts("YES");
vector<int>::iterator it = ans.begin();
printf("%d", *it);
for(it++; it != ans.end(); it++)
printf(" %d", *it);
puts("");
}
else puts("NO");
}
return ;
}

Xenia and Weights(深度优先搜索)的更多相关文章

  1. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

  2. 初涉深度优先搜索--Java学习笔记(二)

    版权声明: 本文由Faye_Zuo发布于http://www.cnblogs.com/zuofeiyi/, 本文可以被全部的转载或者部分使用,但请注明出处. 上周学习了数组和链表,有点基础了解以后,这 ...

  3. 挑战程序2.1.4 穷竭搜索>>深度优先搜索

      深度优先搜索DFS,从最开始状态出发,遍历一种状态到底,再回溯搜索第二种. 题目:POJ2386  思路:(⊙v⊙)嗯  和例题同理啊,从@开始,搜索到所有可以走到的地方,把那里改为一个值(@或者 ...

  4. 回溯 DFS 深度优先搜索[待更新]

      首先申明,本文根据微博博友 @JC向北 微博日志 整理得到,本文在这转载已经受作者授权!   1.概念   回溯算法 就是 如果这个节点不满足条件 (比如说已经被访问过了),就回到上一个节点尝试别 ...

  5. 总结A*,Dijkstra,广度优先搜索,深度优先搜索的复杂度比较

    广度优先搜索(BFS) 1.将头结点放入队列Q中 2.while Q!=空 u出队 遍历u的邻接表中的每个节点v 将v插入队列中 当使用无向图的邻接表时,复杂度为O(V^2) 当使用有向图的邻接表时, ...

  6. [codeforces 339]C. Xenia and Weights

    [codeforces 339]C. Xenia and Weights 试题描述 Xenia has a set of weights and pan scales. Each weight has ...

  7. 深度优先搜索(DFS)

    定义: (维基百科:https://en.wikipedia.org/wiki/Depth-first_search) 深度优先搜索算法(Depth-First-Search),是搜索算法的一种.是沿 ...

  8. 图的遍历之深度优先搜索(DFS)

    深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...

  9. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

随机推荐

  1. Django站点管理--ModelAdmin

    class AuthorAdmin(admin.ModelAdmin): list_display=('name', 'age', 'sex') #指定要显示的字段 search_fields=('n ...

  2. paip.java swt 乱码问题解决

    paip.java swt 乱码问题解决 看累挂,Dfile.encoding是gbk的.. 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专 ...

  3. O2O已死?不!美团点评们迎来新风口

    当年的千团大战,巅峰时期曾涌入了5000多家团购网站,刘旷本人也参与了此次团购大战.而就在当时很多人都唱衰团购的时候,美团和大众点评却最终脱颖而出,市值一路飙升,人人网旗下的糯米网因为卖给了百度,也得 ...

  4. Jquery对Cookie的操作

    第一步:先引用jQuery的插件jquery-1.9.1.min.js 第二步:引用jquery.cookie.js插件 下对cookie的操作: $.cookie("cookieName& ...

  5. jquery时间倒计时

    代码: js: function countDown(time, id) {  //time的格式yyyy/MM/dd hh:mm:ss    var day_elem = $(id).find('. ...

  6. Android Application 对象介绍

    What is Application Application和Actovotu,Service一样是android框架的一个系统组件,当android程序启动时系统会创建一个 application ...

  7. Android显示等宽图片的问题

    安卓开发常遇到一个问题,就是在listView里面,在不知道图片宽高的前提下,另图片布满屏幕(图片宽度等于屏幕宽度,高度自适应).在listView中,只是设置scaleType,imageView. ...

  8. WordPress安装使用问题记录

    本文记录在使用WordPress过程中的问题和解决. 安装 比较顺利没有问题,具体如下(CentOS 6.5,DO的CentOS7 image里默认的yum源没有mysql-serve比较奇怪r): ...

  9. 一致性hash和solr千万级数据分布式搜索引擎中的应用

    互联网创业中大部分人都是草根创业,这个时候没有强劲的服务器,也没有钱去买很昂贵的海量数据库.在这样严峻的条件下,一批又一批的创业者从创业中 获得成功,这个和当前的开源技术.海量数据架构有着必不可分的关 ...

  10. js 生成笛卡尔积

    其实生成 笛卡尔积的方法原本很简单,for循环就可以了, function discarts() { //笛卡尔积 var twodDscartes = function (a, b) { var r ...