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.  
  63. while(s<e){
  64. int mid = s + (e-s)/2;
  65. int first = lower_bound(begin(a), end(a), a[mid]) - begin(a);
  66. int last = upper_bound(begin(a), end(a), a[mid]) - begin(a) - 1;
  67.  
  68. if( (last+1) % k != 0 ){
  69. e = first;
  70. }
  71. else{
  72. s = last+1;
  73. }
  74. }
  75.  
  76. cout << a[e] << " ";
  77.  
  78. }
  79.  
  80.  
  81.  
  82. void solve() {
  83.  
  84. int n, k;
  85. cin >> n >> k;
  86.  
  87. a.resize(n);
  88.  
  89. for(int i=0; i<n; i++) cin >> a[i];
  90.  
  91.  
  92. consistency1(n, k);
  93. consistency2(n, k);
  94. cout << endl;
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. int32_t main() {
  103. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  104.  
  105. int t = 1;
  106. // cin >> t;
  107. while (t--) {
  108. solve();
  109. }
  110.  
  111. return 0;
  112. }
Success #stdin #stdout 0.01s 5288KB
stdin
14 4
2 2 2 2 3 3 4 4 4 4 5 5 5 5
stdout
3 3