比赛 收心赛 评测结果 WAAAWWAAAA
题目名称 卡牌游戏 最终得分 70
用户昵称 梧叶已同秋雨去 运行时间 1.995 s
代码语言 C++ 内存使用 25.83 MiB
提交时间 2026-02-24 11:12:09
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
long long n,m,a[1000005],b[1000005];long long t[1000005];
struct node{
    long long s,f,id;
}c[2000005];
bool cmp(node x,node y){
    return x.s<y.s;
}
int main(){
    freopen("card.in","r",stdin);
    freopen("card.out","w",stdout);
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);c[i].s=a[i];c[i].f=1;c[i].id=i;
    }
    for(int j=1;j<=n;j++){
        scanf("%lld",&b[j]);c[n+j].s=b[j];c[n+j].f=2;c[n+j].id=j;
    }
    sort(c+1,c+(2*n)+1,cmp);
//    for(int i=1;i<=2*n;i++){
//        cout<<c[i].s<<" ";
//    }cout<<endl;
//    for(int i=1;i<=2*n;i++){
//        cout<<c[i].id<<" ";
//    }cout<<endl;
    long long l=0,r=1,cnt=0,ans=1e9+1,mcnt=0;
    while(l<=r){
        while(cnt<n&&r<=2*n&&mcnt<m){
//            cout<<c[r].id<<" "<<t[c[r].id]<<endl;
            if(t[c[r].id]){
                t[c[r].id]++;
                if(c[r].f==1)mcnt--;r++;
//                cout<<1222<<endl;
            }else{
                cnt++;
                t[c[r].id]++;
                if(c[r].f==2)mcnt++;r++;
//                cout<<2111<<endl;
            }//cout<<mcnt<<endl;
        }
//        for(int i=l+1;i<=r-1;i++){
//            cout<<c[i].s<<" ";
//        }cout<<endl;
//        cout<<r<<endl;
        l++;
        if(cnt==n){
            ans=min(ans,c[r-1].s-c[l].s);
        }
        t[c[l].id]--;
        if(!t[c[l].id])cnt--;
        if(c[l].f==2&&t[c[l].id]==0)mcnt--;
    }
    printf("%d",ans);
    return 0;
}