Codeforces Round #197 (Div. 2) -- C. Xenia and Weights (DFS回溯)
2016-07-08 15:53
441 查看
[align=center]C. Xenia and Weights[/align]
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.
Examples
Input
Output
Input
Output
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.
Examples
Input
0000000101 3
Output
YES 8 10 8
Input
1000000000 2
Output
NO 大体题意: 给你长度为10的字符串,只包括0和1,第i 个位置为1 代表你有一个i 千克的砝码,你还有左右两个盘子,你的目的 是放n 个砝码,第一个砝码要放在左边,第二个砝码要放在右边,第三个左边,,,,,其次,你放的这个砝码和 上一次放的砝码 不能同重,并且放上之后这个盘子要严格重于另一个盘子,问是否可以! 可以得话,输出m 个砝码,不可以的话输出NO 思路: 思路就DFS回溯法,没想到这样竟然能过! 开g[2]数组 代表两个盘子,g[0]和g[1],vis数组代表这个砝码是否有! dfs函数中有三个参数 pan,代表改放哪一个盘子,pos 该放第几个了,last 上一个放的什么。 dfs没什么好说的了,就是判断能放就放了!可以用一个位运算 ^1 可以01 之间来回变换! 注意回溯!
注意回溯!
注意回溯!因为忘了回溯 总是wa!!!
#include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<deque> #include<queue> #include<stack> #include<list> typedef long long ll; typedef unsigned long long llu; const int maxn = 100000 + 10; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); const double eps = 1e-8; using namespace std; int g[2]; char s[100]; int vis[maxn]; int ans[maxn]; int m; bool dfs(int pan,int pos,int last){ if (pos > m){ return true; } int t = last; for (int i = 1; i <= 10; ++i){ if (!vis[i])continue; if (g[pan] + i > g[pan^1] && i != last){ last = i; g[pan] += i; if (dfs(pan^1,pos+1,last)){ ans[pos] = i; return true; } g[pan]-=i; last = t; } } // if (pos == m-1)return true; return false; } int main(){ while(scanf("%s",s) == 1){ memset(g,0,sizeof g); memset(vis,0,sizeof vis); scanf("%d",&m); int len = 10; int cnt = 0; for (int i =0 ; i < len; ++i){ if (s[i] == '1')vis[i+1] = 1; } if (!dfs(0,1,-1))printf("NO\n"); else { printf("YES\n"); int len = m; for (int i = 1; i <= len; ++i){ if (i> 1)printf(" "); printf("%d",ans[i]); } printf("\n"); } } return 0; }
相关文章推荐
- 【WINDOWS环境 React Native学习】React Native移植原生Android项目及其各种奇葩Bug解决
- Android ListView的item背景色设置和item点击无响应的解决方法
- hdu3468 Treasure Hunting(二分图)
- 【JZOJ 4595】String
- java web项目整体异常处理机制
- NPOI Excel类
- 页式存储管理系统中------------虚拟地址转换为逻辑地址
- 浅谈html5 响应式布局
- 计算机基本概念的理解
- 设置 select 选中值只读的两种方法
- App版本更新后的引导界面
- Oracle:TNS数据传输协议-进阶篇
- HTTP缓存头Last-Modified和ETag介绍
- pyinstaller生成的exe运行报错 Intel MKL FATAL ERROR: Cannot load mkl_mc3.dll or mkl_def.dll
- 存储过程简介
- 使用Lucid Virtu在有独立显卡的情况下使用Intel硬件加速H.264编码
- python实现redis客户端单例+hbase客户端单例 推荐
- activity生命周期详解
- Layer3 NAT
- java线程——Thread类的使用