[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 ...
随机推荐
- 常见编程语言对REPL支持情况小结
最近跟一个朋友聊起编程语言的一些特性,他有个言论让我略有所思:“不能REPL的都是渣”.当然这个观点有点偏激,但我们可以探究一下,我们常用的编程语言里面,哪些支持REPL,哪些不支持,还有REPL的一 ...
- angular js的Inline Array Annotation的理解
inline Array annotation的形式是: someModule.controller('MyController', ['$scope', 'greeter', function($s ...
- netty学习指南
这段时间领导让我熟悉Socket开发,我花了三周时间左右去学习相关的知识,包括Java socket开发,重点学习了netty这个异步非阻塞通信框架. 在这里把我学习过程中遇到的有用资料整理了,供大家 ...
- 【BZOJ3624】【APIO2008】免费道路 [生成树][贪心]
免费道路 Time Limit: 2 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Input Output Sampl ...
- noip2014 提高组
T1 生活大爆炸版 石头剪刀布 题目传送门 就是道模拟题咯 #include<algorithm> #include<cstdio> #include<cstring&g ...
- 【洛谷 P1666】 前缀单词 (Trie)
题目链接 考试时暴搜50分...其实看到"单词","前缀"这种字眼时就要想到\(Trie\)的,哎,我太蒻了. 以一个虚点为根,建一棵\(Trie\),然后\( ...
- Logistic函数
Logistic函数(又称sigmoid函数) Logistic函数或Logistic曲线是一种常见的S形函数,它是皮埃尔·弗朗索瓦·韦吕勒在1844或1845年在研究它与人口增长的关系 ...
- JSP的简便写法
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMIAAAFmCAIAAACwedIqAAAWfklEQVR4nO2d7XPV1p3H77+x+3JfpJ ...
- python3 购物车练习
# 购物车# 功能要求:# 要求用户输入总资产,例如:2000# 显示商品列表,让用户根据序号选择商品,加入购物车# 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功.# 可充值.某商品 ...
- LeetCode 4 Median of Two Sorted Array
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...