对局匹配

直接贪心

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
#include<math.h>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII;
void makedata() {
    freopen("input.txt", "w", stdout);
    cout << 200000 << endl;

    for(int i = 0; i < 200000; i++) cout << 1000000000 << ' ';

    fclose(stdout);
}

int a[110000];

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    //makedata();
    std::ios::sync_with_stdio(0), cin.tie(0);
    int n, k;
    cin >> n >> k;

    for(int i = 0; i < n; i++) cin >> a[i];

    sort(a, a + n);
    int ptr = 0, ans = 0;

    while(ptr < n) {
        if(ptr + 2 < n && a[ptr + 2] - a[ptr] <= k) {
            ans++;
            ptr += 3;
        } else ptr++;
    }

    cout << ans << endl;
    return 0;
}
View Code

相关文章: