Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.io.FileReader;
  3. import java.io.PrintWriter;
  4. import java.util.*;
  5. import java.lang.Integer;
  6.  
  7. class Worker {
  8.  
  9. private static class Person {
  10. int index, sum;
  11. int startAuthority;
  12. int additionalAuthority;
  13.  
  14. Person(int authority, int additionalAuthority) {
  15. this.startAuthority = authority;
  16. this.additionalAuthority = additionalAuthority;
  17. }
  18.  
  19. Person(int index, int sum, int startAuthority, int additionalAuthority) {
  20. this.index = index;
  21. this.sum = sum;
  22. this.startAuthority = startAuthority;
  23. this.additionalAuthority = additionalAuthority;
  24. }
  25. }
  26.  
  27. private ArrayList<Person> _array;
  28. private ArrayList<Boolean> _used;
  29. private PrintWriter _out;
  30. private int _authority;
  31. private ArrayList<Integer> _answer;
  32.  
  33. Worker(String input, String output) throws FileNotFoundException {
  34. Scanner in = new Scanner(new FileReader(input));
  35. _out = new PrintWriter(output);
  36. int n = in.nextInt();
  37. _authority = in.nextInt();
  38. _array = new ArrayList<Person>(n);
  39. _used = new ArrayList<Boolean>(n);
  40. _answer = new ArrayList<Integer>(n);
  41. for (int i = 0; i < n; i++) {
  42. int authority = in.nextInt();
  43. int additionalAuthority = in.nextInt();
  44. _array.add(new Person(authority, additionalAuthority));
  45. _used.add(i, false);
  46. }
  47. in.close();
  48. }
  49.  
  50. private void bringPositiveAuthority() {
  51. for (int i = 0; i < _array.size(); i++) {
  52. for (int j = 0; j < _array.size(); j++) {
  53. if ((!_used.get(j)) && _array.get(j).startAuthority <= _authority
  54. && _array.get(j).additionalAuthority >= 0) {
  55. _used.set(j, true);
  56. _authority += _array.get(j).additionalAuthority;
  57. _answer.add(j);
  58. }
  59. }
  60. }
  61. }
  62.  
  63. private void bringNegativeAuthority() {
  64. ArrayList<Person> sortedArray = new ArrayList<Person>(_array.size());
  65. for (int i = 0; i < _array.size(); i++) {
  66. sortedArray.add(i,
  67. new Person(i, _array.get(i).startAuthority + _array.get(i).additionalAuthority,
  68. _array.get(i).startAuthority, _array.get(i).additionalAuthority));
  69. }
  70. Collections.sort(sortedArray, new Comparator<Person>() {
  71. @Override
  72. public int compare(Person o1, Person o2) {
  73. return Integer.valueOf(o1.sum).compareTo(o2.sum);
  74. }
  75. });
  76. for (int i = sortedArray.size() - 1; i >= 0; i--) {
  77. if (!_used.get(sortedArray.get(i).index) && _authority >= sortedArray.get(i).startAuthority) {
  78. _used.set(sortedArray.get(i).index, true);
  79. _authority += sortedArray.get(i).additionalAuthority;
  80. _answer.add(sortedArray.get(i).index);
  81. }
  82. }
  83. }
  84.  
  85. void calc() {
  86. bringPositiveAuthority();
  87. bringNegativeAuthority();
  88. }
  89.  
  90. void print() {
  91. _out.println(_answer.size());
  92. for (int i = 0; i < _answer.size(); i++) {
  93. _out.print(_answer.get(i) + 1 + " ");
  94. }
  95. _out.close();
  96. }
  97. }
  98.  
  99. public class Main {
  100. public static void main(String[] args) throws FileNotFoundException {
  101. //Scanner input = new Scanner(System.in);
  102. Worker worker = new Worker("input.txt", "output.txt");
  103. worker.calc();
  104. worker.print();
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement