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

Platinum

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" , n - ans ) ;return 0 ;
} 

转载于:https://www.cnblogs.com/henry-1202/p/BZOJ5139.html

[BZOJ5139][Usaco2017 Dec]Greedy Gift Takers 权值线段树相关推荐

  1. bzoj5139 [Usaco2017 Dec]Greedy Gift Takers

    http://www.elijahqi.win/2018/02/06/bzoj5139/ 我这种智商低下曾经还不努力的人如果现在不努力,怕是没有大学上了 [题意] n个奶牛排成一排,顺次编号为1到n ...

  2. bzoj5139 [Usaco2017 Dec]Greedy Gift Takers(二分答案+模拟)

    首先我们发现如果第x头牛不能拿到礼物,则x之后的所有牛也不能拿到礼物.因此我们可以二分来找到这第一头不能拿到礼物的牛.满足什么条件的牛不能拿到礼物呢?我们预处理出每头牛拿到礼物之后会出现在哪里,如果在 ...

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

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

  4. 【bzoj2770】YY的Treap 权值线段树

    题目描述 志向远大的YY小朋友在学完快速排序之后决定学习平衡树,左思右想再加上SY的教唆,YY决定学习Treap.友爱教教父SY如砍瓜切菜般教会了YY小朋友Treap(一种平衡树,通过对每个节点随机分 ...

  5. 树套树 ----- P1975 [国家集训队]排队(树状数组套权值线段树求动态逆序对)

    解题思路: 首先我们知道交换两个数a[l]和a[r]a[l]和a[r]a[l]和a[r]影响到的区间是[l+1,r−1][l+1,r-1][l+1,r−1] 对于a[l]a[l]a[l],我们要减去[ ...

  6. 树套树 ---- 树状数组套权值线段树模板题 P2617 Dynamic Rankings 动态第K大

    题目链接 题目大意: 给你一个数组aaa,aaa有两个操作 询问aaa中[l,r][l,r][l,r]区间里面第kkk小的数是哪个? 修改axa_xax​为yyy 解题思路: 首先我们知道权值线段树是 ...

  7. codevs1688 求逆序对(权值线段树)

    1688 求逆序对  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果 题目描述 Description 给定一个序列a1,a2,-,an,如 ...

  8. 【HDU6701】Make Rounddog Happy【权值线段树+双向单调队列】

    题意:给你一个序列,求满足要求的子序列个数,其中要求为: 1.子序列的max-子序列长度len<=k 2.子序列中不出现重复的数字 题解:首先看到子序列max,很容易想到枚举最大值然后分治,这个 ...

  9. HDU-5249 KPI(STL or 权值线段树)

    题目链接 Problem Description 你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了很大的知名度.数十亿的请求被推到一个大管道后同时服务从管头拉取请求.让我们来定义每个请 ...

最新文章

  1. python 将excel文件转换为txt文件_python利用pandas将excel文件转换为txt文件的方法
  2. Android中attrs.xml文件的使用详解
  3. 20135127陶俊杰 实验一
  4. oracle行号排序问题
  5. 对于文件编码格式的浅显理解
  6. html onclick点击事件失效,HTML onfocus,onclick事件不起作用
  7. 肯德尔系数怎么分析_SPSS统计案例笔记16:相关分析之kendall系数
  8. 毕设论文-word格式问题
  9. filter grok 判断_logstash grok使用案例
  10. 三极管流水灯电路设计
  11. 如何看待小米发布的Redmi Note9系列手机?1299元起是否值得?
  12. 克隆vm虚拟机详细步骤
  13. 利用循环输出如下图形
  14. HZ视频配音配字幕工具v1.0配音配字幕、影视视频讲解利器
  15. 2.3.2 HMC硬件管理终端
  16. 【Moasure魔尺】优秀景观设计师们悄悄在用的测量设备
  17. mysql gmt格式_将MySQL数据库时区设置为GMT
  18. Java 航空系统设计
  19. Win11粘滞键怎么取消?
  20. Jmeter安装及配置教程

热门文章

  1. 【企业架构设计实战】应用架构设计
  2. 打码平台php源码,打码平台 远程打码 实例源码
  3. 奎因莫克拉斯基方法_超越源代码奎因和自我复制
  4. The SwiftProgramming Language读书笔记第26页
  5. MarkMind使用技巧
  6. iPhone连接mac电脑连接时反复连接与断开情况的处理
  7. ubuntu18.04安装openvino2022.1
  8. C++八股文分享---进程
  9. UVa514 Rails(铁轨)
  10. 思科 计算机网络 第6章测试考试 答案