LibreOJ#2362蚯蚓

蚯蚓

很奇妙的一道题.
显然的暴力:
用堆维护,好了,没了.
复杂度(Theta((n+m) imes log_2{(n+m)})),当然这个不紧,因为堆的大小不是每时每刻都是(n+m)的.
看起来是非常优秀的复杂度,但我们看数据范围:
(nle 10^5,mle 7 imes 10^6).
没错,它为了卡你一个(log)开到了(7e6),果然丧心病狂.
那么我们就需要一个线性的做法.
但怎么才能去线性维护呢?
考虑题目的要求:
每次选择一个最大的,切成两半,重新放入.
假如某次最大的是(x),那么我们考虑下一次的次大(y).
(x)切出的较小一半和较大一半分别为(a_1,b_1),
(y)切出的较小一半和较大的一半分别为(a_2,b_2).
那么...如果我们把原值,切出的较小值,切出的较大值分别放入三个队列的话,是不是这三个队列显然是有序的?
每次取的时候就比较三个队头取最大即可.
然后你就愉快地以(Theta(n+m))地复杂度做完了这道题.
(Code:)

#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 one first
#define two second
#define rint read<int>
#define int long long
#define pb push_back
#define db double

using std::queue ;
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

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 = 1e5 + 100 ;
const int M = 7e6 + 100 ;
queue < int > p , p1 , p2 ;
int n , m , q , u , v , t ;
int a[N] , delta , ans[M+N] , cnt ;

inline bool cmp (int x , int y) { return x >= y ; }

inline int getmax () {
    int x1 = - ( 1 << 30 ) , x2 = x1 , x3 = x1 ;
    if ( ! p.empty () ) x1 = p.front() ;
    if ( ! p1.empty () ) x2 = p1.front() ;
    if ( ! p2.empty () ) x3 = p2.front() ;
    if ( x1 >= x2 && x1 >= x3 ) { p.pop() ; return x1 ; }
    else if ( x2 >= x1 && x2 >= x3 ) { p1.pop() ; return x2 ; }
    p2.pop() ; return x3 ;
}

signed main (int argc , char * argv[]) {
    n = rint () ; m = rint () ; q = rint () ; u = rint () ; v = rint () ; t = rint () ;
    rep ( i , 1 , n ) a[i] = rint () ; sort ( a + 1 , a + n + 1 , cmp ) ;
    rep ( i , 1 , n ) p.push ( a[i] ) ;
    rep ( tim , 1 , m ) {
        int tmp = getmax () + delta ; if ( tim % t == 0 ) printf ("%lld " , tmp ) ;
        int div = tmp * u / v ; int remainder = tmp - div ; delta += q ;
        p1.push ( div - delta ) ; p2.push ( remainder - delta ) ;
    }
    int tmp = 0 ;
    while ( ! p.empty () || ! p1.empty () || ! p2.empty () ) ans[++cnt] = getmax () + delta ;
    puts ("") ;
    for (int i = t ; i <= cnt ; i += t) if ( ans[i] != - 1 ) printf ("%lld " , ans[i] ) ;
    return 0 ;
}
原文地址:https://www.cnblogs.com/Equinox-Flower/p/11638020.html