import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class boh { public static void main(String[] args) throws Exception { BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); String[] firstInput = reader.readLine().split(" "); int numberOfMonsters=Integer.parseInt(firstInput[0]); double healthPoints=Double.parseDouble(firstInput[1]); List<double[]> monsters=new ArrayList<double[]>(numberOfMonsters); List<Integer> winingMonsters=new ArrayList<Integer>(numberOfMonsters); int monsterIndex=1; double hitSum=0; double healthSum=0; while(numberOfMonsters-->0){ String[] stringMonster = reader.readLine().split(" "); double hitPoints=Long.parseLong(stringMonster[0]); hitSum+=hitPoints; double healthPotion=Long.parseLong(stringMonster[1]); healthSum+=healthPotion; double priority=(healthPotion-(hitPoints+1.))/(hitPoints+1.); monsters.add(new double[]{hitPoints,healthPotion,priority,monsterIndex++}); } Collections.sort(monsters,new Comparator<double[]>() { @Override public int compare(double[] o1, double[] o2) { return o1[2]-o2[2]<0.?1:-1; } }); boolean canWin=((healthSum+healthPoints)-hitSum)>0; while(canWin&&monsters.size()>0){ boolean hadMove=false; for(int i = 0; i<monsters.size();i++){ double[] monster=monsters.get(i); if(monster[0]<healthPoints){ monsters.remove(i); healthPoints=healthPoints-monster[0]+monster[1]; winingMonsters.add((int)monster[3]); hadMove=true; break; } } if(hadMove==false){ canWin=false; } } if(canWin){ System.out.println("TAK"); System.out.println(strJoin(winingMonsters.toArray()," ")); } else{ System.out.println("NIE"); } } public static String strJoin(Object[] aArr, String sSep) { StringBuilder sbStr = new StringBuilder(); for (int i = 0, il = aArr.length; i < il; i++) { if (i > 0) sbStr.append(sSep); sbStr.append(aArr[i]); } return sbStr.toString(); } }
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 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class boh { public static void main(String[] args) throws Exception { BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); String[] firstInput = reader.readLine().split(" "); int numberOfMonsters=Integer.parseInt(firstInput[0]); double healthPoints=Double.parseDouble(firstInput[1]); List<double[]> monsters=new ArrayList<double[]>(numberOfMonsters); List<Integer> winingMonsters=new ArrayList<Integer>(numberOfMonsters); int monsterIndex=1; double hitSum=0; double healthSum=0; while(numberOfMonsters-->0){ String[] stringMonster = reader.readLine().split(" "); double hitPoints=Long.parseLong(stringMonster[0]); hitSum+=hitPoints; double healthPotion=Long.parseLong(stringMonster[1]); healthSum+=healthPotion; double priority=(healthPotion-(hitPoints+1.))/(hitPoints+1.); monsters.add(new double[]{hitPoints,healthPotion,priority,monsterIndex++}); } Collections.sort(monsters,new Comparator<double[]>() { @Override public int compare(double[] o1, double[] o2) { return o1[2]-o2[2]<0.?1:-1; } }); boolean canWin=((healthSum+healthPoints)-hitSum)>0; while(canWin&&monsters.size()>0){ boolean hadMove=false; for(int i = 0; i<monsters.size();i++){ double[] monster=monsters.get(i); if(monster[0]<healthPoints){ monsters.remove(i); healthPoints=healthPoints-monster[0]+monster[1]; winingMonsters.add((int)monster[3]); hadMove=true; break; } } if(hadMove==false){ canWin=false; } } if(canWin){ System.out.println("TAK"); System.out.println(strJoin(winingMonsters.toArray()," ")); } else{ System.out.println("NIE"); } } public static String strJoin(Object[] aArr, String sSep) { StringBuilder sbStr = new StringBuilder(); for (int i = 0, il = aArr.length; i < il; i++) { if (i > 0) sbStr.append(sSep); sbStr.append(aArr[i]); } return sbStr.toString(); } } |