Wi Know-- thinking (segment tree assist)

Transfer:

https://cn.vjudge.net/problem/Kattis-wiknow

Meaning of the questions:

Get shaped like ABAB sequence in the string, requiring minimal AB lexicographically.

Ideas:

The most important property of this question: If the sequence is present in the form ABAB, then we can find a A1B1A2B2, meet no other between A A1A2, B1B2 B. no other between

Each character to the right of a first pre-position. String scan it again, the right of each character to join the tree line, while the enumeration B1, between B1, B2 range minimum query, this minimum must B1 appeared on the left, because the update segment tree is added to each character right side.

code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef pair<int,int> P;
 4 const int N=400000+10;
 5 const int INF=0x3f3f3f3f;
 6 int dat[4*N];
 7 int a[N],nex[N],r[N],n;
 8 void update(int pos,int k,int l,int r)
 9 {
10     if(r-l==1){dat[k]=a[pos];return;}
11     int m=(l+r)>>1;
12     if(pos<m)update(pos,k<<1,l,m);
13     else update(pos,(k<<1)|1,m,r);
14     dat[k]=min(dat[k<<1],dat[(k<<1)|1]);
15 }
16 int query(int a,int b,int k,int l,int r)
17 {
18     if(l==r)return INF;
19     if(a<=l&&b>=r)return dat[k];
20     int m=(l+r)>>1,mi=INF;
21     if(a<m)mi=min(mi,query(a,b,k<<1,l,m));
22     if(b>m)mi=min(mi,query(a,b,(k<<1)|1,m,r));
23     return mi;
24 }
25 int main()
26 {
27     memset(dat,0x3f,sizeof(dat));
28     scanf("% D " , & n-);
 29      for ( int I = . 1 ; I <= n-; ++ I)
 30          Scanf ( " % D " , A + I);
 31 is      for ( int I = n-; I> = . 1 ; Inc. (www.i-levelmedia.com)) // position of a character to the right of each process 
32      {
 33 is          NEX [I] = R & lt [a [I]];
 34 is          R & lt [a [I]] = I;
 35      }
 36      P ANS (INF, INF );
 37 [      for ( int I = . 1 ; I <= n-; ++ I)
 38 is     {
 39          IF (NEX [I]!) Continue ;
 40          int V = Query (I + . 1 , NEX [I], . 1 , . 1 , n-+ . 1 ); // query the minimum between the current position and the next position 
41 is          ANS = min (ANS, the make_pair (V, a [I]));
 42 is          update (NEX [I], . 1 , . 1 , n-+ . 1 ); // the current character under an update into the segment tree 
43 is      }
 44 is      IF (ANS. INF || ans.second == == First INF)
 45          the puts ( " -1 " );
 46 is      the else printf("%d %d\n",ans.first,ans.second);
47     return 0;
48 }
View Code

 

 

Guess you like

Origin www.cnblogs.com/judp/p/11330758.html