codeforces 339C 记忆化搜索
来源:互联网 发布:ipad版怎么看淘宝直播 编辑:程序博客网 时间:2024/05/29 11:38
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.
00000001013
YES8 10 8
10000000002
NO
题意: 有一个天平, 有10种砝码, 分别往两边放置砝码, 每次放砝码必须满足两个条件: 一是放置的砝码不能跟上一次的一样, 二是放置这个砝码的时候, 这边的盘子的总重量必须大于另一边的总重量.
分析: 利用记忆化搜索来做就行了, 分别记录位置, 差值, 以及上一次的选择.
#include<bits/stdc++.h>#define inf 0x3f3f3f3fusing namespace std;typedef long long ll;typedef pair<int,int> pii;const int N=1010,MOD=1e9+7;bool dp[N][12][12];int path[N];string s;int m;bool dfs(int pos,int differ,int pre){ if(pos>0 && (differ <=0 || differ > 10)) return 0; if(pos == m){ puts("YES"); for(int i=0;i<m;i++) printf("%d ",path[i]); return 1; } if(dp[pos][differ][pre]) return 0; dp[pos][differ][pre]=1; for(int i=1;i<=10;i++){ if(s[i-1]=='1' && i!=pre){ path[pos] = i; if(dfs(pos+1,i-differ,i)) return 1; } } return 0;}int main(){ cin>>s; cin>>m; if(!dfs(0,0,-1)) puts("NO"); return 0;}
- codeforces 339C 记忆化搜索
- CodeForces 374C 记忆化搜索
- codeforces 721C 记忆化搜索 / dp
- Codeforces Round #247 (Div. 2)(C)记忆化搜索
- codeforces (数位dp 记忆化搜索)
- codeforces B. Cow Program (记忆化搜索)
- Codeforces 208 B Solitaire(记忆化搜索)
- Codeforces 750D 记忆化搜索
- codeforces E. Kidnapping(记忆化搜索)
- Codeforces Round #220 (Div. 2) C. Inna and Dima (记忆化搜索)
- Codeforces Round #220 (Div. 2)C题:Inna and Dima(记忆化搜索+DP)
- codeforces 505C. Mr. Kitayuta, the Treasure Hunter (记忆化搜索)
- Codeforces 505C Mr. Kitayuta, the Treasure Hunter【记忆化搜索+分块数据处理】好题
- Codeforces Round #390 (Div. 2) C. Vladik and chat (DP/记忆化搜索)
- Codeforces Round #390 (Div. 2)(A,B,C(记忆化搜索),D(贪心,优先队列))
- Codeforces Round #406 (Div. 2):C. Berzerk(记忆化搜索解决博弈问题)
- Codeforces Round #439 (Div. 2) C.The Intriguing Obsession(组合数、记忆化搜索)
- codeforces 118D 记忆搜索
- js 数据类型与转换
- 再谈HashMap-由一个实际问题引发的对HashMap设计吐嘈
- Jersey 2.22.2 官方文档第7章学习笔记
- poj2226 muddy fields
- 百度地图获取二个经纬度坐标间的距离
- codeforces 339C 记忆化搜索
- ios移动端直播
- unity3d中让物体显示和隐藏的方法
- 你是想读书,还是想读完书?
- php 数据类型与转换
- Windows下QT软件发布
- String.matches()的用法/正则表达式
- struts文件上传下载
- SpringMVC Controller 介绍