【Codeforces 339C】Xenia and Weights
生活随笔
收集整理的這篇文章主要介紹了
【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 herenew 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());}} }轉載于:https://www.cnblogs.com/AWCXV/p/10505641.html
總結
以上是生活随笔為你收集整理的【Codeforces 339C】Xenia and Weights的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NumPy 数学函数
- 下一篇: injection