[SDOI2009] birthday gift (monotone queue)

 

And very much like visiting the exhibition, is different for each location can be put away with a few beads of ...... structures exist on OK

The first type of write queue structure, easy to use ~ unexpected!

Title Description

Konishi had on a long ribbon, ribbons hung with a variety of colored beads. Beads are known with N, into K species. Briefly, ribbon may be considered as the x-axis, each of the coordinate Beads (i.e., position) corresponding to a. Beads can not coordinate some time, but several Beads can also appear in the same position.

Bush birthday is coming up, so the small West intends to cut some ribbons for the little cloth. To make a gift ribbons pretty enough, Konishi hope ribbon in this paragraph can contain all kinds of Beads. Meanwhile, for convenience, Konishi hope this ribbon as short as possible, you can help Konishi calculate the shortest length of it? The ribbon is the ribbon length start position to the end position of the position difference.

Input Format

The first line contains two integers N, K, represent the total number and the number of kinds of colored beads. Next K lines of the first number is Ti, i represents the number of kinds of colored beads. Next, Ti ascending given nonnegative integer, a location which Ti appears Beads respectively.

Output Format

The output should contain a line, the length of the shortest ribbons.

Sample input and output

Input # 1

63

15

217

3138

Output # 1 
3

Description / Tips

[Sample Description]

There are many alternative schemes, which are relatively short 1-5 and 5-8. 3 the shortest length of the latter.

[Data] scale

For 50% of the data, N≤10000;

For 80% of the data, N≤800000;

To 100% of the data, 1≤N≤1000000,1≤K≤60, 0 beads position to <2 31 is .

ΣT i = n;

 

 

#include<bits/stdc++.h>
using namespace std;
struct node{
  int pos,kind;    
}; 
int n,M,t,k,len,ans=INT_MAX,sum;
node a[1000050];
int m[1000050];
int cmp(node x,node y){return x.pos<y.pos;}
int main()
{
  queue<node>q;
  scanf("%d %d",&n,&M);  //珠子总数 和 种类数 
  for(int i=1;i<=M;i++){
      scanf("%d",&t);
      for(int j=1;j<=t;j++){
          cin>>a[++len].pos;
          a[len].kind=i;
      }
  }
  sort(a+1,a+n+1,cmp);
  for (int i=1;i<=n;i++){
        q.push(a[i]);
        m[a[i].kind]++;
        if (m[a[i].kind]==1) sum++;
        while (sum==M) { 
            Node E = q.back (), S = q.front ();   // right endpoint and left endpoint 
            IF (ANS> e.pos-S.POS) ANS = e.pos- S.POS; 
            Q .pop (); 
            m [s.kind] - ;
             IF (m [s.kind] == 0 ) sum-- ; 
        } 
    } 
    COUT << ANS; 
}

 

Guess you like

Origin www.cnblogs.com/phemiku/p/11403625.html