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