fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5.  
  6.  
  7. const int M = 1000000007;
  8. const int N = 3e5+9;
  9. const int INF = 2e9+1;
  10. const int MAXN = 100000;
  11. const int LINF = 2000000000000000001;
  12.  
  13. //_ ***************************** START Below *******************************
  14.  
  15.  
  16.  
  17. vector<int> a;
  18.  
  19. vector<int> consistency(int n, int k){
  20.  
  21. vector<pair<int,int>> p(n);
  22. for(int i=0; i<n; i++){
  23. p[i] = {a[i], i};
  24. }
  25.  
  26. sort(begin(p), end(p));
  27.  
  28. for(int i=0; i<n-2; i++){
  29. int t = k-p[i].first;
  30. int s = i+1, e = n-1;
  31.  
  32. while(s<e){
  33. int left = p[s].first;
  34. int right = p[e].first;
  35.  
  36.  
  37. if(left + right == t) {
  38. vector<int> ans = {p[i].second+1, p[s].second+1, p[e].second+1};
  39. sort(begin(ans), end(ans));
  40. return ans;
  41. }
  42. else if(left + right > t){
  43. e--;
  44. }
  45. else {
  46. s++;
  47. }
  48. }
  49. }
  50.  
  51. return {};
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. vector<int> practice(int n, int k){
  63.  
  64. return {};
  65. }
  66.  
  67.  
  68.  
  69.  
  70. void solve() {
  71.  
  72. int n, k;
  73. cin >> n >> k;
  74.  
  75. a.resize(n);
  76. for(int i=0; i<n; i++) cin >> a[i];
  77.  
  78.  
  79. auto ans1 = consistency(n, k);
  80. if(ans1.empty()) cout << "IMPOSSIBLE" << endl;
  81. else cout << ans1[0] << " " << ans1[1] << " " << ans1[2] << endl;
  82.  
  83.  
  84. // auto p = practice(n, k);
  85. // if(p.empty()) cout << " -> " << "IMPOSSIBLE" << endl;
  86. // else cout << " -> " << ans1[0] << " " << ans1[1] << " " << ans1[2] << endl;
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94. int32_t main() {
  95. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  96.  
  97. int t = 1;
  98. while (t--) {
  99. solve();
  100. }
  101.  
  102. return 0;
  103. }
Success #stdin #stdout 0s 5320KB
stdin
4 8
2 7 5 1
stdout
1 3 4