1293: [SCOI2009] Birthday present time limit:10 Sec Memory limit:162 MB
submit:1096 solved:584
[Submit] [Status] Description Little West has a very long ribbon, ribbon hanging on a variety of color beads. Known color beads have n, divided into K species. Simply put, the ribbon can be considered as the x-axis, each color bead has a corresponding coordinate (i.e. position). There can be no color beads on some coordinates, but multiple beads can also appear in the same position. Xiao Xi is going to cut a ribbon of ribbons for his birthday soon. In order to make the gift ribbon beautiful enough, Xiao Xi hopes that this section of the Ribbon can contain all kinds of colored beads. At the same time, in order to facilitate, Xiao Xi hope that this piece of ribbon as short as possible, you can help the small west to calculate the shortest length? The length of the Ribbon is the difference between the start position of the ribbon and the end position. The first line of input contains two integers n, K, each representing the total number of beads and the number of species. The next K-line, the first number of each row is Ti, indicating the number of color beads i. The next step is to give ti a non-negative integer, the position of the TI color beads appear respectively. The output should contain one row for the shortest ribbon length. Sample Input6 3
1 5
2 1 7
3 1 3 8
Sample Output3
HINT
There are a variety of options, among which the shorter is the 5~8 and the more. The latter length is 3 shortest.
"Data Size"
For 50% of data, n≤10000;
For 80% of data, n≤800000;
For 100% of data, 1≤n≤1000000,1≤k≤60,0≤ color bead position <2^31.
Source
The puzzle: Well, finally no brain pumping (phile: Praise such as Chao Hansbug: "da) ... The idea is also relatively simple, is directly through that what the various pillars (hansbug: Ah yuck, beads phile: You ah a look is more than Aruba ... Hansbug:tt This is all you have found) coordinates from small to large order, and then start from 1 to find a shortest to meet the test instructions of the sub-section, record the length, and then the sub-section of the first position of the beads are all thrown away, and then at the end of the expansion, Each time to expand as little as possible but to meet the expansion can contain m beads, and then every time the length of the current segment, this way to roll over the past, the theoretical complexity of O (nlogn+n) (ps:1. Note that the color bead position can be 0, so it is recommended to store in the sorted array + 1, it does not affect the results, and subsequent processing less than a lot of trouble 2. This problem incredibly 3928ms I am also drunk, estimated this sequence is more orderly, so the quick row compare tease?
1 var
2I,j,k,l,m,n,ll:longint;
3A,b,c:Array[0..1000500] ofLongint;
4procedureSwapvarX,y:longint);
5varZ:longint;
6 begin
7Z:=x;x:=y;y:=z;
8End;
9procedureSort (l,r:longint);
TenvarI,j,x,y:longint;
Onebegin
Ai:=l;j:=r;x:=a[(I+J)Div2];
-Repeat
- whileA[i]<x DoInc (I);
the whileA[j]>x DoDec (j);
-ifI<=j Then
- begin
-Swap (a[i],a[j]);
+Swap (b[i],b[j]);
-Inc (I);d EC (j);
+End;
Auntili>j;
atifI<r ThenSort (i,r);
-ifL<j ThenSort (l,j);
-End;
-begin
-READLN (N,M);
-Fillchar (A,sizeof (a),0);
inFillchar (b,sizeof (b),0);
- fori:=1 toM Do
tobegin
+Read (l);
- forj:=1 toL Do
thebegin
*Read (k);
$Inc (a[0]); a[a[0]]:=k+1;
Panax Notoginsengb[a[0]]:=i;
- End;
theREADLN;
+End;
Aa[0]:=0; l:=0;
theSort1, n);
+Fillchar (C,sizeof (c),0);
-i:=1;
$j:=1;
$ while(l<m) and(j<=n) Do
-begin
-K:=j;
the whileA[J]=A[K] Do
- begin
WuyiInc (C[b[j]);
theifc[b[j]]=1 ThenInc (L);
-Inc (J);
WuEnd;
-End;
AboutifJ>n ThenHalt
$ll:=a[j-1]-a[i];
- whileJ<=n Do
-begin
-K:=i;
A whileA[I]=A[K] Do
+begin
theDec (C[b[i]]);
- ifc[b[i]]=0 ThenDec (l);
$Inc (I);
theEnd;
the while(l<m) and(j<=n) Do
thebegin
theK:=j;
- while(l<m) and(A[j]=a[k]) Do
inbegin
theInc (C[b[j]);
theifc[b[j]]=1 ThenInc (L);
AboutInc (J);
theEnd;
theEnd;
the ifL<m ThenBreak
+if(a[j-1]-a[i]) <ll Thenll:=a[j-1]-a[i];
-End;
theWriteln (LL);
BayiEnd.
1293: [SCOI2009] Birthday present