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 ...
随机推荐
- C++读取一串不知个数的数字
#include <iostream> using namespace std; int main(){ ]; ; while(cin>>shuzu[i]){ i++; } ; ...
- Android开发笔记之: 数据存储方式详解
无论是神马平台,神马开发环境,神马软件程序,数据都是核心.对于开发平台来讲,如果对数据的存储有良好的支持,那么对应用程序的开发将会有很大的促进作用.总体的来讲,数据存储方式有三种:一个是文件,一个是数 ...
- js实现点击ul/li等改变背景颜色
今天项目遇到了标题所说的问题,找到一篇很高效的例子,值得学习. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//E ...
- Hadoop学习笔记-HDFS命令
进入 $HADOOP/bin 一.文件操作 文件操作 类似于正常的linux操作前面加上“hdfs dfs -” 前缀也可以写成hadoop而不用hdfs,但终端中显示 Use of this scr ...
- 获取url 参数
epresssjs 里面请求参数,4.x 里面有3种方法 1.req.params app.get('user/:id',function(req,res){ res.send('user',req. ...
- [TYVJ] P1026 犁田机器人
犁田机器人 背景 Background USACO OCT 09 2ND 描述 Description Farmer John為了让自己从无穷无尽的犁田工作中解放出来,於是买了个新机器人帮助他犁田 ...
- My Eclipse 自动提示
1.My Eclipse 自带代码提示快捷键 “ alt+/”. 2.输入即提示:window-->preferences-->java-->Editor 展开后点击Content ...
- hex、bin、elf、axf文件区别
AXF和ELF axf文件是ARM的调试文件,除了包含bin的内容之外,还附加了其他的调试信息,这些调试信息加在可执行的二进制数据的前面.在调试的时候,这些调试信息是不必下到RAM中去的,真正下到RA ...
- GStreamer Plugin: Embedded video playback halted; module decodebin20 reported: Your GStreamer installation is missing a plug-in.
标题是在Linux下使用系统yum install 的opencv库来获取视频帧的时候抛出来的错误消息.opencv调用了Gstream的API来处理了视频.错误抛出的代码如下图: http://ub ...
- 在wp中,使用NavigationService.Navigate导航页面出现错误
我们在WP项目中采用页面导航时候,经常会使用以下代码 NavigationService.Navigate(new Uri("/Page1.xaml",UriKind.Relati ...