fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28. using pii = pair<int,int>;
  29.  
  30. vector<int> a;
  31.  
  32. vector<int> consistency(int n, int t){
  33.  
  34. unordered_map<int, int> mp;
  35. for(int k=2; k<=n-2; k++){
  36. for(int l=k+1; l<=n-1; l++){
  37. int sum = a[k] + a[l];
  38. mp[sum]++;
  39. }
  40. }
  41.  
  42. int x = -1;
  43. int y = -1;
  44.  
  45. for(int j=1; j<=n-3; j++){
  46.  
  47. int i = j-1;
  48. while(i>=0){
  49. int sum = a[i] + a[j];
  50. if(mp.count(t-sum)){
  51. x = i;
  52. y = j;
  53. break;
  54. }
  55. i--;
  56. }
  57.  
  58. for(int k=j+2; k<=n-1; k++){
  59. int sum = a[j+1] + a[k];
  60. mp[sum]--;
  61. if(mp[sum] == 0) mp.erase(sum);
  62. }
  63. }
  64.  
  65. if(x==-1 && y == -1) return {};
  66.  
  67.  
  68. //* 2 Sum Leetcode
  69.  
  70. vector<pii> p;
  71. for(int i=y+1; i<n; i++){
  72. p.push_back({a[i], i});
  73. }
  74.  
  75. sort(begin(p), end(p));
  76.  
  77.  
  78. t = t-(a[x] + a[y]);
  79. int s = 0, e = p.size()-1;
  80.  
  81. while(s<e){
  82. int sum = p[s].first + p[e].first;
  83.  
  84.  
  85. if(sum < t) s++;
  86. else if(sum > t) e--;
  87. else{
  88. int k = p[s].second;
  89. int l = p[e].second;
  90. return {x+1, y+1, k+1, l+1};
  91. }
  92. }
  93.  
  94. return {};
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. vector<int> practice(int n, int k){
  112.  
  113.  
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120. void solve() {
  121.  
  122. int n, k;
  123. cin>> n >> k;
  124.  
  125. a.resize(n);
  126. for(int i=0; i<n; i++) cin >> a[i];
  127.  
  128. auto ans = consistency(n, k);
  129.  
  130. if(ans.empty()){
  131. cout << "IMPOSSIBLE";
  132. return;
  133. }
  134. print(ans);
  135.  
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142. int32_t main() {
  143. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  144.  
  145. int t = 1;
  146. // cin >> t;
  147. while (t--) {
  148. solve();
  149. }
  150.  
  151. return 0;
  152. }
Success #stdin #stdout 0s 5324KB
stdin
8 6
4 3 1 3 2 1 4 2
stdout
3 5 6 8