Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴Java代码。排序后贪心(带注释)import java.util.*; public class Main{ public static void main(String[] args) { Scanner s = new Scanner(System.in); // 输入数据后按a升序排序 int K = s.nextInt(); block blocks[] = new block[K]; for(int i = 0; i < K; i ++){ blocks[i] = new block(); blocks[i].h = s.nextInt(); blocks[i].a = s.nextInt(); blocks[i].c = s.nextInt(); } Arrays.sort(blocks); // 初始化布尔数组。值为true代表可以到达该高度。 boolean ok[] = new boolean[40001]; for(int i = 1; i < 40001; i++) ok[i] = false; ok[0] = true; // 这里我用动态数组来保存可以到达的所有高度 // 想要稍微降低空间复杂度也可以选择用一维数组 ArrayList<Integer> al = new ArrayList<Integer>(); al.add(0); // 循环所有类型的block for(int i = 0; i < K; i ++){ int len = al.size(); // 循环用该block之前的所有block能达到的所有高度 for(int j = 0; j < len; j++){ int index = al.get(j); // 循环该block的数量 for(int n = 1; n <= blocks[i].c; n++){ int h = index + n*blocks[i].h; // 剪枝条件*2 if(h > blocks[i].a) break; if(ok[h]) break; // 如果到达了新的高度,则加入al ok[h] = true; al.add(h); } } } // 最后输出最大的高度 for(int i = 40000; i >= 0; i--){ if(ok[i]){ System.out.println(i); break; } } } } class block implements Comparable<block>{ int h, a, c; public int compareTo(block o) { return this.a - o.a; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator