[codeforces 339]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.

输入

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

输出

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

输入示例

  1.  

输出示例

  1. YES

数据规模及约定

见“输入

题解

一开始我想贪心,但强大的 cf 数据一巴掌把我打了回来。于是就开始想怎么 dp,发现可以设 f(i, j, k) 表示添加第 i 个砝码,使得砝码 i 所对应的天平中总质量比另一个天平总质量多 j,且当前砝码质量为 k,那么就可以转移了,枚举一下当前添加的砝码的质量,状态数 O(100m),转移 O(10),总时间复杂度相当于 O(m2)。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <stack>
  6. #include <vector>
  7. #include <queue>
  8. #include <cstring>
  9. #include <string>
  10. #include <map>
  11. #include <set>
  12. using namespace std;
  13.  
  14. const int BufferSize = 1 << 16;
  15. char buffer[BufferSize], *Head, *Tail;
  16. inline char Getchar() {
  17. if(Head == Tail) {
  18. int l = fread(buffer, 1, BufferSize, stdin);
  19. Tail = (Head = buffer) + l;
  20. }
  21. return *Head++;
  22. }
  23. int read() {
  24. int x = 0, f = 1; char c = getchar();
  25. while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
  26. while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
  27. return x * f;
  28. }
  29.  
  30. #define maxn 15
  31. #define maxm 1010
  32. char has[maxn];
  33. int m, f[maxm][maxn][maxn], ans[maxm], cnt;
  34. struct Node {
  35. int i, j, k;
  36. Node() {}
  37. Node(int _1, int _2, int _3): i(_1), j(_2), k(_3) {}
  38. } fa[maxm][maxn][maxn];
  39.  
  40. int main() {
  41. scanf("%s", has + 1);
  42. m = read();
  43.  
  44. for(int j = 1; j <= 10; j++)
  45. if(has[j] == '1') f[1][j][j] = 1;
  46. for(int i = 1; i <= m; i++)
  47. for(int j = 1; j <= 10; j++)
  48. for(int k = 1; k <= 10; k++)
  49. fa[i][j][k] = Node(-1, -1, -1);
  50. for(int i = 2; i <= m; i++)
  51. for(int j = 1; j <= 9; j++)
  52. for(int k = 1; k <= 10; k++)
  53. for(int x = 1; x <= 10; x++)
  54. if(has[x] == '1' && x != k && x > j && f[i-1][x-j][k])
  55. f[i][j][x] = 1, fa[i][j][x] = Node(i-1, x-j, k);
  56.  
  57. bool ok = 0;
  58. for(int j = 1; j <= 10; j++) {
  59. for(int k = 1; k <= 10; k++)
  60. if(f[m][j][k]) {
  61. ok = 1;
  62. Node u = Node(m, j, k);
  63. while(u.i >= 0) {
  64. ans[++cnt] = u.k;
  65. u = fa[u.i][u.j][u.k];
  66. }
  67. break;
  68. }
  69. if(ok) break;
  70. }
  71.  
  72. if(!ok) return puts("NO"), 0;
  73. puts("YES");
  74. for(int i = cnt; i > 1; i--) printf("%d ", ans[i]); printf("%d\n", ans[1]);
  75.  
  76. return 0;
  77. }
  78. /*
  79. 1111000011
  80. 16
  81. 1111000011
  82. 1
  83. */

[codeforces 339]C. Xenia and Weights的更多相关文章

  1. [codeforces 339]D. Xenia and Bit Operations

    [codeforces 339]D. Xenia and Bit Operations 试题描述 Xenia the beginner programmer has a sequence a, con ...

  2. 【Codeforces 339】Xenia and Bit Operations

    Codeforces 339 D 题意:给定\(2^n​\)个数字,现在把它们进行如下操作: 相邻的两个数取\(or\) 相邻的两个数取\(xor\) 以此类推,直到剩下一个数. 问每次修改一个数字, ...

  3. 【Codeforces 339C】Xenia and Weights

    [链接] 我是链接,点我呀:) [题意] 在天平上放砝码 你要在左边放一下然后到右边放一下 一直重复这样放m次 每次你放在其中一边都要让另外一边的重量比你少 你可以用1~10中的某些砝码 问你要怎样放 ...

  4. codeforces 339 D.Xenia and Bit Operations(线段树)

    这个题目属于线段树的点更新区间查询,而且查的是整个区间,其实不用写query()函数,只需要输出根节点保存的值就可以了. 题意: 输入n,m表示有2^n个数和m个更新,每次更新只把p位置的值改成b,然 ...

  5. codeforces 339C Xenia and Weights(dp或暴搜)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Xenia and Weights Xenia has a set of weig ...

  6. [codeforces 339]E. Three Swaps

    [codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a ...

  7. Xenia and Weights(深度优先搜索)

    Xenia and Weights time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. CodeForces 339C Xenia and Weights(暴力求解DFS)

    题意:给定 1-10的某几种砝码,给定的每种有无穷多个,然后放 m 个在天平上,要满足,相邻的两次放的砝码不能是同一种,然后是在天平两端轮流放,并且放在哪一个托盘上,那么天平必须是往哪边偏. 析:这个 ...

  9. Xenia and Weights(Codeforces Round #197 (Div. 2)+DP)

    题目链接 传送门 思路 \(dp[i][j][k]\)表示第\(i\)次操作放\(j\)后与另一堆的重量差为\(k\)是否存在. 代码实现如下 #include <set> #includ ...

随机推荐

  1. 在spark-shell里用集群方式启动时加入用户需要的jar

    希望在spark-shell中测试集群方式的elasticsearch操作, # 1 首先下载相关的jar # 2 启动spark-shell时用--jars ./bin/spark-shell –m ...

  2. input的onkeyup效果 超级简短代码

    效果代码 title="请输入正确的十六位数字" onkeyup="javascript: return this.value = this.value.toUpperC ...

  3. iOS注册,找回密码时用到的获取验证码

    #import "ViewController.h" #import "NSTimer+BlocksKit.h" @interface ViewControll ...

  4. iOS7以上图片模糊效果

    模糊后的效果 模糊后的效果 框架UIImage+BlurredFrame里的 applyLightEffectAtFrame方法 例如 bgImage = [bgImage applyLightEff ...

  5. 如何在ubuntu中启用SSH服务

    如何在ubuntu14.04 中启用SSH服务 开篇科普:  SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为 ...

  6. VisualStudio.gitignore git 忽略

    https://github.com/kaedei/gitignore/blob/master/VisualStudio.gitignore

  7. basePath = request.getScheme()+"://"+request.getServerName()+":"+r

    basePath = request.getScheme()+"://"+request.getServerName()+":"+r (2014-06-30 1 ...

  8. /etc/sysctl.conf参数解释

    /etc/sysctl.conf参数解释: fs.file max = 999999 #表示进程(例如一个worker进程)可能同时打开的最大句柄数,直接限制最大并发连接数 net.ipv4.tcp_ ...

  9. 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别

    先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cache.  我们再用. ...

  10. Java调试

    线上load高的问题排查步骤是: 先用top找到耗资源的进程 ps+grep找到对应的java进程/线程 jstack分析哪些线程阻塞了,阻塞在哪里 jstat看看FullGC频率 jmap看看有没有 ...