Codeforces Round #651 (Div. 2) D. Odd-Even Subsequence ###K ###K //K
题目链接:https://codeforces.ml/contest/1370/problem/D
题意:给一段序列 找一段长度为k不连续子序列 使得min(max(s1,s3,s5……),max(s2,s4,s6……)) 最小
思路:正面思考 没法确定何时最优 考虑复杂度和给定的长度k 不难想到二分check答案
假设现在求的是奇数的序列 那么只要有a[i]<=mid 那么就让cnt奇数++ 然后下一个数马上要cnt偶数++ 这样才能让后面可以选择的数更多
刚开始想的二分还错误的贪心了一下 想k是奇数的时候 一定让偶数的序列取得最大值 如2 5 4 如果让mid为4 让偶数取的话就会不满足
反正求一遍奇数贡献和偶数贡献不难 所以这样的不知道是否正确的贪心很没必要
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define pb push_back 5 const int maxn=2e5+10; 6 const int mod=1e4+7; 7 int n,k; 8 int a[maxn]; 9 int check(int x) 10 { 11 int cnt1=0,cnt2=0,cnt3=0,cnt4=0; 12 for(int i=1;i<=n;i++) 13 { 14 if(a[i]<=x) 15 { 16 cnt1++; 17 if(i+1<=n) 18 cnt2++,i++; 19 } 20 } 21 cnt3++; 22 for(int i=2;i<=n;i++) 23 { 24 if(a[i]<=x) 25 { 26 cnt4++; 27 if(i+1<=n) 28 cnt3++,i++; 29 } 30 } 31 if((cnt1>=(k+1)/2&&cnt2>=k/2)||(cnt3>=(k+1)/2&&cnt4>=k/2)) 32 return 1; 33 else 34 return 0; 35 } 36 37 38 39 void solve() 40 { 41 cin>>n>>k; 42 for(int i=1;i<=n;i++) 43 { 44 cin>>a[i]; 45 } 46 int l=1,r=1e9; 47 int ans=0; 48 while(l<=r) 49 { 50 int mid=(l+r)/2; 51 if(check(mid)) 52 { 53 r=mid-1; 54 ans=mid; 55 } 56 else 57 l=mid+1; 58 } 59 cout<<ans<<'\n'; 60 } 61 62 63 int main() 64 { 65 ios::sync_with_stdio(false); 66 cin.tie(0); 67 solve(); 68 69 70 }

浙公网安备 33010602011771号