二分思路,求查错或提纲正确思路
#pragma GCC diagnostic error "-std=c++14"
#pragma GCC target("avx")
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
template <class code>inline code read(const code &a)
{
code x=0;short w=0;char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return w?-x:x;
}
void print(int x){
if(x<0)putchar('-'),x=-x;
if(x>=10)print(x/10);
putchar(x%10+48);
}
int a[200005],n,k;
struct pp{
int z;
int zz;
int r;
}b[200005],c[200005],d[200005];
bool p[200005];
void dfs(int x,int y,int maxn){
if(y==(k+1)/2){
ans=min(ans,maxn);
return;
}
if(maxn>ans)return;
for(int i=x+2;i<=n;i++){
dfs(i,y+1,max(maxn,a[i]));
}
}
void dfs1(int x,int y,int maxn){
if(y==k/2){
ans=min(ans,maxn);
return;
}
if(maxn>ans)return;
for(int i=x+2;i<=n;i++){
dfs1(i,y+1,max(maxn,a[i]));
}
}
bool cmp(pp x,pp y){
return x.z<y.z;
}
bool check(int x){
memset(p,0,sizeof(p));
for(int i=1;i<=x;i++){
p[d[i].r]=1;
}
int js=x,flag=0;
if(p[1]&&!p[2])flag=1;
if(k%2&&p[n]&&!p[n-1])flag=1;
for(int i=1;i<=n;i++){
if(p[i]&&p[i+1]){
i++;
js--;
}
}
if(flag)return js>=(k+1)/2;
else return js>=k/2;
}
int main()
{
n=read(n),k=read(k);
int u1=0,u2=0,u=0;
for(int i=1;i<=n;i++){
a[i]=read(a[i]);
d[++u].z=a[i],d[u].r=i;
}
sort(d+1,d+n+1,cmp);
int l=1,r=n,ans=0;
while(l<=r){
int mid=(l+r)/2;
if(check(mid)){
r=mid-1;
ans=mid;
}else{
l=mid+1;
}
}
printf("%d\n",d[ans].z);
return 0;
}