import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.StringTokenizer; public class boh { public static class Combat { private int loss; private int gain; private int diff; private int index; public Combat(int loss, int gain, int index) { this.loss = loss; this.gain = gain; this.diff = gain - loss; this.index = index; } public int getLoss() { return loss; } public int getGain() { return gain; } public int getDiff() { return diff; } public int getIndex() { return index; } @Override public String toString() { return "[l=" + loss + ", g=" + gain + ", d=" + diff + "]"; } } public static class CombatLossComparatorAsc implements Comparator<Combat> { @Override public int compare(Combat arg0, Combat arg1) { return Integer.compare(arg0.getLoss(), arg1.getLoss()); } } public static class CombatGainComparatorDesc implements Comparator<Combat> { @Override public int compare(Combat arg0, Combat arg1) { return -Integer.compare(arg0.getGain(), arg1.getGain()); } } public static void main(String[] args) { try{ BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(buffReader.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); long z = Long.parseLong(tokenizer.nextToken()); ArrayList<Combat> negativeCombat = new ArrayList<Combat>(n); ArrayList<Combat> positiveCombat = new ArrayList<Combat>(n); for(int i=0; i<n; i++){ tokenizer = new StringTokenizer(buffReader.readLine()); Combat combat = new Combat(Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()), i+1); if(combat.getDiff() < 0) negativeCombat.add(combat); else positiveCombat.add(combat); } Collections.sort(negativeCombat, new CombatLossComparatorAsc()); Collections.sort(negativeCombat, new CombatGainComparatorDesc()); Collections.sort(positiveCombat, new CombatGainComparatorDesc()); Collections.sort(positiveCombat, new CombatLossComparatorAsc()); boolean allKill = true; Iterator<Combat> conbatIterator = positiveCombat.iterator(); while(conbatIterator.hasNext() && allKill){ Combat combat = conbatIterator.next(); z -= combat.getLoss(); if(z <= 0) allKill = false; z += combat.getGain(); } conbatIterator = negativeCombat.iterator(); while(conbatIterator.hasNext() && allKill){ Combat combat = conbatIterator.next(); z -= combat.getLoss(); if(z <= 0) allKill = false; z += combat.getGain(); } if(allKill){ System.out.println("TAK"); for(Combat combat : positiveCombat) System.out.print(combat.getIndex() + " "); for(Combat combat : negativeCombat) System.out.print(combat.getIndex() + " "); System.out.println(); }else{ System.out.println("NIE"); } }catch(Exception e){ e.printStackTrace(); } } }
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 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.StringTokenizer; public class boh { public static class Combat { private int loss; private int gain; private int diff; private int index; public Combat(int loss, int gain, int index) { this.loss = loss; this.gain = gain; this.diff = gain - loss; this.index = index; } public int getLoss() { return loss; } public int getGain() { return gain; } public int getDiff() { return diff; } public int getIndex() { return index; } @Override public String toString() { return "[l=" + loss + ", g=" + gain + ", d=" + diff + "]"; } } public static class CombatLossComparatorAsc implements Comparator<Combat> { @Override public int compare(Combat arg0, Combat arg1) { return Integer.compare(arg0.getLoss(), arg1.getLoss()); } } public static class CombatGainComparatorDesc implements Comparator<Combat> { @Override public int compare(Combat arg0, Combat arg1) { return -Integer.compare(arg0.getGain(), arg1.getGain()); } } public static void main(String[] args) { try{ BufferedReader buffReader = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(buffReader.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); long z = Long.parseLong(tokenizer.nextToken()); ArrayList<Combat> negativeCombat = new ArrayList<Combat>(n); ArrayList<Combat> positiveCombat = new ArrayList<Combat>(n); for(int i=0; i<n; i++){ tokenizer = new StringTokenizer(buffReader.readLine()); Combat combat = new Combat(Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()), i+1); if(combat.getDiff() < 0) negativeCombat.add(combat); else positiveCombat.add(combat); } Collections.sort(negativeCombat, new CombatLossComparatorAsc()); Collections.sort(negativeCombat, new CombatGainComparatorDesc()); Collections.sort(positiveCombat, new CombatGainComparatorDesc()); Collections.sort(positiveCombat, new CombatLossComparatorAsc()); boolean allKill = true; Iterator<Combat> conbatIterator = positiveCombat.iterator(); while(conbatIterator.hasNext() && allKill){ Combat combat = conbatIterator.next(); z -= combat.getLoss(); if(z <= 0) allKill = false; z += combat.getGain(); } conbatIterator = negativeCombat.iterator(); while(conbatIterator.hasNext() && allKill){ Combat combat = conbatIterator.next(); z -= combat.getLoss(); if(z <= 0) allKill = false; z += combat.getGain(); } if(allKill){ System.out.println("TAK"); for(Combat combat : positiveCombat) System.out.print(combat.getIndex() + " "); for(Combat combat : negativeCombat) System.out.print(combat.getIndex() + " "); System.out.println(); }else{ System.out.println("NIE"); } }catch(Exception e){ e.printStackTrace(); } } } |