【Codeforces 339C】Xenia and Weights
【链接】 我是链接,点我呀:)
【题意】
在天平上放砝码
你要在左边放一下然后到右边放一下
一直重复这样放m次
每次你放在其中一边都要让另外一边的重量比你少
你可以用1~10中的某些砝码
问你要怎样放才行,或者告知系统不能放m次
【题解】
动态规划
设dp[i][j][k]表示第i轮结束之后,左边右边的重量差的绝对值为j,最后一个放的砝码重量为k的情况能否达到
枚举一下每次用哪种砝码(只要不和之前一个状态最后一个用的一样就好)做一下转移即可。
(倒推然后写一个记忆化搜索可能更方便,因为可以直接打印出最后的结果
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = 10;
static int M = 1000;
static class Task{
String s;
int a[] = new int[N+10],n;
boolean can[][][] = new boolean[M+10][N+10][N+10];
int pre[][][] = new int[M+10][N+10][N+10];
int m;
void dfs(int dep,int delta,int last) {
if (dep==0) return;
int prelast = pre[dep][delta][last];
dfs(dep-1,last-delta,prelast);
out.print(last+" ");
}
public void solve(InputReader in,PrintWriter out) {
s = in.next();
m = in.nextInt();
for (int i = 1;i <= 10;i++)
if (s.charAt(i-1)=='1') {
a[++n] = i;
}
can[0][0][0] = true;
for (int i = 0;i < m;i++)
for (int delta = 0;delta <= N;delta++)
for (int j = 0;j <= N;j++)
if (can[i][delta][j]==true) {
for (int j2 = 1;j2 <= n;j2++)
if (a[j2]>delta && a[j2]!=j) {
can[i+1][a[j2]-delta][a[j2]] = true;
pre[i+1][a[j2]-delta][a[j2]] = j;
}
}
for (int delta = 0;delta <= N;delta++)
for (int j = 0;j <= N;j++)
if (can[m][delta][j]) {
out.println("YES");
dfs(m,delta,j);
return;
}
out.println("NO");
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
【Codeforces 339C】Xenia and Weights的更多相关文章
- 【Codeforces 342A】Xenia and Divisors
[链接] 我是链接,点我呀:) [题意] [题解] 最后a,b,c只有以下3种情况 1,2,4 1,2,6 1,3,6 那么用cnt[8]统计每个数字出现的次数. 输出cnt[4]次1,2,4 (如果 ...
- 【Codeforces 339】Xenia and Bit Operations
Codeforces 339 D 题意:给定\(2^n\)个数字,现在把它们进行如下操作: 相邻的两个数取\(or\) 相邻的两个数取\(xor\) 以此类推,直到剩下一个数. 问每次修改一个数字, ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- [codeforces 339]C. Xenia and Weights
[codeforces 339]C. Xenia and Weights 试题描述 Xenia has a set of weights and pan scales. Each weight has ...
- 【42.86%】【codeforces 742D】Arpa's weak amphitheater and Mehrdad's valuable Hoses
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【30.23%】【codeforces 552C】Vanya and Scales
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【22.73%】【codeforces 606D】Lazy Student
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- JVM系列-类加载机制
简介 在java中,类的声明周期总共分为以下几种: 加载(Loading),验证(Verification),准备(Preparation),解析(Analysis), 初始化(Initializat ...
- Git简介(转载)
转自:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137396284551 ...
- (斯特林公式)51NOD 1058 N的阶乘的长度
输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3. Input 输入N(1 <= N <= 10^6) Output 输出N的阶乘的长度 Input示例 6 Out ...
- 'latin-1' codec can't encode characters in position解决字符问题
当遇到这样的报错时,原因是: pymysql库在处理mysql语句时,默认的编码方式是'latin-1',这种编码方式能识别的字符是有限的 解决办法:找到\site-packages\pymysql\ ...
- Get 和 Post 使用篇(1)
1.Post 请求发送方式 实例: const string sResponseEncoding = "gb2312"; //测试文本信息 string postText = &q ...
- 使用Oracle实现的MyBatis分页查询效果
1.mybatis.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configur ...
- selenium + python实现截图并且保存图片
webdriver的截图功能十分强悍,无论页面多长,webdriver都能比较完美的截到完整的页面. python代码: # -*- coding: utf-8 -*-from selenium im ...
- Python---查看安装路径
python是解释型脚本语言,在执行时,逐句解释执行,不需要进行预编译.但需要有自身的Python解释器. 所以在执行Python代码时,需要指定python解释器. 指定解释器方法: 在文件开头添加 ...
- Spring Boot (27) actuator服务监控与管理
actuaotr是spring boot项目中非常强大的一个功能,有助于对应用程序进行监控和管理,通过restful api请求来监管.审计.收集应用的运行情况,针对微服务而言它是必不可少的一个环节. ...
- string与int的相互转换C++(转)
string与int之间的相互转换C++(转) #include<iostream> #include<string> #include<sstream> usin ...