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 LINF = 2000000000000000001;
  11.  
  12.  
  13. inline int power(int base, int exp) {
  14. int res = 1;
  15. while (exp > 0) {
  16. if (exp % 2 == 1) res = (res * base)%M;
  17. base = (base * base);
  18. exp /= 2;
  19. }
  20. return res;
  21. }
  22.  
  23.  
  24. //_ ***************************** START Below *******************************
  25.  
  26. // 3
  27. // 5
  28. // 1 3 4 2 2
  29. // 5
  30. // 3 1 3 4 2
  31. // 5
  32. // 3 3 3 3 3
  33.  
  34.  
  35.  
  36. vector<int> a;
  37. int count(int n, int mid){
  38. int ct = 0;
  39. for(int i=0; i<n; i++){
  40. if(a[i] <= mid) ct++;
  41. }
  42. return ct;
  43. }
  44.  
  45.  
  46.  
  47.  
  48. int consistency1(int n) {
  49. int s = 1;
  50. int e = n-1;
  51. int ans = 0;
  52.  
  53. while(s<=e){
  54. int mid = s + (e-s)/2;
  55. if(count(n, mid) > mid) {
  56. ans = mid;
  57. e = mid-1;
  58. }
  59. else s = mid+1;
  60. }
  61. return ans;
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68. int consistency2(int n){
  69. int s = 1, e = n-1;
  70. while(s<e){
  71. int mid = s + (e-s)/2;
  72. if(count(n, mid) > mid){
  73. e = mid;
  74. }
  75. else s = mid+1;
  76. }
  77. return e;
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. int practice(int n){
  92.  
  93.  
  94.  
  95.  
  96. return 0;
  97. }
  98.  
  99.  
  100. void solve() {
  101.  
  102. int n;
  103. cin >> n;
  104.  
  105. a.resize(n);
  106. for(int i=0; i<n; i++) cin >> a[i];
  107.  
  108. cout << consistency1(n) << " " << consistency2(n) << endl;
  109. // cout << consistency1(n) << " -> " << practice(n) << endl;
  110.  
  111.  
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. int32_t main() {
  119. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  120.  
  121.  
  122. int t = 1;
  123. cin >> t;
  124. while (t--) {
  125. solve();
  126. }
  127.  
  128. return 0;
  129. }
Success #stdin #stdout 0s 5324KB
stdin
3
5
1 3 4 2 2
5
3 1 3 4 2
5
3 3 3 3 3
stdout
2 2
3 3
3 3