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. JfreeChart使用(转载)

    http://www.cnblogs.com/xingyun/ http://www.huosen.net/archives/156.html(此篇除了struts2外,还介绍了servlet下Jfr ...

  2. DOM操作(Window.document对象)

    间隔与延迟: 间隔一段代码: window.setInterval("代码",间隔执行秒数) 延迟一段时间后执行一段代码: window.setTimeout("执行代码 ...

  3. paip.slap工具与于64位win7与JDBC的性能对比

    paip.slap工具与于64位win7与JDBC的性能对比 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog ...

  4. paip.互联网产品要成功的要素

    paip.互联网产品要成功的要素 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net/atti ...

  5. fir.im Weekly - 如果让你重新做一款APP

    设想下:如果让你重新做一款 APP ,你会用到哪些开发.设计等资源和工具? 本期的 Weekly 为大家分享了最近不错的 APP 开发资源,大部分是关于 iOS 开发. Android 开发.UI设计 ...

  6. python pycurl模块

    一.pycurl概述 PycURl是一个C语言写的libcurl的python绑定库.libcurl 是一个自由的,并且容易使用的用在客户端的 URL 传输库.它的功能很强大,在PyCURL的主页上介 ...

  7. sqlserver数据库安全函数、配置函数、游标函数、行级函数、排名函数、元数据函数、系统统计函数 、文本和图像函数--收藏着有用

    行级函数:下列行集函数将返回一个可用于代替 Transact-SQL 语句中表引用的对象. CONTAINSTABLE 返回具有零行.一行或多行的表,这些行的列中包含的基于字符类型的数据是单个词语和短 ...

  8. 编写高质量代码改善C#程序的157个建议——导航开篇

    前言 由于最近工作重心的转移,原来和几个同事一起开发的项目也已经上线了,而新项目就是在现有的项目基础上进行优化延伸扩展.打个比方,现在已经上线的项目行政案件的Web管理网站(代码还没那么多相比较即将要 ...

  9. C#与C++之间类型的对应

    Windows Data Type .NET Data Type BOOL, BOOLEAN Boolean or Int32 BSTR String BYTE Byte CHAR Char DOUB ...

  10. GitHub前50名的Objective-C动画相关库

    GitHub的Objective-C的动画UI库其实是最多的一部分,GitHub有相当一部分的动画大牛,如Jonathan George,Nick Lockwood,Kevin,Roman Efimo ...