CodeForces1154E

CodeForces1154E
meaning of the questions is to have two coaches, every coach in turn operation, each operation will select all students not selected in the ability of the highest value that a student left and the right of each \ (k \) students selected go.
If less than \ (k \) months on all selected to go two coaches take turns selecting until all students have been selected to go so far. each student is output from the last election which a coach is gone, the first set for the selection of coaches \ (1 \) , and the other is \ (2 \) .
If the simulation directly in accordance with the meaning of problems, complexity is \ (O (n ^ 2) \) , in the face \ (n \ le 2 \ times 10 ^ 5 \) of the data size is obviously powerless.
we consider, if there is no left or right, respectively, then select \ (k \) months, you can use the heap to maintain direct, each taking top of the heap on the line. so add that one step further can a heap to maintain it?
the answer seems to be, if we try to maintain the heap, then this problem because the special operation, we need to maintain the position of each number in the pile.
such an obvious idea is in the crowd pressed into a number of \ (pair \) , due to the \ (pair \) comparing the default is the first dimension, that is, \ (first \) for the first keyword, so we do not Should override operator.
Then press-fit \ (pair \)Then what? There is a very obvious problem is that a student was selected to go, but it could still pile, later if we take the top of the heap might take him. This problem can not handle, marking can be resolved. So how to play this mark it?
If every direct \ (for \) again, it is clear that \ (TLE \) of a pair of considering how to optimize the complexity of the tag.
obviously, each student will only be selected once, in other words in fact, we mark each student played only once is enough, and this complexity is clearly right.
then begin to consider how to avoid duplication. a more natural idea is to maintain each student left to right first position one has not been selected students.
here, and ideas on many broadened. this thing you can doubly linked list maintenance can be disjoint-set maintenance, it can also directly maintain each number predecessor successor, after each select students updated on the line a.
we opened two stacks, each time a student selected to go put pressure being selected to go into the second pile, before taking the top of the heap, first determine if two stacks are equal top of the heap, then that is now top of the heap we have chosen to go, keeps popping up that is .
Here the second reactor in fact equivalent marking role.
Such complexity ... I do not analyze, but you will only have to traverse each student again, so complexity is correct, and because the heap exist, so the overall complexity is probably \ (O (n log_2 n) \) is not clear, anyway? \ (O (\: can \: over \:) \)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
#define per(i,a,b) for (int i = a ; i >= b ; -- i)
#define pii pair < int , int >
#define X first
#define Y second
#define rint read<int>
#define int long long
#define pb push_back

using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;

template < class T >
    inline T read () {
        T x = 0 , f = 1 ; char ch = getchar () ;
        while ( ch < '0' || ch > '9' ) {
            if ( ch == '-' ) f = - 1 ;
            ch = getchar () ;
        }
        while ( ch >= '0' && ch <= '9' ) {
            x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
            ch = getchar () ;
       }
   return f * x ;
}

const int N = 2e5 + 100 ;

priority_queue < pii > q , d ;
int n , k , v[N] , pre[N] , suf[N] ;
int cnt1 , cnt2 , pos1 , pos2 ;
bool coach , bel[N] ;

signed main (int argc , char * argv[] ) {
    n = rint () ; k = rint () ; coach = false ;
    rep ( i , 1 , n ) { v[i] = rint () ; pre[i] = i - 1 ; suf[i] = i + 1 ; q.push ( { v[i] , i } ) ; }
    while ( ! q.empty () ) {
        while ( ! d.empty () && ! q.empty () && d.top () == q.top () ) d.pop () , q.pop () ;
        if ( q.empty () ) break ; pii tmp = q.top () ; pos1 = pos2 = tmp.Y ;
        for (cnt1 = 1 , pos1 = suf[pos1] ; cnt1 <= k && pos1 ; ++ cnt1 , pos1 = suf[pos1]) d.push ( { v[pos1] , pos1 } ) , bel[pos1] = coach ;
        for (cnt2 = 1 , pos2 = pre[pos2] ; cnt2 <= k && pos2 ; ++ cnt2 , pos2 = pre[pos2]) d.push ( { v[pos2] , pos2 } ) ,  bel[pos2] = coach ;
        pre[pos1] = pos2 ; suf[pos2] = pos1 ; bel[tmp.Y] = coach ; coach ^= 1 ; q.pop () ;
    }
    rep ( i , 1 , n ) printf ("%d" , ((int)bel[i]) + 1 ) ; 
    system ("pause") ; return 0 ;
}

Guess you like

Origin www.cnblogs.com/Equinox-Flower/p/11411703.html