Greedy Gift Takers
Farmer John's nemesis, Farmer Nhoj, has N cows (1≤N≤105 ), conveniently numbered 1...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 t the head of the queue and cow 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 will always cut exactly Ci cows (0≤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.
INPUT FORMAT (file greedy.in):
The first line contains a single integer, N.
The second line contains N space-separated integers C1,C2,C3...CN.
OUTPUT FORMAT (file greedy.out):
Please output the number of cows who cannot receive any gifts.
SAMPLE INPUT:
3
1 2 0
SAMPLE OUTPUT:
1
翻译:
农场主Nhoj是农场主John的死对头,他有N头奶牛(1≤ N ≤ 105),为了记录方便,给奶牛们从1 … N的顺序编了号。这些奶牛很意外的出现了在John的农场里,所以这位彬彬有礼的农场主John打算给这些奶牛送点礼物。
为此,农夫John准备了无限量的礼物给那些奶牛,而Nhoj的奶牛们也已经在他的面前排好队了,奶牛1排在队伍的最前面,奶牛N排在队伍的最后。农夫John希望这些奶牛可以按顺序拿好礼物,然后再按顺序从队伍的前头排到队伍的最后。然而,他刚刚才意识到Nhoj的奶牛们并不会按照他想的那样行动。而是在拿到礼物后,每头牛都不一定会排到队伍的最后,甚至可能在队伍的后方插队。具体点说就是奶牛i总是会插在Ci头牛的前边(0 ≤ ci≤ N-1)。
农夫John并不担心有些牛可能会收到多份的礼物,因为他准备了无限多的礼物,但是,他担心有些牛可能会因为没有收到任何礼物而不开心。
请帮助农民John计算一下在送出的礼物数量没有限制的情况下,收不到任何礼物的牛的数量。
输入格式(file greedy.in):
第一行填写一个整数N。
第二行填写N个用空格分开的整数c1,c2, … , cN
输出格式(file greedy.out):
请输出不能收到任何礼物的牛的数量。
#include<bits/stdc++.h> #define ll long long #define mode 1000000007 using namespace std; int x[100010],y[100010]; bool cmp(int a,int b) { return a>b; } int check(int a) { int b=a; for(int i=1; i<b; i++) { y[i]=x[i]; } sort(y+1,y+b,cmp); for(int i=1; i<b; ++i) { if(y[i]<a) return false; a--; } return true; } int main() { int n; scanf("%d",&n); for(int i=1; i<=n; ++i) { scanf("%d",&x[i]); x[i]=n-x[i]; } int l=1,r=n,mid,ans=1; while(l<=r) { mid=(l+r)/2; if(check(mid)) { ans=mid; l=mid+1; } else r=mid-1; } cout<<n-ans<<endl; return 0; }
https://www.sohu.com/a/211375775_641382
https://www.cnblogs.com/JYYHH/p/8401237.html