同花顺

“同花顺
题目描述
所谓同花顺,就是指这些扑克牌,它们花色相同,并且数字连续。
现在我手中有 n 张扑克牌,但它们可能并不能凑成同花顺。我现在想知道,最
少更换其中的多少张牌,我能让这 n 张牌都凑成同花顺?
输入格式
第一行一个整数 n,表示扑克牌的张数。
接下来 n行,每行两个整数 ai 和 bi。其中 ai 表示第 i 张牌的花色, bi 表示第
i 张牌的数字。
输出格式
一行一个整数,表示最少更换多少张牌可以达到目标。
样例输入 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 ≤ ai ≤ 10……5, 1 ≤ bi ≤ n。
对于 100% 的数据, n ≤ 10……5, 1 ≤ ai, bi ≤ 10……9。

program df;
var i,j,n,m,x,y,z,k,t:longint;
a,b,c,d:array[0..100000] of longint;
procedure sq(l,r:longint);
var i,j,m1,m2,dd:longint;
begin
i:=l; j:=r;
m1:=a[(l+r) div 2];
m2:=c[(l+r) div 2];
repeat
while (a[i]小于m1) or ((a[i]=m1) and (c[i]小于m2)) do inc(i);
while (a[j]>m1) or ((a[j]=m1) and (c[j]>m2)) do dec(j);
if i<=j then
begin
dd:=a[i]; a[i]:=a[j]; a[j]:=dd;
dd:=c[i]; c[i]:=c[j]; c[j]:=dd;
inc(i); dec(j);
end;
until i>j;
if j>l then sq(l,j);
if r>j then sq(i,r);
end;

function max(x,y:longint):longint;
begin
if x>y then exit(x)
else exit(y);
end;
function min(x,y:longint):longint;
begin
if x>y then exit(y)
else exit(x);
end;

begin
assign(input,’card.in’);
reset(input);
assign(output,’card.out’);
rewrite(output);
readln(n);
for i:=1 to n do
readln(a[i],c[i]);
sq(1,n);
t:=n-1; z:=1;
for i:=2 to n do
if a[i]=a[i-1] then
begin
if c[i]<>c[i-1] then
begin
inc(z);
d[z]:=d[z-1]+c[i]-c[i-1];
end;
if a[i+1]<>a[i] then
begin
y:=0;
for j:=1 to z do
for k:=j+1 to z do
if d[k]-d[j]<=n then y:=max(y,k-j+1);
t:=min(t,n-y);
z:=1;
end;
end;
writeln(t);
close(input);
close(output);
end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值