| 比赛 |
收心赛 |
评测结果 |
AAAAAAAAAA |
| 题目名称 |
卡牌游戏 |
最终得分 |
100 |
| 用户昵称 |
李金泽 |
运行时间 |
4.652 s |
| 代码语言 |
C++ |
内存使用 |
16.79 MiB |
| 提交时间 |
2026-02-24 12:27:11 |
显示代码纯文本
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define N 1000005
#define int long long
#define fo(i,l,r) for(int i=l;i<=r;i++)
#define rf(i,r,l) for(int i=r;i>=l;i--)
using namespace std;
int n,m,ans,x,y,z;bool ha[N],hb[N];
struct node{int i,op,x;bool operator<(node y){return x<y.x;}}a[N<<1];
void swap(int &x,int &y){int t=x;x=y;y=t;}
int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
bool ck(int x)
{
for(int i=1;i*2<=n;i++)ha[i]=hb[i]=0;
int s1=0,s2=0;
for(int i=1,j=1;j<=n;j++)
{
if(!ha[a[j].i]&&!hb[a[j].i])s1++;
if(!ha[a[j].i]&&!hb[a[j].i]&&a[j].op==1)s2++;
if(!ha[a[j].i]&&hb[a[j].i])s2--;
if(!a[j].op)ha[a[j].i]=1;if(a[j].op==1)hb[a[j].i]=1;
while(i<j&&a[j].x-a[i].x>x)
{
if(!ha[a[i].i]||!hb[a[i].i])s1--;
if(ha[a[i].i]&&hb[a[i].i]&&!a[i].op)s2++;
if(!ha[a[i].i]&&hb[a[i].i])s2--;
if(!a[i].op)ha[a[i].i]=0;if(a[i].op==1)hb[a[i].i]=0;
i++;
}
if(a[j].x-a[i].x<=x&&s1*2==n&&s2<=m)return 1;
}
return 0;
}
int read(){
int sum=0;bool f=0;char c=getchar();
for(;c<48||c>57;c=getchar())if(c==45)f=1;
for(;c>=48&&c<=57;c=getchar())sum=sum*10+(c&15);
return f?-sum:sum;
}
signed main(){
freopen("card.in","r",stdin);freopen("card.out","w",stdout);
n=read();m=read();
fo(i,1,n)a[i*2-1]={i,0,read()};
fo(i,1,n)a[i*2]={i,1,read()};
n<<=1;
sort(a+1,a+n+1);
int l=1,r=1e9,mid;
while(l<r)
{
mid=l+r>>1;
if(ck(mid))r=mid;
else l=mid+1;
}
printf("%lld",l);
return 0;
}