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. while(s<e){
  32. int mid = s + (e-s)/2;
  33. int lastIdx = upper_bound(begin(a), end(a), a[mid]) - begin(a) - 1;
  34. if( (lastIdx+1)%k != 0 ){
  35. e = mid;
  36. }
  37. else{
  38. s = lastIdx+1;
  39. }
  40. }
  41.  
  42.  
  43. cout << a[e] << " ";
  44.  
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. void solve() {
  53.  
  54. int n, k;
  55. cin >> n >> k;
  56.  
  57. a.resize(n);
  58.  
  59. for(int i=0; i<n; i++) cin >> a[i];
  60.  
  61.  
  62. consistency1(n, k);
  63. cout << endl;
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. int32_t main() {
  72. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  73.  
  74. int t = 1;
  75. // cin >> t;
  76. while (t--) {
  77. solve();
  78. }
  79.  
  80. return 0;
  81. }
Success #stdin #stdout 0s 5320KB
stdin
14 4
2 2 2 2 3 3 4 4 4 4 5 5 5 5
stdout
3