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 + ; int n ;
int c[ N ] , t[ N << ] ;
int ans ;
vector < int > vt[ N ] ; void add( int l , int r , int rt , int x ) {
t[ rt ] ++ ;
int mid = ( l + r ) >> ;
if( l == r ) return ;
if( x <= mid ) add( l , mid , lc , x ) ;
else add( mid + , r , rc , x ) ;
} int query( int l , int r , int rt , int x ) {
if( l == r ) return l ;
int mid = ( l + r ) >> ;
if( t[ lc ] >= x ) return query( l , mid , lc , x ) ;
else return query( mid + , r , rc , x - t[ lc ] ) ;
} int main() {
scanf( "%d" , &n ) ;
for( int i = ; i <= n ; i ++ ) scanf( "%d" , &c[ i ] ) , vt[ n - c[ i ] ].push_back( i ) ;
ans = n ;
for( int i = ; i <= n ; i ++ ) {
for( int j = , len = vt[ i ].size() ; j < len ; j ++ ) add( , n , , vt[ i ][ j ] ) ;
if( t[ ] >= i ) ans = min( ans , query( , n , , i ) ) ;
}
printf( "%d\n" , n - ans ) ;
return ;
}

[BZOJ5139][Usaco2017 Dec]Greedy Gift Takers 权值线段树的更多相关文章

  1. BZOJ4777 [Usaco2017 Open]Switch Grass[最小生成树+权值线段树套平衡树]

    标题解法是吓人的. 图上修改询问,不好用数据结构操作.尝试转化为树来维护.发现(不要问怎么发现的)最小生成树在这里比较行得通,因为最近异色点对一定是相邻的(很好想),所以只要看最短的一条两端连着异色点 ...

  2. BZOJ 4777 Usaco2017 Open Switch Grass Kruskal+替罪羊树+权值线段树

    这道题首先可以看出答案一定是一条边,而且答案一定在最小生成树上,那么我们就可以在这个最小生成树上维护他与异色儿子的边最小值,所以我们就可以已通过Kruskal和一棵平衡树来解决,时间复杂度是O(n*l ...

  3. 2019.01.21 bzoj2441: [中山市选2011]小W的问题(树状数组+权值线段树)

    传送门 数据结构优化计数菜题. 题意简述:给nnn个点问有多少个www型. www型的定义: 由5个不同的点组成,满足x1<x2<x3<x4<x5,x3>x1>x2 ...

  4. Codeforces 558E A Simple Task(权值线段树)

    题目链接  A Simple Task 题意  给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...

  5. HDU 6464 /// 权值线段树

    题目大意: 共Q次操作 操作有两种 操作一 在序列尾部加入f[i]个s[i] 操作二 查询序列第f[i]小到第s[i]小之间的总和 离线操作 把序列内的值离散化 然后利用离散化后的值 在线段树上对应权 ...

  6. 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings

    谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...

  7. 【BZOJ-2892&1171】强袭作战&大sz的游戏 权值线段树+单调队列+标记永久化+DP

    2892: 强袭作战 Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 45  Solved: 30[Submit][Status][Discuss] D ...

  8. BZOJ 3110 ZJOI 2013 K大数查询 树套树(权值线段树套区间线段树)

    题目大意:有一些位置.这些位置上能够放若干个数字. 如今有两种操作. 1.在区间l到r上加入一个数字x 2.求出l到r上的第k大的数字是什么 思路:这样的题一看就是树套树,关键是怎么套,怎么写.(话说 ...

  9. 动态求区间K大值(权值线段树)

    我们知道我们可以通过主席树来维护静态区间第K大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...

随机推荐

  1. centos7 设置 静态IP

    centos7 图形设置 yum install NetworkManager-tui #centos7 nmtui edit eth0 #图形设置ip systemctl restart netwo ...

  2. javascript本地,宿主,内置对象

    一.本地对象:官方定义的对象独立于宿主环境的 ECMAScript 实现提供的对象,包括操作系统和浏览器.本地对象就是 ECMA-262 定义的类(引用类型).“本地对象”包含内容:   Object ...

  3. 松下 激光位移传感器 API

    一: /* ============================================================================================== ...

  4. 谁有stanford ner训练语料

    [冒泡]良橙(1759086270) 12:14:17请教大家一个问题,我有1w多句用户的问题,但是有些包含了一些骂人,数字,特殊符号,甚至,语句不通,有什么方法可以过滤不[吐槽]爱发呆的小狮子(19 ...

  5. windows下docker的安装并使用

    硬件虚拟化:硬件虚拟化是一种对计算机或操作系统的虚拟.虚拟化对用户隐藏了真实的计算机硬件,表现出另一个抽象计算平台. 打开任务管理器的性能查看是否支持虚拟化技术 下载windows docker ht ...

  6. Javascript-蔬菜运算价格

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. mysql buffer

    php与mysql的连接有三种方式,mysql,mysqli,pdo.不管使用哪种方式进行连接,都有使用buffer和不使用buffer的区别. 什么叫使用buffer和不使用buffer呢? 客户端 ...

  8. Git—使用方法

    1.:插件的安装(eclipse LUNA版本之后已经自动集成,不需要安装插件). * 先打开该网页提供了对应版本的EGit,自己选择相应的版本.(http://wiki.eclipse.org/EG ...

  9. React组件,React和生命周期

    笔记,具体可以看看这个博客: https://segmentfault.com/a/1190000004168886?utm_source=tag-newest react 的jsx document ...

  10. 强大的chrome(1)以acfun为例抓取视频

    chrome很强大,很强大,很强大. 想要了解他的强大呢,就先要掌握一些基本的chrome命令. 1. chrome://flags   可用来启用或者关闭某些chrome的体验特性   2. chr ...