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. vector<int> a;
  27. int count(int n, int mid){
  28. int ct = 0;
  29. for(int i=0; i<n; i++){
  30. if(a[i] <= mid) ct++;
  31. }
  32. return ct;
  33. }
  34.  
  35.  
  36.  
  37.  
  38. int consistency1(int n) {
  39. int s = 1;
  40. int e = n-1;
  41. int ans = 0;
  42.  
  43. while(s<=e){
  44. int mid = s + (e-s)/2;
  45. if(count(n, mid) > mid) {
  46. ans = mid;
  47. e = mid-1;
  48. }
  49. else s = mid+1;
  50. }
  51. return ans;
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58. int consistency2(int n){
  59. int s = 1, e = n-1;
  60. while(s<e){
  61. int mid = s + (e-s)/2;
  62. if(count(n, mid) > mid){
  63. e = mid;
  64. }
  65. else s = mid+1;
  66. }
  67. return e;
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. int practice(int n){
  82.  
  83.  
  84.  
  85.  
  86. return 0;
  87. }
  88.  
  89.  
  90. void solve() {
  91.  
  92. int n;
  93. cin >> n;
  94.  
  95. a.resize(n);
  96. for(int i=0; i<n; i++) cin >> a[i];
  97.  
  98. cout << consistency1(n) << " " << consistency2(n) << endl;
  99. // cout << consistency1(n) << " -> " << practice(n) << endl;
  100.  
  101.  
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. int32_t main() {
  109. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  110.  
  111.  
  112. int t = 1;
  113. cin >> t;
  114. while (t--) {
  115. solve();
  116. }
  117.  
  118. return 0;
  119. }
Success #stdin #stdout 0s 5320KB
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