Time Limit:10 Sec Memory limit:162 MB
submit:1559 solved:848
[Submit] [Status] [Discuss] DescriptionLittle West has a very long ribbon, the Ribbon is hung with a variety of colored 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. InputThe first line 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. Outputshould 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[Submit][status][discuss]The following:I was looking for a monotonous queue, but this question looks like a chain list ... The idea of violence is still obvious: according to the position of the color bead enumeration, assuming that the color bead as the starting point or end point, and then backward or forward enumeration, until enough to gather all the colors, and update the answer. Obviously this complexity is O (n^2), N is millions, it is obviously not feasible. The following is an optimization: from O (n^2) to O (N) ... constructs a next[] array, next[i] means: Record the same color as I, the number of the last color bead, the number is to the color beads when reading the numbers, because N limited but the position of each color bead may not be continuous, so using CNT to indicate that the first few color beads are relatively provincial space. To construct a head[] array, head[i] means: Color is the number of the most colorful beads of I, where head[] is constantly updated to achieve the purpose of reducing complexitynext[],the construction of head[] is the core of algorithm optimizationWe then enumerate the beads from the back, assuming that the POS is enumerated to the POS position and the point of sale is the endpoint. For the I color, let Head[i] (head[i) storage is the last color is I the color of the number of beads, note! Is the serial number is not the position) using next[] to jump forward, until a color bead position is greater than POS, and next[head[i]]!=0, then next[head[i] "must be the nearest point of the POS color of the beads (and ensure that the pos-1,pos-2 update will not leak , which is not skipped), contributes to the current answer, thus the maximum value of all colors computed by POS, and then the shortest in 1~n.
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <cmath>6#include <algorithm>7#include <queue>8#include <vector>9 using namespacestd;TentypedefLong LongLL; One Const intmaxn=1000010; A intn,k,maxpos,cnt; - intkin[maxn],a[maxn],next[maxn],head[ -],tmp[ -]; - intans=1e9; theInlinevoidCalcintPOS) { - intans=0; - for(intI=1; i<=k;i++){ - while(kin[head[i]]>POS) { + if(next[head[i]]==0)return ; -head[i]=Next[head[i]]; + } A if(Kin[head[i]]<=pos) Ans=max (ans,pos-Kin[head[i]]); at } -ans=min (ans,ans); - } - intMain () { -scanf"%d%d",&n,&K); - for(intI=1, t;i<=k;i++){ inscanf"%d",&t); - for(intj=1, pos;j<=t;j++){ toscanf"%d", &pos); maxpos=Max (maxpos,pos); +Kin[++cnt]=pos; A[cnt]=pos;//The Kin and a record the position of the first CNT colored beads -Next[cnt]=head[i];//NEXT[CNT] Record the number of the previous bead with the same CNT color thehead[i]=cnt;//Update Head Array * } $ }Panax NotoginsengSort (A +1, a+cnt+1);//Sort by Location A[1]~a[n] indicates that the N colored beads are the first - for(intI=cnt;i>0; i--){ theCalc (a[i]);//search from back to front in order to update head array to reduce complexity + } Aprintf"%d", ANS); the return 0; +}
Bzoj 1293: [SCOI2009] Birthday present