/** * @author Tadeusz Faber (SI-Consulting SA). * * Potyczki Algorytmiczne 2014. * * 2014-05-13 Runda 2B. Bohater. * * Pamiec: 128MB. * Czas: ?s. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.Arrays; import java.util.Comparator; class Monster { private int id; private int injury; private int elixir; private int rate; public Monster(int id, int injury, int elixir) { this.id = id; this.injury = injury; this.elixir = elixir; rate = elixir - injury; // -100 000..100 000 if (rate < 0) { rate = 500000 - injury; // strata, wedlug malejacych obrazen } else if (rate > 0) { rate = injury; // zysk, wedlug rosnacych obrazen } else { rate = 300000 - injury; // bez zmian, wedlug malejacych obrazen } } public int getID() { return id; } public int getInjury() { return injury; } public int getElixir() { return elixir; } public int getBalane() { return elixir - injury; } public int getRate() { return rate; } } class MonsterComparator implements Comparator<Monster> { @Override public int compare(Monster m1, Monster m2) { if (m2 == null) return -1; // m1 < m2 if (m1.getRate() > m2.getRate()) return 1; // m1 > m2 else if (m1.getRate() < m2.getRate()) return -1; // m1 < m2 else return 0; } } public final class boh { private static Reader in = null; /** * Zwroc kolejna liczbe calkowita ze strumienia in * * @return wczytana liczba */ private static int nextInteger() { int r = 0; boolean minus = false; char c; try { do { c = (char) in.read(); } while (c == ' ' || c == '\n' || c == '\r'); if (c == '-') { minus = true; c = (char) in.read(); } do { r = r * 10 + (int) c - 48; c = (char) in.read(); } while (c != ' ' && c != '\n' && c != '\r' && c != (char) -1); } catch (Exception ex) { ex.printStackTrace(); } if (minus) { return -r; } else { return r; } } /** * @param args */ public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); int n = nextInteger(); // 1..100 000 long z = nextInteger(); // 1..100 000 boolean all = true; Monster[] monsters = new Monster[n]; long health = z; for (int i = 0; i < n; i++) { monsters[i] = new Monster(i + 1, nextInteger(), nextInteger()); health += monsters[i].getBalane(); } if (health <= 0) { all = false; } else { MonsterComparator comparator = new MonsterComparator(); Arrays.sort(monsters, comparator); health = z; for (int i = 0; i < n && all; i++) { Monster m = monsters[i]; if (health - m.getInjury() <= 0) { all = false; } else { health += m.getBalane(); } } } if (all) { System.out.println("TAK"); for (int i = 0; i < n && all; i++) { System.out.print(monsters[i].getID() + " "); } System.out.println(); } else { System.out.println("NIE"); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | /** * @author Tadeusz Faber (SI-Consulting SA). * * Potyczki Algorytmiczne 2014. * * 2014-05-13 Runda 2B. Bohater. * * Pamiec: 128MB. * Czas: ?s. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.Arrays; import java.util.Comparator; class Monster { private int id; private int injury; private int elixir; private int rate; public Monster(int id, int injury, int elixir) { this.id = id; this.injury = injury; this.elixir = elixir; rate = elixir - injury; // -100 000..100 000 if (rate < 0) { rate = 500000 - injury; // strata, wedlug malejacych obrazen } else if (rate > 0) { rate = injury; // zysk, wedlug rosnacych obrazen } else { rate = 300000 - injury; // bez zmian, wedlug malejacych obrazen } } public int getID() { return id; } public int getInjury() { return injury; } public int getElixir() { return elixir; } public int getBalane() { return elixir - injury; } public int getRate() { return rate; } } class MonsterComparator implements Comparator<Monster> { @Override public int compare(Monster m1, Monster m2) { if (m2 == null) return -1; // m1 < m2 if (m1.getRate() > m2.getRate()) return 1; // m1 > m2 else if (m1.getRate() < m2.getRate()) return -1; // m1 < m2 else return 0; } } public final class boh { private static Reader in = null; /** * Zwroc kolejna liczbe calkowita ze strumienia in * * @return wczytana liczba */ private static int nextInteger() { int r = 0; boolean minus = false; char c; try { do { c = (char) in.read(); } while (c == ' ' || c == '\n' || c == '\r'); if (c == '-') { minus = true; c = (char) in.read(); } do { r = r * 10 + (int) c - 48; c = (char) in.read(); } while (c != ' ' && c != '\n' && c != '\r' && c != (char) -1); } catch (Exception ex) { ex.printStackTrace(); } if (minus) { return -r; } else { return r; } } /** * @param args */ public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); int n = nextInteger(); // 1..100 000 long z = nextInteger(); // 1..100 000 boolean all = true; Monster[] monsters = new Monster[n]; long health = z; for (int i = 0; i < n; i++) { monsters[i] = new Monster(i + 1, nextInteger(), nextInteger()); health += monsters[i].getBalane(); } if (health <= 0) { all = false; } else { MonsterComparator comparator = new MonsterComparator(); Arrays.sort(monsters, comparator); health = z; for (int i = 0; i < n && all; i++) { Monster m = monsters[i]; if (health - m.getInjury() <= 0) { all = false; } else { health += m.getBalane(); } } } if (all) { System.out.println("TAK"); for (int i = 0; i < n && all; i++) { System.out.print(monsters[i].getID() + " "); } System.out.println(); } else { System.out.println("NIE"); } } } |