• 【Codeforces 349B】Color the Fence


    【链接】 我是链接,点我呀:)
    【题意】

    让你组成一个只由1~9组成的数字 每个数字需要的paint数字给定。 让你组成一个最大的数字,且所有数字的paint的总和不超过v.

    【题解】

    先求出a中的最小值mi 最后的长度显然就是a/mi啦 然后从高位到低位,优先让高位优先选择大的数字就好. (判断这一位能否为i的条件就是,后面的所有位置全都选择mi 看看会不会超过剩余的paint数量就好

    【代码】

    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 L = (int)1e6;
        static class Task{
            
            int v,mi;
            int a[],b[];
            
            public void dfs(int dep,int rest) {
            	if (dep==0) return;
            	for (int i = 9;i >= 1;i--) {
            		if (rest-a[i]-(dep-1)*mi>=0) {
            			b[dep] = i;
            			dfs(dep-1,rest-a[i]);
            			return;
            		}
            	}
            }
            public void solve(InputReader in,PrintWriter out) {
            	a = new int[N+10];
            	b = new int[L+10];
            	v = in.nextInt();
            	for (int i = 1;i <= 9;i++) a[i] = in.nextInt();
            	mi = a[1];
            	for (int i = 1;i <= 9;i++)  mi = Math.min(a[i], mi);
            	int len = v/mi;
            	if (len==0) 
            		out.println(-1);
            	else {
            		dfs(len,v);
            		for (int i = len;i >= 1;i--) out.print(b[i]);
            	}
            }
        }
    
        
    
        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());
            }
        }
    }
    
  • 相关阅读:
    php 计算两个日期相差天数
    Linux系统查找清理磁盘大文件
    虚拟机重启网络服务失败,当查看状态显示错误Failed to start LSB......
    c++简单编写线性表(实验)
    学校的c++程序课程设计(简单的写法 并无太多c++的特色)
    C语言简单实现链栈基本几个功能
    简单用数组模拟顺序栈(c++)
    详细易懂的二叉树遍历(先中后)
    大数加法之C语言函数法(只有正数版)
    有关Java垃圾回收的几个问题
  • 原文地址:https://www.cnblogs.com/AWCXV/p/10396592.html
Copyright © 2020-2023  润新知