【洛谷P2652】同花顺

题面

分析

今天考了这道题qwq,题解参考了老师的solution。

\(30pts\):暴搜解决

\(60pts\):去重+贪心

下面是满分做法:

首先,肯定是要排序的。我们按照花色排序,相同花色的按照数字排序。然后就是去重,由于重复的牌无论如何也会被替换掉,所以我们先将其去重,并记录下此时的牌数。

考虑枚举每种花色中,以每一张牌作为结尾时可能的序列,取最大值,最后用排序前的总牌数减去这个值即可,显然地,如果当前牌的值为\(a\)\(last\)的值为\(b\),则需满足\(a-b+1>m\)

代码

#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;
}
posted @ 2019-08-05 15:37  小蒟蒻hlw  阅读(135)  评论(0)    收藏  举报