zoukankan      html  css  js  c++  java
  • [BZOJ5139][Usaco2017 Dec]Greedy Gift Takers 权值线段树

    Description

    Farmer John's nemesis, Farmer Nhoj, has NN cows (1≤N≤10^5), conveniently numbered 1…N. They have 
    unexpectedly turned up at Farmer John's farm, so the unfailingly polite Farmer John is attempting to
     give them gifts.To this end, Farmer John has brought out his infinite supply of gifts, and Nhoj's c
    ows have queued up in front of him, with cow 11 at the head of the queue and cow N at the tail. Farm
    er John was expecting that at every timestep, the cow at the head of the queue would take a gift fro
    m Farmer John and go to the tail of the queue. However, he has just realized that Nhoj's cows are no
    t that polite! After receiving her gift, each cow may not go to the tail of the queue, but rather ma
    y cut some number of cows at the tail, and insert herself in front of them. Specifically, cow ii wil
    l always cut exactly cici cows (0≤ci≤N-1).Farmer John knows that some cows might receive multiple 
    gifts; as he has an infinite supply, this does not worry him. But he is worried that some cows might
     become unhappy if they do not get any gifts.Help Farmer John find the number of cows who never rece
    ive any gifts, no matter how many gifts are handed out.
    有 N (1 <= N <= 10^5)头牛按顺序排成一列,编号从1到N,1 号牛在队头,N 号牛在队尾。每次位于队头的牛 i 
    拿到一个礼物,然后插入到从队尾数 c_i 头牛之前的位置。举个栗子: 初始队列 1 2 3 4 5, c_1 = 2,c_2 = 
    3,则第一次操作后的序列为 2 3 4 1 5,第二次操作后的序列为 3 2 4 1 5 。重复无限次操作,求最后有几头牛
    拿不到礼物。
     

    Input

    The first line contains a single integer, N.
    The second line contains N space-separated integers c1,c2,…,cN
     

    Output

    Please output the number of cows who cannot receive any gifts.
     

    Sample Input

    3
    1 2 0

    Sample Output

    1

    HINT

     

    Source

    Solution

    做法:权值线段树

    后面的牛不会领到礼物当且仅当前面形成了循环节

    所以找出最小的循环节就是答案了

    设$a_i = n - c_i $,$x=max(b_i)$($b_i$在循环节中),形成循环节的条件就是牛的数量大于等于$x$(这个在纸上画画图就出来了)

    枚举这个$x$,然后在$b_i<=x$的$b_i$中找第$x$小的,这个操作可以用权值线段树维护

    #include <bits/stdc++.h>
    
    using namespace std ;
    
    #define lc ( rt << 1 | 1 )
    #define rc ( rt << 1 )
    const int N = 1e5 + 10 ;
    
    int n ;
    int c[ N ] , t[ N << 2 ] ;
    int ans ;
    vector < int > vt[ N ] ;
    
    void add( int l , int r , int rt , int x ) {
        t[ rt ] ++ ;
        int mid = ( l + r ) >> 1 ;
        if( l == r ) return ;
        if( x <= mid ) add( l , mid , lc , x ) ;
        else add( mid + 1 , r , rc , x ) ;
    }
    
    int query( int l , int r , int rt , int x ) {
        if( l == r ) return l ;
        int mid = ( l + r ) >> 1 ;
        if( t[ lc ] >= x ) return query( l , mid , lc , x ) ;
        else return query( mid + 1 , r , rc , x - t[ lc ] ) ;
    }
    
    int main() {
        scanf( "%d" , &n ) ;
        for( int i = 1 ; i <= n ; i ++ ) scanf( "%d" , &c[ i ] ) , vt[ n - c[ i ] ].push_back( i ) ;
        ans = n ;
        for( int i = 1 ; i <= n ; i ++ ) {
            for( int j = 0 , len = vt[ i ].size() ; j < len ; j ++ ) add( 1 , n , 1 , vt[ i ][ j ] ) ;
            if( t[ 1 ] >= i ) ans = min( ans , query( 1 , n , 1 , i ) ) ;
        }
        printf( "%d
    " , n - ans ) ;
        return 0 ;
    } 
  • 相关阅读:
    php redis 操作大全
    迟到的2017读书计划
    将博客搬至CSDN
    maven安装教程
    liunx centOS6.5安装jdk教程
    centOS6.0虚拟机ip配置
    html网页调用本地exe程序的实现方法:
    org.springframework.beans.factory.BeanCreationException 解决异常错误
    sqlserver查看锁表进程及对锁定的表进行解锁
    tomcat免安装版做成windows系统服务
  • 原文地址:https://www.cnblogs.com/henry-1202/p/BZOJ5139.html
Copyright © 2011-2022 走看看