fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<int> tab;
  5.  
  6. bool czy(int x,int n,int k)
  7. {
  8. vector<int> lis;
  9. vector<int> dp(k+3,0);
  10. for(int i = 0;i < n;++i)
  11. {
  12. if(tab[i] >= x)
  13. {
  14. lis.push_back(1);
  15. }
  16. else
  17. {
  18. lis.push_back(0);
  19. }
  20. }
  21. int d = (n%k) + ((n%k) == 0 ? n : 0);
  22. for(int i = n-1;i >= 0;i--)
  23. {
  24. int j = i%k;
  25. dp[j] = max(dp[j],(j+1 < d ? dp[j+1] : 0) + lis[i]);
  26. //cout << dp[j] << " " << j << " dp\n";
  27. }
  28. // cout << dp[0] << " " << d << endl;
  29. if(dp[0] > d/2) return true;
  30. return false;
  31. //cout << dp[k-1] << endl;
  32. }
  33.  
  34. int main()
  35. {
  36. int t;
  37. cin >> t;
  38. for(int q = 0;q < t;++q)
  39. {
  40. int n,k,x;
  41. cin >> n >> k;
  42. tab = {};
  43. for(int i = 0;i < n;++i)
  44. {
  45. cin >> x;
  46. tab.push_back(x);
  47. }
  48. //cout << czy(3,n,k);
  49. int l = 0,p=1000000003,mid;
  50. //czy(6,n,k);
  51. while(l < p)
  52. {
  53. mid = (l+p+1)/2;
  54. //cout << l << " " << p << " lr\n";
  55. if(czy(mid,n,k) == true)
  56. {
  57. l = mid;
  58. }
  59. else
  60. {
  61. p = mid-1;
  62. }
  63. }
  64. cout << l << endl;
  65. }
  66. }
Success #stdin #stdout 0.01s 5284KB
stdin
5
4 3
3 9 9 2
5 3
3 2 5 6 4
7 1
5 9 2 6 5 4 6
8 2
7 1 2 6 8 3 4 5
4 5
3 4 5 6
stdout
3
4
0
0
4