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. 浅谈CSRF攻击方式(转)

    add by zhj: 在看Django开发的应用时,看到了CSRF,然后搜到了这篇文章,讲的不错.其实CSRF 攻击也蛮简单的.当你登陆网站A后,会在本地存有cookie,在cookie没有过期的情 ...

  2. Javascript修正this的引用

    1.使用this作为参数来传递给函数.因为this始终与当前对象一致. <!DOCTYPE html> <html> <head> <meta charset ...

  3. 微软官方出的各种dll丢失的修复工具

    例如 :因为计算机中丢失 api-ms-win-crt-runtime-l1-1-0.dll.尝试重新安装该程序以解决此问题. 软件名称: Visual C++ Redistributable for ...

  4. 教你在Android手机上使用全局代理

    前言:在Android上使用系统自带的代理,限制灰常大,仅支持系统自带的浏览器.这样像QQ.飞信.微博等这些单独的App都不能使用系统的代理.如何让所有软件都能正常代理呢?ProxyDroid这个软件 ...

  5. [vue]组件的创建(componet)和销毁(keep-alive缓存)和父子dom同步nextTick

    思路: 1. 组件的好处,重用性 2. 组件对的slot用法 3. 子如何调用父的数据 4. 子如何触发父的方法执行 5. 父如何触发子的方法执行 6. 如何创建组件和销毁自建--如何缓存避免每次切换 ...

  6. 前m大的数(哈希入门)&&sort

    http://acm.hdu.edu.cn/showproblem.php?pid=1280 普通方法(625ms) #include <stdio.h> #include <str ...

  7. [LeetCode] 161. One Edit Distance_Medium

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  8. webpack2

    中文网址:http://www.css88.com/doc/webpack2/guides/installation/

  9. Lintcode: Lowest Common Ancestor

    Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes ...

  10. 通俗理解RxJS(一)

    自学 Rx 快有一个周了, 它非常适合处理复杂的异步场景.结合自己所学,决定写系列教程. 我认为, Rx 中强大的地方在于两处 管道思想,通过管道,我们订阅了数据的来源,并在数据源更新时响应 . 强大 ...