题目描述

Farmer John's nemesis, Farmer Nhoj, has NN cows (1 \leq N \leq 10^51≤N≤105 ), conveniently numbered 1 \dots N1…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 cows have queued up in front of him, with cow 11 at the head of the queue and cow NN at the tail. Farmer John was expecting that at every timestep, the cow at the head of the queue would take a gift from Farmer John and go to the tail of the queue. However, he has just realized that Nhoj's cows are not that polite! After receiving her gift, each cow may not go to the tail of the queue, but rather may cut some number of cows at the tail, and insert herself in front of them. Specifically, cow ii will always cut exactly c_ici​ cows (0 \leq c_i \leq N-10≤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 receive any gifts, no matter how many gifts are handed out.

有 N(1≤N≤10^51≤N≤105 )头牛按顺序排成一列,编号从 1 到 N,1 号牛在队头,N 号牛在队尾。

每次位于队头的牛 i 拿到一个礼物,然后插入到从队尾数c_ici​ 头牛之前的位置。举个栗子: 初始队列 1,2,3,4,5 c_1c1​ = 2,c_2c2​ = 3,则第一次操作后的序列为 2,3,4,1,5,第二次操作后的序列为 3,2,4,1,5。重复无限次操作,求最后有几头牛拿不到礼物。

输入输出格式

输入格式:

The first line contains a single integer, NN .

The second line contains NN space-separated integers c_1, c_2, \dots, c_Nc1​,c2​,…,cN​ .

输出格式:

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

输入输出样例

输入样例#1:

3
1 2 0
输出样例#1:

1

下午强行拉姜sir陪我去打球hhhh
虽然一个月没运动啦打了一会就累趴了www,但是出汗的感觉超级棒!!! 好了不瞎扯了赶紧说题解。。。。
(我在纸上验算了半天才找出规律,,没救了)
首先发现如果一个奶牛拿不到糖的话,它后面的肯定也都拿不到,因为这个拿不到糖的会一直挡在它们的前面。
然后前面能拿到糖的就会构成某种py循环。。。
这显然具有二分性。 但是当我们二分到一个位置的时候,怎么确定这个位置是否能拿到糖呢???
注意只有当它被"推"到第一位的时候才能拿到糖,也就是说这个过程里它前面的牛都要逐渐到它的后面去才行。
发现第一个拿到糖的且c值<=n-mid(二分的位置)的牛会到它的后面,然后我们二分的牛会前进一步。
本来c值=n-mid+1的牛是正好到我们二分的牛前面的,而现在则可以到它后面的了。。 所以我们在前面找牛的时候肯定是优先找c值小的,因为这样可以尽量让我们二分的牛前进(你可以看成一种贪心)。
当然会有c值较大的比较小的先到我们二分的牛后面,但是顺便是没有关系的,因为这样最后的结果都是让我们二分的牛前进了两格。 于是就可以得到一个O(N*log^2(N))的算法(虽然不知道能不能再优化但是已经能水过这个题了hhhh)
#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
using namespace std;
int n,m,a[maxn],b[maxn];
int l,r,mid,ans; inline bool solve(){
if(mid==) return ;
for(int i=;i<mid;i++) b[i]=a[i];
sort(b+,b+mid); int base=n-mid;
for(int i=;i<mid;i++){
if(b[i]>base) return ;
base++;
} return ;
} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",a+i);
l=,r=n;
while(l<=r){
mid=l+r>>;
if(solve()) ans=mid,l=mid+;
else r=mid-;
} printf("%d\n",n-ans);
return ;
}
 

最新文章

  1. mssql查询列名中包含特定字段的列
  2. 大商创 sql追踪 卖家入驻
  3. nova.conf部分参数解析
  4. BMP图像格式
  5. 基于Schema的AOP 配置使用详解
  6. angular-ui-bootstrap插件API - Tabs
  7. idea代码调试debug篇
  8. SEO—Meta标签优化
  9. Docker 生态概览
  10. 【原】使用less实现随机下雪动画
  11. ServiceStack.Redis遇到的问题:ServiceStack.Redis.Generic.RedisTypedClient`1”的方法“get_Db”没有实现。
  12. flask设置cookie,设置session,模拟用户认证、模拟管理后台admin、模拟用户logout
  13. position实现分层和遮罩层功能
  14. url地址 参数 带 参数 注意事项 , chain , redirect , redirectAction
  15. BugPhobia开发篇章:绩效管理的层次优化
  16. linux文件与目录管理命令(ubuntu)
  17. iWebShop安装教程
  18. jqgrid 编辑表格(包含下拉框)
  19. Cocos2d-x使用Luajit将Lua脚本编译为bytecode,从而实现加密
  20. Cef应用程序结构

热门文章

  1. js保存用户名与密码
  2. MySQL 数据库性能优化之缓存参数优化
  3. 买卖股票的最佳时机 [ leetcode ]
  4. canvas知识01
  5. CodeSmith和PowerDesigner (转)
  6. HDU2066一个人的旅行---(多起点多终点最短路径)
  7. RPC-Thrift(四)
  8. 【LA3487】最小割-经典模型 两种方法
  9. [POJ1595]欧拉线性筛(虽然这道题不需要...)
  10. Spring JdbcTemplate框架搭建及其增删改查使用指南