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. Loadrunner 手动关联技术

    录制成功,回放失败,怀疑和动态数据有关: 1 重新录制一份脚本,两次录制的脚本进行比对,确定动态数据,复制动态数据: 2  找到第一次产生该动态数据的响应对应的相应请求: 1)  拷贝脚本中适当长度的 ...

  2. css内边距 边框

    /*1 元素的各边都有 10 像素的内边距 四个值上.右.下.左 两个上下,左右 三个值:上,左右,下*/ /*p {padding: 10%;}*/ h1 { padding-top: 10px; ...

  3. 前端js如何生成一个对象,并转化为json字符串

    https://www.cnblogs.com/May-day/p/6841958.html 一,直接上代码 <script src="../../Content/jquery-2.0 ...

  4. sdut2613(This is an A+B Problem)大数加法(乘法)

    #include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...

  5. win10 + cuda(v9.0) 安装TensorFlow-gpu版

    之前在实习公司的电脑上装过TensorFlow-gpu,那时候很快就装好了.但在自己的笔记本上装时,却搞了很久... 一部分原因是因为用校园网下载cuda toolkit 和cudnn ,总是在最后时 ...

  6. os模块的使用

    python编程时,经常和文件.目录打交道,这是就离不了os模块.os模块包含普遍的操作系统功能,与具体的平台无关.以下列举常用的命令 1. os.name()——判断现在正在实用的平台,Window ...

  7. VS2012提示突然没有了

    重置Visual Studio可以解决此问题, 方法是从开始->Microsoft Visual Studio 2012->Visual Studio  Tools->Visual ...

  8. 服务器返回的json数据中含有null的处理方法

    个人博客:http://guohuaden.com/2017/03/06/json-dataNull/因为有遇到过类似情况,所以就想到了一些解决方法,并且实践了一下,这里简单的做个记录. 注:有看到不 ...

  9. linux常用命令:find命令之xargs

    在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出 ...

  10. linux服务器---安装swat

    安装swat swat是一个图形化的samba管理软件,可以帮助不熟悉的人去灵活的配置samba服务, 1.安装swat [root@localhost wj]#yum install -y samb ...