BZOJ-1293: [SCOI2009]生日礼物(离散化+单调队列)

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std ;
#define inf 0x7fffffff
#define maxn 1000100
#define maxk 101
struct node {
    int v , t ; 
    void oper( int _v , int _t ) {
        v = _v , t = _t ;
    }
    bool operator < ( const node &a ) const {
        return t < a.t ;
    }
} a[ maxn ] , q[ maxn ] ;
int n , k , cnt[ maxk ] , counter = 0 , m = 0 , head = 1 , tail = 0 , ans = inf ;
int main(  ) {
    scanf( "%d%d" , &n , &k ) ;
    for ( int i = 0 ; i ++ < k ; ) {
        int x ; scanf( "%d" , &x ) ;
        while ( x -- ) {
            int pos ; scanf( "%d" , &pos ) ;
            a[ ++ m ].oper( i , pos ) ;
        }
    }
    sort( a + 1 , a + m + 1 ) ;
    memset( cnt , 0 , sizeof( cnt ) ) ;
    for ( int i = 1 , j ; i <= n ; ) {
        for ( j = i ; a[ j + 1 ].t == a[ j ].t && j < n ; ++ j ) ;
        for ( int h = i ; h <= j ; ++ h ) {
            q[ ++ tail ] = a[ h ] ;
            if ( ! ( cnt[ a[ h ].v ] ++ ) ) ++ counter ;
        }
        for ( ; cnt[ q[ head ].v ] > 1 ; -- cnt[ q[ head ++ ].v ] ) ;
        if ( counter == k ) ans = min( ans , a[ i ].t - q[ head ].t ) ;
        i = j + 1 ;
    }
    printf( "%d\n" , ans ) ;
    return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值