結果
問題 | No.417 チューリップバブル |
ユーザー | 37zigen |
提出日時 | 2016-08-11 16:39:35 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,254 bytes |
コンパイル時間 | 4,703 ms |
コンパイル使用メモリ | 89,228 KB |
実行使用メモリ | 74,916 KB |
最終ジャッジ日時 | 2024-11-07 17:46:08 |
合計ジャッジ時間 | 19,326 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 57 ms
56,628 KB |
testcase_01 | AC | 57 ms
49,900 KB |
testcase_02 | AC | 58 ms
49,856 KB |
testcase_03 | AC | 58 ms
50,204 KB |
testcase_04 | AC | 58 ms
50,156 KB |
testcase_05 | AC | 58 ms
49,892 KB |
testcase_06 | AC | 58 ms
50,192 KB |
testcase_07 | AC | 58 ms
50,132 KB |
testcase_08 | AC | 60 ms
49,832 KB |
testcase_09 | AC | 60 ms
50,128 KB |
testcase_10 | AC | 62 ms
50,280 KB |
testcase_11 | AC | 63 ms
50,252 KB |
testcase_12 | AC | 64 ms
50,312 KB |
testcase_13 | WA | - |
testcase_14 | AC | 91 ms
51,560 KB |
testcase_15 | AC | 76 ms
50,844 KB |
testcase_16 | AC | 75 ms
50,648 KB |
testcase_17 | TLE | - |
testcase_18 | AC | 233 ms
58,052 KB |
testcase_19 | AC | 236 ms
58,120 KB |
testcase_20 | AC | 398 ms
56,472 KB |
testcase_21 | AC | 240 ms
57,304 KB |
testcase_22 | AC | 293 ms
58,504 KB |
testcase_23 | AC | 192 ms
57,280 KB |
testcase_24 | AC | 60 ms
50,156 KB |
testcase_25 | AC | 572 ms
60,552 KB |
testcase_26 | AC | 97 ms
51,260 KB |
testcase_27 | AC | 175 ms
55,940 KB |
testcase_28 | AC | 1,812 ms
62,492 KB |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | AC | 274 ms
56,620 KB |
testcase_32 | AC | 58 ms
50,216 KB |
testcase_33 | AC | 58 ms
50,212 KB |
testcase_34 | WA | - |
testcase_35 | TLE | - |
testcase_36 | TLE | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
ソースコード
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; public class チューリップバブル { static InputStream is; static PrintWriter out; static String INPUT = ""; ArrayList<Pair> opt = new ArrayList<>();// 単位時間あたりにゲットできる最大チューリップ数 static long total_time; class Pair { long u; int t; Pair(long u, int t) { this.u = u; this.t = t; } } long opt_sol(ArrayList<Pair> opt, long l) { int min = 0; int max = opt.size() - 1; while (max - min > 1) { int mid = (min + max) / 2; if (opt.get(mid).t < l) { min = mid; } else { max = mid; } } return opt.get(max).u; } void solver() { int n = ni();// 村の数 int m = ni();// 残り時間 total_time = m; long[] u = new long[n]; for (int i = 0; i < n; i++) { u[i] = nl();// 税収 } ArrayList<Edge>[] e = new ArrayList[n]; for (int i = 0; i < n; i++) { e[i] = new ArrayList<>(); } for (int i = 0; i < n - 1; i++) { int a = ni(); int b = ni(); int c = ni(); e[a].add(new Edge(c, a, b)); e[b].add(new Edge(c, b, a)); opt.add(new Pair(u[i], c)); } opt.sort(new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return Double.compare(((double) p1.u) / ((double) p1.t), ((double) p2.u) / ((double) p2.t)); } }); for (int i = 1; i < opt.size(); i++) { opt.get(i).u += opt.get(i - 1).u; opt.get(i).t += opt.get(i - 1).t; } ArrayList<State> list = dfs(-1, 0, e, u, m, 0, 0); list.sort(new Comparator<State>() { @Override public int compare(State s1, State s2) { return -Long.compare(s1.u, s2.u); } }); out.println(list.get(0).u); } ArrayList<State> dfs(int p, int cur, ArrayList<Edge>[] e, long[] u, int m, int depth, long total) { ArrayList<ArrayList<State>> tmpList = new ArrayList<>(); for (Edge v : e[cur]) { if (v.to != p) { ArrayList<State> d = dfs(cur, v.to, e, u, m - 2 * v.time, depth + 1, u[cur] + total); ArrayList<State> tmpList2 = new ArrayList<>(); for (State tmp : d) { tmp.t += 2 * v.time; tmp.u += u[cur]; if (m >= tmp.t) { tmpList2.add(tmp); } } tmpList.add(tmpList2); } } ArrayList<State> ret = new ArrayList<>(); for (ArrayList<State> lst : tmpList) { int n = ret.size(); for (State s : lst) { for (int j = 0; j < n; j++) { State tmp = new チューリップバブル().new State(s.u + ret.get(j).u - u[cur], s.t + ret.get(j).t); if (m >= tmp.t) { ret.add(tmp); } } } ret.addAll(lst); ret.sort(new Comparator<State>() { @Override public int compare(State s1, State s2) { return Long.compare(s1.t, s2.t); } }); for (int i = 0; i < ret.size() - 1; i++) { while (i + 1 < ret.size() && ret.get(i).u > ret.get(i + 1).u) { ret.remove(i + 1); } } } for (int i = 0; i < ret.size() - 1; i++) { if (ret.size() >= 2 && ret.get(0).u + opt_sol(opt,total_time - ret.get(0).t) < ret.get(ret.size() - 1).u + total) { ret.remove(0); } } ret.add(new チューリップバブル().new State(u[cur], 0)); return ret; } static class Edge { int time; int from; int to; Edge(int time, int from, int to) { this.time = time; this.from = from; this.to = to; } } class State { long u; int t; State(long u, int t) { this.u = u; this.t = t; } } public static void main(String[] args) throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long t = System.currentTimeMillis(); new チューリップバブル().solver(); System.err.println(System.currentTimeMillis() - t); out.flush(); } static long nl() { try { long num = 0; boolean minus = false; while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-')) ; if (num == '-') { num = 0; minus = true; } else { num -= '0'; } while (true) { int b = is.read(); if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } } } catch (IOException e) { } return -1; } static char nc() { try { int b = skip(); if (b == -1) return 0; return (char) b; } catch (IOException e) { } return 0; } static double nd() { try { return Double.parseDouble(ns()); } catch (Exception e) { } return 0; } static String ns() { try { int b = skip(); StringBuilder sb = new StringBuilder(); if (b == -1) return ""; sb.append((char) b); while (true) { b = is.read(); if (b == -1) return sb.toString(); if (b <= ' ') return sb.toString(); sb.append((char) b); } } catch (IOException e) { } return ""; } public static char[] ns(int n) { char[] buf = new char[n]; try { int b = skip(), p = 0; if (b == -1) return null; buf[p++] = (char) b; while (p < n) { b = is.read(); if (b == -1 || b <= ' ') break; buf[p++] = (char) b; } return Arrays.copyOf(buf, p); } catch (IOException e) { } return null; } public static byte[] nse(int n) { byte[] buf = new byte[n]; try { int b = skip(); if (b == -1) return null; is.read(buf); return buf; } catch (IOException e) { } return null; } static int skip() throws IOException { int b; while ((b = is.read()) != -1 && !(b >= 33 && b <= 126)) ; return b; } static boolean eof() { try { is.mark(1000); int b = skip(); is.reset(); return b == -1; } catch (IOException e) { return true; } } static int ni() { try { int num = 0; boolean minus = false; while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-')) ; if (num == '-') { num = 0; minus = true; } else { num -= '0'; } while (true) { int b = is.read(); if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } } } catch (IOException e) { } return -1; } }