codeforces 339C Xenia and Weights(dp或暴搜)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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.
0000000101
3
YES
8 10 8
1000000000
2
NO
div2的C题,果断选择暴搜,搜到不符合的就往回跳,搜到正解就结束。。。
dp的话用dp[i][j][k]表示取到第i个的时候前一个重的一头比轻的重j且取的为k是否满足
//#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
string str;
int a[];
int b[];
bool flag=;
int n;
int tot;
int dfs(int d,int lx,int rx){
if(d==n){
flag=;
return ;
}
int x;
x=upper_bound(a,a+tot,lx-rx)-a;
for(int i=x;i<tot;i++){
if(a[i]==b[d-])continue;
b[d]=a[i];
if(dfs(d+,rx+a[i],lx)){
return ;
}
}
return ;
} int main()
{
ios::sync_with_stdio(false);
cin>>str;
cin>>n;
tot=;
fill(a,a+,INF);
for(int i=;i<str.length();i++)
if(str[i]=='')a[tot++]=i+;
ll lx=,rx=;
int ans=;
int last=;
int i;
int x=;
for(int i=;i<tot;i++){
b[]=a[i];
if(dfs(,a[i],))break;
}
if(flag){
cout<<"YES"<<endl;
for(int i=;i<n;i++){
if(i)cout<<" ";
cout<<b[i];
}
cout<<endl;
}
else cout<<"NO"<<endl; return ;
}
代码君
codeforces 339C Xenia and Weights(dp或暴搜)的更多相关文章
- CodeForces 339C Xenia and Weights(暴力求解DFS)
题意:给定 1-10的某几种砝码,给定的每种有无穷多个,然后放 m 个在天平上,要满足,相邻的两次放的砝码不能是同一种,然后是在天平两端轮流放,并且放在哪一个托盘上,那么天平必须是往哪边偏. 析:这个 ...
- codeforces 339C Xenia and Bit Operations(线段树水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Bit Operations Xenia the beginn ...
- codevs 1085 数字游戏 dp或者暴搜
1085 数字游戏 2003年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单 ...
- [codeforces 339]C. Xenia and Weights
[codeforces 339]C. Xenia and Weights 试题描述 Xenia has a set of weights and pan scales. Each weight has ...
- Codeforces 1519F - Chests and Keys(暴搜+以网络流为状态的 dp)
Codeforces 题目传送门 & 洛谷题目传送门 难度终于出来了--又独立切掉一道 *3200,凯信(所以我已经独立切掉三道 *3200 了?) 首先考虑我们已经知道了每个宝箱上有哪些锁, ...
- Xenia and Weights(深度优先搜索)
Xenia and Weights time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- suoi62 网友跳 (暴搜+dp)
传送门 sbw太神啦orz 首先N<=20可以直接暴搜 然后玄学剪枝可以过18个点 那么N<=40的时候,就把它拆成两半分别暴搜,再用dp拼起来 对于前半段,设f[i][j]是开始高度为i ...
- 子矩阵(暴搜(全排列)+DP)
子矩阵(暴搜(全排列)+DP) 一.题目 子矩阵 时间限制: 1 Sec 内存限制: 128 MB 提交: 1 解决: 1 [提交][状态][讨论版] 题目描述 给出如下定义: 1. 子矩阵:从一 ...
- CodeForces - 357D - Xenia and Hamming
先上题目: D. Xenia and Hamming time limit per test 1 second memory limit per test 256 megabytes input st ...
随机推荐
- 转载:js实现上传图片时 点击浏览后 就可以看到缩略图 很实用
转载网址:http://blog.sina.com.cn/s/blog_6094f04d0100o6kj.html <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- 文成小盆友python-num2 数据类型、列表、字典
一.先聊下python的运行过程 计算机是不能够识别高级语言的,所以当我们运行一个高级语言程序的时候,就需要一个“翻译机”来从事把高级语言转变成计算机能读懂的机器语言的过程.这个过程分成两类,第一种是 ...
- 用python随机生成数据,再插入到postgresql中
用python随机生成学生姓名,三科成绩和班级数据,再插入到postgresql中. 模块用psycopg2 random import random import psycopg2 fname=[' ...
- AngularJS中的控制器示例_2
<!doctype html> <html ng-app="myApp"> <head> <script src="C:\\Us ...
- DAO 基础学习笔记
一.DAO 1.概念: (1)Date Access Object(数据存取对象) (2)位于业务逻辑和持久化数据之间 (3)实现对持久化数据的访问 (4)类---> DAO --->数据 ...
- a:hover和a:visited书写顺序的重要性
2a:hover和a:visited书写顺序的重要性今天在用a:hover属性的时候发现一个奇怪的问题,同一个页面里面有些链接的a:hover效果不能正常表现出来.链接的代码是一样,没有使用其它样式固 ...
- HEX转BIN源码分析(51系列)
以前写的一个Atmel的S5X的下载程序,其中有支持HEX格式的文件,所以将这个程序贴出来,程序的意思是将输入的HEX文件转换为BIN格式的文件,并存储到文件中,注意不支持64K的扩展模式. int ...
- PowerShell中的输出
1 输出重定向 > 或者>> 2 输出控制 out-* -paging#分页输出 get-process | out-host -paging more指令用于屏显 get-pro ...
- esxi5.5 安装,虚拟机复制
尝试在vmware workstation上安装hadoop,感觉太慢了. 好在家里的台式机配置还可以,所以就想在它上面虚拟出几台服务器出来. 台式机配置如下: 虚拟出来三个应该没问题了吧. 第一步, ...
- SxsTrace工具使用方法
Windows7平台上有一个强大的SxsTrace工具,可以跟踪调试应用程序运行时需要的动态库的版本和路径. SxsTrace使用的方法: 1.首先必须以Administrator用户身份登录 ...