fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. vector<int> numbers;
  8. vector<int> c;
  9. vector<int> indices;
  10. int total = 0;
  11.  
  12. //ponis
  13. int search(int start, int end, int value){
  14.  
  15. if(start > end) return -1;
  16.  
  17. if(start== end){
  18. if(c[start] == value)return start;
  19. return -1;
  20. }
  21.  
  22. if(end- start == 1){
  23. if(c[start] == value) return start;
  24. if(c[end] == value) return end;
  25. return -1;
  26. }
  27.  
  28. int middle = (start + end)/2;
  29.  
  30. if(c[middle] == value) return middle;
  31.  
  32. if(c[middle] < value) return search(middle+1, end, value);
  33.  
  34. return search(start, middle-1, value);
  35. }
  36.  
  37. int main(){
  38.  
  39. int n;
  40. cin >> n;
  41.  
  42. int temp;
  43.  
  44. for(int i =0; i < n; i++){
  45. cin >> temp;
  46. numbers.push_back(temp);
  47. c.push_back(temp);
  48. total+=temp;
  49.  
  50. }
  51.  
  52.  
  53. sort(c.begin(), c.end());
  54.  
  55. int index;
  56.  
  57. for(int i =0; i < numbers.size(); i++){
  58. c[numbers[i]]--;
  59. if((total - numbers[i]) % 2 == 0){
  60. temp = total-numbers[i];
  61. temp/=2;
  62.  
  63. index = search(0, numbers.size()-1, temp);
  64.  
  65. if(index != -1){
  66. if(numbers[i] != temp){
  67. indices.push_back(i);
  68. }else{
  69. if(index + 1 < numbers.size() && c[index+1] == temp){
  70. indices.push_back(i);
  71. }else if(index > 0 && c[index-1] == temp){
  72. indices.push_back(i);
  73. }
  74. }
  75. }
  76.  
  77. }
  78. c[numbers[i]]++;
  79. }
  80.  
  81. cout << indices.size() << endl;
  82.  
  83. for(int i =0; i < indices.size(); i++){
  84. cout << (indices[i] + 1 ) << " ";
  85. }
  86. cout << endl;
  87.  
  88.  
  89. return 0;
  90. }
Success #stdin #stdout 0s 15240KB
stdin
4
8 3 5 2
stdout
2
1 4