【洛谷P2652】同花顺

题面

分析

需要先将牌排序并去重。因为重复的牌是一定会被换掉的,所以去重,并用\(m\)记录之前的牌数。然后是处理部分。我们将每种花色的每一张牌作为起点牌,分别求出其满足要求的牌数,用\(last\)作中间处理,最后找最多的,答案为总牌数减去最多的满足要求牌数,即\(m-ans\)

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define il inline
#define re register
#define tie0 cin.tie(0),cout.tie(0)
#define fastio ios::sync_with_stdio(false)
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
typedef long long ll;

template <typename T> inline void read(T &x) {
    T f = 1; x = 0; char c;
    for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1;
    for ( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
    x *= f;
}

struct card {
    int a, b;
    bool operator < (const card &x)const{return a < x.a || (a == x.a && b < x.b);}
    bool operator == (const card &x)const{return a == x.a && b == x.b;}
}cd[100003];

int n, last, ans;

int main() {
//  File("card");
    read(n);
    for (int i = 1; i <= n; ++i) read(cd[i].a), read(cd[i].b);
    sort(cd + 1, cd + 1 + n);
    int m = n;
    n = unique(cd + 1, cd + 1 + n) - (cd + 1);
    for (int i = 1; i <= n; ++i) {
        if (i == 1 || cd[i].a != cd[i-1].a) last = i;
        while(cd[i].b - cd[last].b + 1 > m) last++;
        ans = max(ans, i - last + 1);
    }
    printf("%d",m - ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hlw1/p/11303356.html