[USACO17DEC]Greedy Gift Takers
题目描述
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.
输入输出样例
3
1 2 0
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 ;
}
[USACO17DEC]Greedy Gift Takers的更多相关文章
- P4090 [USACO17DEC]Greedy Gift Takers
题目链接 题意分析 首先 如果当前序列中一头奶牛拿不到礼物的话 那么他后面的奶牛也拿不到礼物 所以我们可以二分 由于可以操作无限次 所以我们对于当前\([1,mid)\)的奶牛按照\(c\)值排序之后 ...
- NC24083 [USACO 2017 Dec P]Greedy Gift Takers
NC24083 [USACO 2017 Dec P]Greedy Gift Takers 题目 题目描述 Farmer John's nemesis, Farmer Nhoj, has N cows ...
- [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 ...
- [USACO 2017DEC] Greedy Gift Takers
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5139 [算法] 二分答案 时间复杂度 : O(NlogN^2) [代码] #incl ...
- USACO . Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...
- 119 - Greedy Gift Givers
Greedy Gift Givers The Problem This problem involves determining, for a group of gift-giving frien ...
- USACO Section 1.1-2 Greedy Gift Givers
Greedy Gift Givers 贪婪的送礼者 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少. 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那 ...
- Section 1.1 Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends hasdecided to exchange gifts o ...
- 1.1.4 PROB Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...
随机推荐
- 【COGS 2051】王者之剑 最小割
这个其实就是在说明相邻的点不能取,我们发现只要其满足这个条件他总能走出来,那么我们就最小割就是了,我们先黑白染色,S 一排黑点 一排白点 T 对于相邻的点我们就直接中间连INF,于是就满足只要一个点选 ...
- git使用笔记(一)入门
By francis_hao Nov 17,2016 本来是想把git的使用笔记写在一个文件里,但是越写越长,最后也不得不分开了.这样也好,每一篇一个侧重,可以写的详细一点. 初学乍练 在l ...
- TCP ------ RST的产生
产生RST的几个原因 1.请求超时 有89.27两台主机.主机89向主机27发送了一个SYN,表示希望连接8888端口,主机27回应了主机89一个SYN表示可以连接.但是主机89莫名其妙的发送了一个R ...
- nginx 设置client header 的大小与400错误
nginx默认的header长度上限是4k,如果超过了这个值 如果header头信息请求超过了,nginx会直接返回400错误可以通过以下2个参数来调整nginx的header上限 client_he ...
- 【LibreOJ】【LOJ】#6220. sum
[题意]对于n个数,找出一些数使得它们的和能被n整除,输出任意一组方案,n<=10^6. [算法]构造/结论 [题解]引用自:http://www.cnblogs.com/Sakits/p/74 ...
- Codeforces Round #469 Div. 2 A B C D E
A. Left-handers, Right-handers and Ambidexters 题意 \(l\)个左撇子,\(r\)个右撇子,\(a\)个两手均可.要组成一支队伍,里面用左手的人数与用右 ...
- web前端 CSS基础
简单的CSS文件 <style type="text/css"> a{ color:rebeccapurple; font-size: larger; font-wei ...
- The Clean Architecture--一篇很不错的关于架构的文章
翻译水平有限,见谅! Uncle Bob 13 Aug 2012 Architecture Craftsmanship Over the last several years we’ve seen a ...
- For循环中不可以嵌套RDD操作
今天犯了一个致命理解错误,Spark中的RDD Map操作只是一个计算式的传递,并不是Action,也就是在for循环中不会产生真正的计算. 因此,如果for循环中出现了RDD的Map类似操作,都会引 ...
- 用js和jQuery做轮播图
Javascript或jQuery做轮播图 css样式 <style> a{ text-decoration:none; } .naver{ width: 100%; position:r ...