【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; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- http报文和协议首部
http报文和协议首部 http报文 3>报文格式 request 报文 <method> <request-URL> <version> <heade ...
- PCB 帆软FineReport安装,布署,配置
公司使用帆软FineReport做为报表平台工具也有一年多时间,而FineReport报表平台与Tomcat Web应用服务是站队在java阵营里,因为相信拥抱微软;.net未来发展会越来越好,所以对 ...
- javascript实现页面跳转
这里指的页面跳转是将浏览器标签页转到新的网址. 只需要使用 window.location.href="url" 就行了 示例 <!DOCTYPE html> < ...
- [C和指针] 6-指针
6.1 内存和地址 我们可以把计算机的内存看作是一条长街上的一排房屋,每座房子都可以容纳数据,并通过一个房号来标识. 这个比喻颇为有用,但也存在局限性.计算机的内存由以亿万计的位(bit)组成,每个位 ...
- C# 的占位符
static void Main(string[] args) { Console.WriteLine("A:{0},a:{1}",65,97); Console.ReadLine ...
- 经典矩阵dp寻找递增最大长度
竖向寻找矩阵最大递增元素长度,因为要求至少一列为递增数列,那么每行求一下最大值就可以作为len[i]:到i行截止的最长的递增数列长度. C. Alyona and Spreadsheet time l ...
- encodeURIComponent的用法
实践出真知,项目中遇到坑,填满后总结:编码不一定需要解码 rsa加密字段(base64位后),通过url?filed=value传输后,总是有+等特殊字符,然后到后端时base64解不开,发现很多空格 ...
- Spring:(三) --常见数据源及声明式事务配置
Spring自带了一组数据访问框架,集成了多种数据访问技术.无论我们是直接通过 JDBC 还是像Hibernate或Mybatis那样的框架实现数据持久化,Spring都可以为我们消除持久化代码中那些 ...
- php循环结构
1.while循环 先判断条件,如果条件成立则执行循环的代码 嵌套循环,需要先把嵌套在内的循环执行完毕再执行外面的循环 While(条件语句){......} //如果()条件成立,执行{}里面的语句 ...
- Python学习日记之中文支持
解决中文输出错误 在开头添加 # -*- coding: utf-8 -*- 即可