【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; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- VIDIOC_S_INPUT 作用 (转载)
转载:http://blog.csdn.net/kickxxx/article/details/7088658 G_INPUT和S_INPUT用来查询和选则当前的input 一个video设备节点可能 ...
- Rails 确认params的统一方法
创建: 2017/11/06 Gemfile ### デバッグ出力の整形 gem 'awesome_print', :group => [:development, :test] a ...
- bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序【置换群】
至今都不知道置换群是个什么东西--题解说什么就是什么.jpg 以下来自hzwer:http://hzwer.com/3905.html #include<iostream> #includ ...
- ACM_N皇后问题
N皇后问题 Time Limit: 2000/1000ms (Java/Others) Problem Description: 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不 ...
- python2行代码调用程序
import win32api win32api.ShellExecute(0, 'open', r'C:\Users\TOPFEEL\AppData\Local\Postman\app-5.5.0\ ...
- [ TJOI 2012 ] 防御
\(\\\) Description 有 \(n\) 人,第 \(i\) 个人有一个护甲值 \(a_i\). 有 \(m\) 次操作,分为以下两种: \(A\ l\ r\ x\) 对编号在 \([l, ...
- IPython、Notebook、qtconsole使用教程
IPython.Notebook.qtconsole使用教程 上一篇为Python,IPython,qtconsole,Notebook,Jupyter快速安装教程 1. 使用IPython 自动补全 ...
- Selenium Grid操作使用指南
一.实现串行多浏览器执行脚本1.启动selenium-server-standalonejava -jar selenium-server-standalone-2.44.0.jar2.脚本代码 fr ...
- ci框架中model简单的mysql操作
<?php class SingerModel extends CI_Model { function SingerModel() { //会将数据库对象赋值给CI_Controller的db属 ...
- POJ_1050_(dp)
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 48232 Accepted: 25534 Desc ...