题目描述

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==1) return 1;for(int i=1;i<mid;i++) b[i]=a[i];sort(b+1,b+mid);int base=n-mid;for(int i=1;i<mid;i++){if(b[i]>base) return 0;base++;}return 1;
}int main(){scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",a+i);l=1,r=n;while(l<=r){mid=l+r>>1;if(solve()) ans=mid,l=mid+1;else r=mid-1;}printf("%d\n",n-ans);return 0;
}

 

转载于:https://www.cnblogs.com/JYYHH/p/8401237.html

[USACO17DEC]Greedy Gift Takers相关推荐

  1. P4090 [USACO17DEC]Greedy Gift Takers

    题目链接 题意分析 首先 如果当前序列中一头奶牛拿不到礼物的话 那么他后面的奶牛也拿不到礼物 所以我们可以二分 由于可以操作无限次 所以我们对于当前\([1,mid)\)的奶牛按照\(c\)值排序之后 ...

  2. Greedy Gift Takers

    Farmer John's nemesis, Farmer Nhoj, has N cows (1≤N≤105 ), conveniently numbered 1...N .They have un ...

  3. [BZOJ5139][Usaco2017 Dec]Greedy Gift Takers 权值线段树

    Description Farmer John's nemesis, Farmer Nhoj, has NN cows (1≤N≤10^5), conveniently numbered 1-N. T ...

  4. USACO 2017 December Contest Platinum T3: Greedy Gift Takers

    题目大意 有 N(1≤N≤1e5)头牛按顺序排成一列,编号从 1 到 N,1 号牛在队头,N 号牛在队尾. 每次位于队头的牛 i 拿到一个礼物,然后插入到从队尾数ci​头牛之前的位置..举个栗子: 初 ...

  5. bzoj5139 [Usaco2017 Dec]Greedy Gift Takers

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

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

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

  7. [USACO 2017DEC] Greedy Gift Takers

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5139 [算法] 二分答案 时间复杂度 : O(NlogN^2) [代码] #incl ...

  8. USACO Training Section 1.1 贪婪的送礼者Greedy Gift Givers

    P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers 题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一 ...

  9. Greedy Gift Givers

    原题地址 Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange g ...

最新文章

  1. ARC在Release与Debug模式中内存释放的坑
  2. openstack-Mitaka Glance上传镜像报错
  3. OpenCV学习笔记(五):Mat结构
  4. java如何开发bpm系统_java工作流bpm开发ERP实例
  5. 线性表之顺序表与单链表的区别及优缺点
  6. linux 脚本 发送邮件,shell 脚本发送邮件
  7. 国内成品油价近期历次调整一览
  8. 在Dockerfile中安装本地whl包
  9. yolov3前向传播(三)-- 坐标转换,iou计算,权重加载,图片显示
  10. OpenCV轮廓vectorvector
  11. [LeetCode][easy]Create Target Array
  12. 找高手用c语言帮写一个qpsk调制解调的程序,QPSK调制解调完整程序配有自己的注释...
  13. led屏背后线路安装图解_弱电工程LED屏安装工程施工方案
  14. Win10 安装 Ubuntu 使用 Linux 教程
  15. 【BP回归预测】鲸鱼算法优化BP神经网络回归预测(多输入单输出)【含Matlab源码 1554期】
  16. mysql全称量词_MySQL操作记录的方法集合,供以后查看
  17. taro开发微信小程序配置全局appid
  18. 设计分享|基于51单片机的万年历(汇编)
  19. GBT25000.51-2016信息安全特性解读
  20. 好看的网站正在升级维护中html单页源码

热门文章

  1. 【老鸟进阶】deepfacelab合成参数详解
  2. Falsehoods Programmers Believe About Names
  3. Noteability的一些使用小技巧
  4. ubuntu设置网卡默认启动_ubuntu 网络配置
  5. linux配置selinux为许可模式,SELinux安全配置,详细说明
  6. 数字技术使智能视频监控超越传统模拟系统
  7. 计算机基础知识图谱,基于知识图谱的大学计算机基础习题推荐方法技术
  8. 形态等位点对迭代次数的贡献
  9. 计算机java用什么收题_JAVA是一种______。 A.计算机语言 B.计算机没备 C.数据库 D.应用软件...
  10. py3+requests+urllib+bs4+threading,爬取斗图图片