测试
A 同花顺
文件名 输入文件 输出文件 时间限制 空间限制
card.cpp/c/pas card.in card.out 1s 512MB
题目描述
所谓同花顺,就是指一些扑克牌,它们花色相同,并且数字连续。
现在我手里有 n 张扑克牌,但它们可能并不能凑成同花顺。我现在想知道,最
少更换其中的多少张牌,我能让这 n 张牌凑成一个同花顺?
输入格式
第一行一个整数 n,表示扑克牌的张数。
接下来 n 行,每行两个整数 a i 和 b i 。其中 a i 表示第 i 张牌的花色,b i 表示第
i 张牌的数字。
(注意: 这里的牌上的数字不像真实的扑克牌一样是 1 到 13, 具体见数据范围)
输出格式
一行一个整数,表示最少更换多少张牌可以达到目标。
样例输入 1
5
1 1
1 2
1 3
1 4
1 5
样例输出 1
0
样例输入 2
5
1 9
1 10
2 11
2 12
2 13
样例输出 2
2
数据范围
对于 30% 的数据,n ≤ 10。
对于 60% 的数据,n ≤ 10 5 ,1 ≤ a i ≤ 10 5 ,1 ≤ b i ≤ n。
对于 100% 的数据,n ≤ 10 5 ,1 ≤ a i ,b i ≤ 10 9 。
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int n,ans,cnt; struct node{ int col,num; }a[100010],q[100010]; int cmp(node x,node y){ if(x.col==y.col)return x.num<y.num; return x.col<y.col; } int main(){ freopen("card.in","r",stdin);freopen("card.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d%d",&a[i].col,&a[i].num); sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++){ if(a[i].col==a[i-1].col&&a[i].num==a[i-1].num)continue; q[++cnt]=a[i]; } for(int i=1;i<=cnt;i++){ int tmp=0; for(int j=i;j>=1;j--){ if(q[j].col==q[i].col&&q[i].num-q[j].num+1<=n)tmp++; else break; } ans=max(ans,tmp); } printf("%d",n-ans); fclose(stdin);fclose(stdout); return 0; }