[USACO17DEC]Greedy Gift Takers

Posted 蒟蒻JHY

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO17DEC]Greedy Gift Takers相关的知识,希望对你有一定的参考价值。

题目描述

Farmer John‘s nemesis, Farmer Nhoj, has NN cows (1 \leq N \leq 10^51N105 ), conveniently numbered 1 \dots N1N . 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-10ci?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^51N105 )头牛按顺序排成一列,编号从 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;
}

 

 

以上是关于[USACO17DEC]Greedy Gift Takers的主要内容,如果未能解决你的问题,请参考以下文章

P1201-[USACO1.1]贪婪的送礼者Greedy Gift Givers

P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers

P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers

USACO 2017 December Contest Platinum T3: Greedy Gift Takers

P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers

洛谷P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers