import java.io.IOException; import java.io.InputStream; import java.util.Arrays; public class boh { public static void main(String[] args) throws IOException { Timer timer = new Timer(); final InputStream inputStream = System.in; final SimpleNumber simpleNumber = new SimplePositiveInteger(); final SimpleStringParser simpleStringParser = new SimpleStringParser(100000, inputStream); final int monstersCount = simpleStringParser.parseInt(simpleNumber); long lifePoints = simpleStringParser.parseLastInt(simpleNumber, true); StringBuilder answer = new StringBuilder(monstersCount * 4); Monster[] monsters = new Monster[monstersCount]; int lastIndex = monstersCount - 1; for(int i = 0;i < monstersCount;i++) { int strength = simpleStringParser.parseInt(simpleNumber); int health = simpleStringParser.parseLastInt(simpleNumber, i < lastIndex); monsters[i] = new Monster(i + 1, strength, health); } Arrays.sort(monsters); for(int i = 0;i < monstersCount;i++) { Monster monster = monsters[i]; lifePoints -= monster.getStrength(); if(lifePoints < 1L) { System.out.println("NIE"); timer.printTimeInMillis(); return; } lifePoints += monster.getHealth(); answer.append(monster.getId()).append(' '); } System.out.println("TAK"); System.out.println(answer.substring(0, answer.length() - 1)); timer.printTimeInMillis(); } static class Monster implements Comparable<Monster> { private final int id; private final int strength; private final int health; public Monster(int id, int strength, int health) { this.id = id; this.strength = strength; this.health = health; } @Override public int compareTo(Monster monster) { int monsterOutcome = monster.getOutcome(); int outcome = getOutcome(); if(monsterOutcome < 0 && outcome > 0) { return -1; } if(monsterOutcome > 0 && outcome < 0) { return 1; } if(monsterOutcome > 0 && outcome > 0) { return getStrength() - monster.getStrength(); } return monster.getHealth() - getHealth(); } private int getOutcome() { return health - strength; } @Override public String toString() { return "Monster[" + id + ", " + strength + ", " + health + ']'; } public int getId() { return id; } public int getStrength() { return strength; } public int getHealth() { return health; } } static class SimpleStringParser { int index = 0; final byte[] array; int bytesReaded; final InputStream inputStream; public SimpleStringParser(final int arraySize, final InputStream inputStream) throws IOException { array = new byte[arraySize]; this.inputStream = inputStream; bytesReaded = inputStream.read(array); } protected final void nextChunkOfData() throws IOException { bytesReaded = inputStream.read(array); index = 0; } protected final boolean nextChunkOfDataRequired() { return bytesReaded <= index; } public int parseInt(final SimpleNumber simpleNumber) throws IOException { final int integer = simpleNumber.parseInt(this); ++index; return integer; } public int parseLastInt(final SimpleNumber simpleNumber, final boolean hasMore) throws IOException { return simpleNumber.parseLastInt(this, hasMore); } } static abstract class SimpleNumber { //protected static final String LINE_SEPARATOR = "\n"; protected static final String LINE_SEPARATOR = System.getProperty("line.separator"); protected static final char BEGIN_OF_LINE_SEPARATOR = LINE_SEPARATOR.charAt(0); protected static final int ZERO = '0'; protected static final int SPACE = ' '; public int parseInt(SimpleStringParser simpleStringParser) throws IOException {throw new UnsupportedOperationException();} public int parseLastInt(SimpleStringParser simpleStringParser, boolean hasMore) throws IOException {throw new UnsupportedOperationException();} } static class SimplePositiveInteger extends SimpleNumber { @Override public int parseInt(final SimpleStringParser simpleStringParser) throws IOException { if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } if(simpleStringParser.array[simpleStringParser.index] == SPACE) { ++simpleStringParser.index; } int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO; int value; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } while((value = simpleStringParser.array[simpleStringParser.index]) != SPACE) { integer *= 10; integer += value - ZERO; ++simpleStringParser.index; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } } return integer; } @Override public int parseLastInt(final SimpleStringParser simpleStringParser, final boolean hasMore) throws IOException { if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO; int value; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } while((value = simpleStringParser.array[simpleStringParser.index]) != BEGIN_OF_LINE_SEPARATOR) { integer *= 10; integer += value - ZERO; ++simpleStringParser.index; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } } if(hasMore) { simpleStringParser.index += LINE_SEPARATOR.length(); if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); for(int i = 0;i < LINE_SEPARATOR.length();++i) { if(simpleStringParser.array[i] == LINE_SEPARATOR.charAt(i)) { ++simpleStringParser.index; } } } } return integer; } } static public class Timer { private long time; public Timer() { startNextMeasure(); } public void startNextMeasure() { time = System.nanoTime(); } public void printTimeInMillis() { System.err.println(getTitmeInNanos() / 1000000); } public void printTimeInNanos() { System.err.println(getTitmeInNanos()); } public void printTimeInMicros() { System.err.println(getTitmeInNanos() / 1000); } public void printTimeInSeconds() { System.err.println(getTitmeInNanos() / 1000000000); } private long getTitmeInNanos() { return (System.nanoTime() - time); } } }
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | import java.io.IOException; import java.io.InputStream; import java.util.Arrays; public class boh { public static void main(String[] args) throws IOException { Timer timer = new Timer(); final InputStream inputStream = System.in; final SimpleNumber simpleNumber = new SimplePositiveInteger(); final SimpleStringParser simpleStringParser = new SimpleStringParser(100000, inputStream); final int monstersCount = simpleStringParser.parseInt(simpleNumber); long lifePoints = simpleStringParser.parseLastInt(simpleNumber, true); StringBuilder answer = new StringBuilder(monstersCount * 4); Monster[] monsters = new Monster[monstersCount]; int lastIndex = monstersCount - 1; for(int i = 0;i < monstersCount;i++) { int strength = simpleStringParser.parseInt(simpleNumber); int health = simpleStringParser.parseLastInt(simpleNumber, i < lastIndex); monsters[i] = new Monster(i + 1, strength, health); } Arrays.sort(monsters); for(int i = 0;i < monstersCount;i++) { Monster monster = monsters[i]; lifePoints -= monster.getStrength(); if(lifePoints < 1L) { System.out.println("NIE"); timer.printTimeInMillis(); return; } lifePoints += monster.getHealth(); answer.append(monster.getId()).append(' '); } System.out.println("TAK"); System.out.println(answer.substring(0, answer.length() - 1)); timer.printTimeInMillis(); } static class Monster implements Comparable<Monster> { private final int id; private final int strength; private final int health; public Monster(int id, int strength, int health) { this.id = id; this.strength = strength; this.health = health; } @Override public int compareTo(Monster monster) { int monsterOutcome = monster.getOutcome(); int outcome = getOutcome(); if(monsterOutcome < 0 && outcome > 0) { return -1; } if(monsterOutcome > 0 && outcome < 0) { return 1; } if(monsterOutcome > 0 && outcome > 0) { return getStrength() - monster.getStrength(); } return monster.getHealth() - getHealth(); } private int getOutcome() { return health - strength; } @Override public String toString() { return "Monster[" + id + ", " + strength + ", " + health + ']'; } public int getId() { return id; } public int getStrength() { return strength; } public int getHealth() { return health; } } static class SimpleStringParser { int index = 0; final byte[] array; int bytesReaded; final InputStream inputStream; public SimpleStringParser(final int arraySize, final InputStream inputStream) throws IOException { array = new byte[arraySize]; this.inputStream = inputStream; bytesReaded = inputStream.read(array); } protected final void nextChunkOfData() throws IOException { bytesReaded = inputStream.read(array); index = 0; } protected final boolean nextChunkOfDataRequired() { return bytesReaded <= index; } public int parseInt(final SimpleNumber simpleNumber) throws IOException { final int integer = simpleNumber.parseInt(this); ++index; return integer; } public int parseLastInt(final SimpleNumber simpleNumber, final boolean hasMore) throws IOException { return simpleNumber.parseLastInt(this, hasMore); } } static abstract class SimpleNumber { //protected static final String LINE_SEPARATOR = "\n"; protected static final String LINE_SEPARATOR = System.getProperty("line.separator"); protected static final char BEGIN_OF_LINE_SEPARATOR = LINE_SEPARATOR.charAt(0); protected static final int ZERO = '0'; protected static final int SPACE = ' '; public int parseInt(SimpleStringParser simpleStringParser) throws IOException {throw new UnsupportedOperationException();} public int parseLastInt(SimpleStringParser simpleStringParser, boolean hasMore) throws IOException {throw new UnsupportedOperationException();} } static class SimplePositiveInteger extends SimpleNumber { @Override public int parseInt(final SimpleStringParser simpleStringParser) throws IOException { if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } if(simpleStringParser.array[simpleStringParser.index] == SPACE) { ++simpleStringParser.index; } int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO; int value; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } while((value = simpleStringParser.array[simpleStringParser.index]) != SPACE) { integer *= 10; integer += value - ZERO; ++simpleStringParser.index; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } } return integer; } @Override public int parseLastInt(final SimpleStringParser simpleStringParser, final boolean hasMore) throws IOException { if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO; int value; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } while((value = simpleStringParser.array[simpleStringParser.index]) != BEGIN_OF_LINE_SEPARATOR) { integer *= 10; integer += value - ZERO; ++simpleStringParser.index; if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); } } if(hasMore) { simpleStringParser.index += LINE_SEPARATOR.length(); if(simpleStringParser.nextChunkOfDataRequired()) { simpleStringParser.nextChunkOfData(); for(int i = 0;i < LINE_SEPARATOR.length();++i) { if(simpleStringParser.array[i] == LINE_SEPARATOR.charAt(i)) { ++simpleStringParser.index; } } } } return integer; } } static public class Timer { private long time; public Timer() { startNextMeasure(); } public void startNextMeasure() { time = System.nanoTime(); } public void printTimeInMillis() { System.err.println(getTitmeInNanos() / 1000000); } public void printTimeInNanos() { System.err.println(getTitmeInNanos()); } public void printTimeInMicros() { System.err.println(getTitmeInNanos() / 1000); } public void printTimeInSeconds() { System.err.println(getTitmeInNanos() / 1000000000); } private long getTitmeInNanos() { return (System.nanoTime() - time); } } } |