BZOJ2761: [JLOI2011]不重复数字

2761: [JLOI2011]不重复数字

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1482  Solved: 569
[Submit][Status]

Description

给出N个数,要求把其中重复的去掉,只保留第一次出现的数。
例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4。
 

Input

输入第一行为正整数T,表示有T组数据。
接下来每组数据包括两行,第一行为正整数N,表示有N个数。第二行为要去重的N个正整数。
 

Output

 
对于每组数据,输出一行,为去重后剩下的数字,数字之间用一个空格隔开。

Sample Input

2
11
1 2 18 3 3 19 2 3 6 5 4
6
1 2 3 4 5 6

Sample Output

1 2 18 3 19 6 5 4
1 2 3 4 5 6

HINT

 

对于30%的数据,1 <= N <= 100,给出的数不大于100,均为非负整数;


对于50%的数据,1 <= N <= 10000,给出的数不大于10000,均为非负整数;


对于100%的数据,1 <= N <= 50000,给出的数在32位有符号整数范围内。


提示:


由于数据量很大,使用C++的同学请使用scanfprintf来进行输入输出操作,以免浪费不必要的时间。


 

 

Source

题解:

刚开始看错题了,而且是两遍。。。

sort顺便求rk数组

排重的时候用一个临时变量对于相同的值去一下 rk的min即可

代码:

 1 uses math;
 2 const maxn=500000+1000;
 3 var i,n,ans,t,m,tmp:longint;
 4     a,b,rk:array[0..maxn] of longint;
 5     v:array[0..maxn] of boolean;
 6     first:boolean;
 7 procedure sort(l,r:longint);
 8  var i,j,x,y:longint;
 9  begin
10  i:=l;j:=r;x:=a[(i+j)>>1];
11  repeat
12   while a[i]<x do inc(i);
13   while a[j]>x do dec(j);
14   if i<=j then
15    begin
16    y:=a[i];a[i]:=a[j];a[j]:=y;
17    y:=rk[i];rk[i]:=rk[j];rk[j]:=y;
18    inc(i);dec(j);
19    end;
20  until i>j;
21  if i<r then sort(i,r);
22  if j>l then sort(l,j);
23  end;
24 procedure main;
25  begin
26  m:=0;
27  readln(n);for i:=1 to n do begin read(a[i]);rk[i]:=i;b[i]:=a[i];end;
28  sort(1,n);tmp:=1;a[n+1]:=-1;
29  fillchar(v,sizeof(v),false);
30  for i:=1 to n+1 do
31   begin
32   if (i=1) or (a[i]<>a[i-1]) then
33    begin
34    v[tmp]:=true;tmp:=maxn;
35    end;
36   tmp:=min(tmp,rk[i]);
37   end;
38  first:=true;
39  for i:=1 to n do
40   if v[i] then
41    if first then begin first:=false;write(b[i]);end else  write(' ',b[i]);
42  writeln;
43  end;
44 
45 begin
46  assign(input,'input.txt');assign(output,'output.txt');
47  reset(input);rewrite(output);
48  readln(t);
49  while t>0 do
50   begin
51    main;dec(t);
52   end;
53  close(input);close(output);
54 end.     
View Code

 

posted @ 2014-08-15 09:25  ZYF-ZYF  Views(154)  Comments(0Edit  收藏  举报