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