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.  
  24.  
  25. vector<int> a;
  26.  
  27.  
  28. void consistency1(int n, int k) {
  29.  
  30. int s = 0, e = n-1;
  31. int ans = INF;
  32.  
  33. while(s<=e){
  34. int mid = s + (e-s)/2;
  35. int first = lower_bound(begin(a), end(a), a[mid]) - begin(a);
  36. int last = upper_bound(begin(a), end(a), a[mid]) - begin(a) - 1;
  37.  
  38. if( (last+1) % k != 0 ){
  39. ans = min(ans, a[mid]);
  40. e = first-1;
  41. }
  42. else{
  43. s = last+1;
  44. }
  45. }
  46.  
  47. cout << ans << " ";
  48.  
  49. }
  50.  
  51.  
  52.  
  53.  
  54. //* Lock BS
  55. //* [ 2 2 2 3 3 4 4 4 ]
  56. //* F F F T T T T T
  57.  
  58.  
  59. void consistency2(int n, int k) {
  60.  
  61. int s = 0, e = n-1;
  62. while(s<e){
  63. int mid = s + (e-s)/2;
  64. int lastIdx = upper_bound(begin(a), end(a), a[mid]) - begin(a) - 1;
  65. if( (lastIdx+1)%k != 0 ){
  66. e = mid;
  67. }
  68. else{
  69. s = lastIdx+1;
  70. }
  71. }
  72.  
  73.  
  74. cout << a[e] << " ";
  75.  
  76. }
  77.  
  78.  
  79. void solve() {
  80.  
  81. int n, k;
  82. cin >> n >> k;
  83.  
  84. a.resize(n);
  85.  
  86. for(int i=0; i<n; i++) cin >> a[i];
  87.  
  88.  
  89. consistency1(n, k);
  90. consistency2(n, k);
  91. cout << endl;
  92.  
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99. int32_t main() {
  100. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  101.  
  102. int t = 1;
  103. // cin >> t;
  104. while (t--) {
  105. solve();
  106. }
  107.  
  108. return 0;
  109. }
Success #stdin #stdout 0s 5320KB
stdin
14 4
2 2 2 2 3 3 4 4 4 4 5 5 5 5
stdout
3 3