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. bool isPossible(int n, int k, int mid ){
  29. int ct = 0;
  30. int i=0;
  31. while(i<n){
  32. if(a[i] <= mid){
  33. ct++;
  34. i += 2;
  35. }
  36. else i+=1;
  37. }
  38. return ct >= k;
  39. }
  40.  
  41.  
  42. void consistency(int n, int k) {
  43.  
  44. int s = 0, e = INF;
  45.  
  46. while(s<e){
  47. int mid = s + (e-s)/2;
  48.  
  49. if(isPossible(n, k, mid) ){
  50. e = mid;
  51. }
  52. else{
  53. s = mid+1;
  54. }
  55. }
  56.  
  57. cout << s << " ";
  58.  
  59. }
  60.  
  61.  
  62. void solve() {
  63.  
  64. int n, k;
  65. cin >> n >> k;
  66.  
  67. a.resize(n);
  68.  
  69. for(int i=0; i<n; i++) cin >> a[i];
  70.  
  71.  
  72. consistency(n, k);
  73. cout << endl;
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. int32_t main() {
  82. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  83.  
  84. int t = 1;
  85. // cin >> t;
  86. while (t--) {
  87. solve();
  88. }
  89.  
  90. return 0;
  91. }
Success #stdin #stdout 0.01s 5272KB
stdin
5 2
2 7 9 3 1 
stdout
2