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. // 10
  28. // -100 -100 -70 -60 -60 110 120 130 130 160
  29.  
  30.  
  31.  
  32.  
  33. vector<int> a;
  34.  
  35. vector<vector<int>> consistency1(int n){
  36.  
  37. sort(begin(a), end(a));
  38.  
  39. vector<vector<int>> ans;
  40.  
  41. for (int i = 0; i <= n - 3; i++) {
  42. if (i > 0 && a[i] == a[i - 1]) continue;
  43.  
  44. unordered_map<int, int> mp;
  45. for (int k=n-1; k>=i+2; k--) {
  46. mp[a[k]]++;
  47. }
  48.  
  49. for (int j = i+1; j<=n-2; j++) {
  50. if (j>=i+2 && a[j] == a[j - 1]){
  51.  
  52. //* sync map to clear next
  53. mp[a[j+1]]--;
  54. if (mp[a[j+1]] == 0) mp.erase(a[j+1]);
  55. continue;
  56. }
  57. int sum = a[i] + a[j];
  58.  
  59. if (mp.count(-sum)) {
  60. ans.push_back({a[i], a[j], -sum});
  61. }
  62.  
  63. //* sync map to clear next
  64. mp[a[j+1]]--;
  65. if (mp[a[j+1]] == 0) mp.erase(a[j+1]);
  66. }
  67. }
  68.  
  69.  
  70. return ans;
  71.  
  72. }
  73.  
  74.  
  75.  
  76. vector<vector<int>> consistency2(int n){
  77.  
  78. sort(begin(a), end(a));
  79.  
  80. vector<vector<int>> ans;
  81.  
  82. for(int i=0; i<=n-3; i++){
  83. if(i-1>=0 && a[i] == a[i-1]) continue;
  84.  
  85. int sum = -a[i];
  86.  
  87. int s = i+1;
  88. int e = n-1;
  89.  
  90. while(s<e){
  91. if(a[s]+a[e] == sum){
  92.  
  93. ans.push_back({a[i], a[s], a[e]});
  94.  
  95. int left = a[s];
  96. while(s<e && a[s] == left) s++;
  97.  
  98. int right = a[e];
  99. while(e>s && a[e] == right) e--;
  100.  
  101. }
  102. else if(a[s] + a[e] < sum){
  103. s++;
  104. }
  105. else{
  106. e--;
  107. }
  108. }
  109.  
  110. }
  111.  
  112.  
  113. return ans;
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. vector<vector<int>> practice(int n){
  140.  
  141.  
  142. }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. void solve() {
  149.  
  150. int n;
  151. cin>> n;
  152.  
  153. a.resize(n);
  154. for(int i=0; i<n; i++) cin >> a[i];
  155.  
  156. auto ans = consistency2(n);
  157.  
  158. for(auto t : ans){
  159. cout << t[0] << " " << t[1] << " " << t[2] << endl;
  160. }
  161.  
  162.  
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169. int32_t main() {
  170. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  171.  
  172. int t = 1;
  173. // cin >> t;
  174. while (t--) {
  175. solve();
  176. }
  177.  
  178. return 0;
  179. }
Success #stdin #stdout 0.01s 5272KB
stdin
10
-100 -100 -70 -60 -60 110 120 130 130 160 
stdout
-100 -60 160
-70 -60 130
-60 -60 120