比赛 收心赛 评测结果 AAAAAAAAAA
题目名称 卡牌游戏 最终得分 100
用户昵称 赵飞羽 运行时间 1.335 s
代码语言 C++ 内存使用 11.57 MiB
提交时间 2026-02-24 09:22:29
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;

const int N = 2000010;
int n, m, cnt, t[N], ans;
struct node{
    int x, y, z;
    bool friend operator <(node u, node v) {
        return u.x < v.x;
    }
} a[N];

signed main() {
    freopen("card.in", "r", stdin);
    freopen("card.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
	    cin >> a[i].x;
        a[i].y = 1;
        a[i].z = i;
    }
	for (int i = 1; i <= n; i++) {
	    cin >> a[i+n].x;
        a[i+n].y = 0;
        a[i+n].z = i;
    }
    sort(a+1, a+1+n+n);
    int l = 1, r = 2 * n;
    while (r > l) {
        if (cnt >= m || t[a[r].z]) break;
        t[a[r].z] = 1;
        if (a[r].y) cnt++;
        r--;
    }
    ans = a[r].x - a[l].x;
    while (r <= 2 * n) {
        if (a[l].y) cnt++;
        t[a[l].z]++;
        while (r <= 2 * n + 10) {
            if (cnt <= m && t[a[l].z] == 1) break;
            if (a[r+1].y) cnt--;
            t[a[r+1].z]--;
            r++;
        }
        if (r > 2 * n) break;
        ans = min(ans, a[r].x - a[l+1].x);
        l++;
    }
    cout << ans;
	return 0;
}