【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; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
随机推荐
- render same axis
// 当前渲染相机的参数 QGlobalCamera* curRenderCamera = _getWorld()->getMainCam(); const Matrix4& ...
- 对数组名取地址&a和 数组首地址a
#include <iostream> using namespace std; ] = {,,,,}; int main() { cout<<a<<" ...
- 393 UTF-8 Validation UTF-8 编码验证
详见:https://leetcode.com/problems/utf-8-validation/description/ C++: class Solution { public: bool va ...
- 转】 Spark SQL UDF使用
原博文出自于: http://blog.csdn.net/oopsoom/article/details/39401391 感谢! Spark1.1推出了Uer Define Function功能,用 ...
- YumRepo Error: All mirror URLs are not using ftp, http[s] or file
有台机器使用Yum的时候,报错如下: YumRepo Error: All mirror URLs are not using ftp, http[s] or file. Eg. $releaseve ...
- 数据采集框架Gobblin简介
问题导读: Gobblin的架构设计是怎样的? Gobblin拥有哪些组建,如何实现可扩展? Gobblin采集执行流程的过程? 前面我们介绍Gobblin是用来整合各种数据源的通用型ETL框架,在某 ...
- C#知识点-GDI绘图
一.开发环境 编译器:VS2013 .Net版本:4.5 二.开发过程 1.画一条直线 private void btnDrawLine_Click(object sender, EventArgs ...
- SAS进阶《深入分析SAS》之数据汇总和展现
SAS进阶<深入分析SAS>之数据汇总和展现 1. 通过Print过程制作报表 proc print <data=数据集>; run; 选项: obs=修改观测序号列标签 no ...
- 跨站脚本攻击XXS(Cross Site Scripting)修复方案
今天突然发现,网站被主页莫名奇妙的出现了陌生的广告. 通过排查发现是跨站脚本攻击XXS(Cross Site Scripting).以下为解决方案. 漏洞类型: Cross Site Scriptin ...
- JS高级——监听浏览器的返回事件
https://www.cnblogs.com/Easty/p/7820055.html https://www.cnblogs.com/zhengyan/p/6912526.html http:// ...