import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Comparator; public class boh { int fightNumber; long power; int positiveFightNumber = 0; int neutralFightNumber = 0; int negativeFightNumber = 0; Fight positiveFights[] = new Fight[100000]; Fight neutralFights[] = new Fight[100000]; Fight negativeFights[] = new Fight[100000]; boolean win = false; void process() { sortFights(); for (int i = 0; i < positiveFightNumber; i++) { int powerLoss = positiveFights[i].getLoss(); power -= powerLoss; if (power <= 0) { return; } int powerWin = positiveFights[i].getWin(); power += powerWin; } for (int i = 0; i < neutralFightNumber; i++) { int powerLoss = neutralFights[i].getLoss(); power -= powerLoss; if (power <= 0) { return; } int powerWin = neutralFights[i].getWin(); power += powerWin; } for (int i = 0; i < negativeFightNumber; i++) { preprocessNegativeFight(i); int powerLoss = negativeFights[i].getLoss(); int powerWin = negativeFights[i].getWin(); power -= powerLoss; if (power <= 0) { return; } power += powerWin; } win = true; } void preprocessNegativeFight(int i) { int powerLoss = negativeFights[i].getLoss(); int powerWin = negativeFights[i].getWin(); if (i+1 < negativeFightNumber) { int nextPowerLoss = negativeFights[i+1].getLoss(); if(power - powerLoss + powerWin <= nextPowerLoss) { swap(negativeFights, i, i+1); } } } void swap(Fight[] a, int i, int j) { Fight tmp = a[i]; a[i] = a[j]; a[j] = tmp; } void sortFights() { if (positiveFightNumber > 0) { Arrays.sort(positiveFights, 0, positiveFightNumber, new Comparator<Fight>() { @Override public int compare(Fight o1, Fight o2) { if (o1.getLoss() < o2.getLoss()) return -1; if (o1.getLoss() > o2.getLoss()) return 1; return 0; } }); } if (negativeFightNumber > 0) { Arrays.sort(negativeFights, 0, negativeFightNumber, new Comparator<Fight>() { @Override public int compare(Fight o1, Fight o2) { if (o1.getLoss() < o2.getLoss()) return 1; if (o1.getLoss() > o2.getLoss()) return -1; if (o1.getWin() < o2.getWin()) return 1; if (o1.getWin() > o2.getWin()) return -1; return 0; } }); } } public static void main(String[] args) { new boh(); } public boh() { read(); process(); print(); } void read() { InputStream is = System.in; // if (Boolean.getBoolean("test")) { // is = this.getClass().getResourceAsStream("boh.in"); // } BufferedInputStream bis = new BufferedInputStream(is); fightNumber = readInt(bis); power = readInt(bis); for (int i = 0; i < fightNumber; i++) { int powerLoss = readInt(bis); int powerWin = readInt(bis); Fight fight = new Fight(powerLoss, powerWin, i); if (powerLoss < powerWin) { positiveFights[positiveFightNumber] = fight; positiveFightNumber++; } if (powerLoss == powerWin) { neutralFights[neutralFightNumber] = fight; neutralFightNumber++; } if (powerLoss > powerWin) { negativeFights[negativeFightNumber] = fight; negativeFightNumber++; } } } private int readInt(InputStream in) { int ret = 0; boolean dig = false; try { for (int c = 0; (c = in.read()) != -1;) { if (c >= '0' && c <= '9') { dig = true; ret = ret * 10 + c - '0'; } else if (dig) { break; } } } catch (IOException e) { throw new RuntimeException(e); } return ret; } void print() { if (win) { System.out.println("TAK"); for (int i = 0; i < positiveFightNumber; i++) { System.out.print(positiveFights[i].getPosition()+1); System.out.print(" "); } for (int i = 0; i < neutralFightNumber; i++) { System.out.print(neutralFights[i].getPosition()+1); System.out.print(" "); } for (int i = 0; i < negativeFightNumber; i++) { System.out.print(negativeFights[i].getPosition()+1); System.out.print(" "); } } else { System.out.println("NIE"); } } static class Fight { private final int loss; private final int win; private final int position; public Fight(int loss, int win, int position) { super(); this.loss = loss; this.win = win; this.position = position; } public int getLoss() { return loss; } public int getWin() { return win; } public int getPosition() { return position; } @Override public String toString() { return "Fight [loss=" + loss + ", win=" + win + ", position=" + position + "]"; } } }
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 | import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Comparator; public class boh { int fightNumber; long power; int positiveFightNumber = 0; int neutralFightNumber = 0; int negativeFightNumber = 0; Fight positiveFights[] = new Fight[100000]; Fight neutralFights[] = new Fight[100000]; Fight negativeFights[] = new Fight[100000]; boolean win = false; void process() { sortFights(); for (int i = 0; i < positiveFightNumber; i++) { int powerLoss = positiveFights[i].getLoss(); power -= powerLoss; if (power <= 0) { return; } int powerWin = positiveFights[i].getWin(); power += powerWin; } for (int i = 0; i < neutralFightNumber; i++) { int powerLoss = neutralFights[i].getLoss(); power -= powerLoss; if (power <= 0) { return; } int powerWin = neutralFights[i].getWin(); power += powerWin; } for (int i = 0; i < negativeFightNumber; i++) { preprocessNegativeFight(i); int powerLoss = negativeFights[i].getLoss(); int powerWin = negativeFights[i].getWin(); power -= powerLoss; if (power <= 0) { return; } power += powerWin; } win = true; } void preprocessNegativeFight(int i) { int powerLoss = negativeFights[i].getLoss(); int powerWin = negativeFights[i].getWin(); if (i+1 < negativeFightNumber) { int nextPowerLoss = negativeFights[i+1].getLoss(); if(power - powerLoss + powerWin <= nextPowerLoss) { swap(negativeFights, i, i+1); } } } void swap(Fight[] a, int i, int j) { Fight tmp = a[i]; a[i] = a[j]; a[j] = tmp; } void sortFights() { if (positiveFightNumber > 0) { Arrays.sort(positiveFights, 0, positiveFightNumber, new Comparator<Fight>() { @Override public int compare(Fight o1, Fight o2) { if (o1.getLoss() < o2.getLoss()) return -1; if (o1.getLoss() > o2.getLoss()) return 1; return 0; } }); } if (negativeFightNumber > 0) { Arrays.sort(negativeFights, 0, negativeFightNumber, new Comparator<Fight>() { @Override public int compare(Fight o1, Fight o2) { if (o1.getLoss() < o2.getLoss()) return 1; if (o1.getLoss() > o2.getLoss()) return -1; if (o1.getWin() < o2.getWin()) return 1; if (o1.getWin() > o2.getWin()) return -1; return 0; } }); } } public static void main(String[] args) { new boh(); } public boh() { read(); process(); print(); } void read() { InputStream is = System.in; // if (Boolean.getBoolean("test")) { // is = this.getClass().getResourceAsStream("boh.in"); // } BufferedInputStream bis = new BufferedInputStream(is); fightNumber = readInt(bis); power = readInt(bis); for (int i = 0; i < fightNumber; i++) { int powerLoss = readInt(bis); int powerWin = readInt(bis); Fight fight = new Fight(powerLoss, powerWin, i); if (powerLoss < powerWin) { positiveFights[positiveFightNumber] = fight; positiveFightNumber++; } if (powerLoss == powerWin) { neutralFights[neutralFightNumber] = fight; neutralFightNumber++; } if (powerLoss > powerWin) { negativeFights[negativeFightNumber] = fight; negativeFightNumber++; } } } private int readInt(InputStream in) { int ret = 0; boolean dig = false; try { for (int c = 0; (c = in.read()) != -1;) { if (c >= '0' && c <= '9') { dig = true; ret = ret * 10 + c - '0'; } else if (dig) { break; } } } catch (IOException e) { throw new RuntimeException(e); } return ret; } void print() { if (win) { System.out.println("TAK"); for (int i = 0; i < positiveFightNumber; i++) { System.out.print(positiveFights[i].getPosition()+1); System.out.print(" "); } for (int i = 0; i < neutralFightNumber; i++) { System.out.print(neutralFights[i].getPosition()+1); System.out.print(" "); } for (int i = 0; i < negativeFightNumber; i++) { System.out.print(negativeFights[i].getPosition()+1); System.out.print(" "); } } else { System.out.println("NIE"); } } static class Fight { private final int loss; private final int win; private final int position; public Fight(int loss, int win, int position) { super(); this.loss = loss; this.win = win; this.position = position; } public int getLoss() { return loss; } public int getWin() { return win; } public int getPosition() { return position; } @Override public String toString() { return "Fight [loss=" + loss + ", win=" + win + ", position=" + position + "]"; } } } |