题面:
Lemonade Line
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
Problem Description
It’s a hot summer day out on the farm, and Farmer John is serving lemonade to his N cows! All N cows (conveniently numbered 1…N) like lemonade, but some of them like it more than others. In particular, cow i is willing to wait in a line behind at most wi cows to get her lemonade. Right now all N cows are in the fields, but as soon as Farmer John rings his cowbell, the cows will immediately descend upon FJ’s lemonade stand. They will all arrive before he starts serving lemonade, but no two cows will arrive at the same time. Furthermore, when cow i arrives, she will join the line if and only if there are at most wi cows already in line.
Farmer John wants to prepare some amount of lemonade in advance, but he does not want to be wasteful. The number of cows who join the line might depend on the order in which they arrive. Help him find the minimum possible number of cows who join the line.
Input
The first line contains N, and the second line contains the N space-separated integers w1,w2,…,wN. It is guaranteed that 1≤N≤1e5, and that 0≤wi≤1e9 for each cow i.
Output
Print the minimum possible number of cows who might join the line, among all possible orders in which the cows might arrive.
Sample Input
5
7 1 400 2 2
Sample Output
3
题意描述
每头牛能容忍它前面有一定数量的牛,如果前面牛的数量大于它的容忍数量,它就不会进入队列排队.John想给出最少数量的柠檬汽水,求出能拿到柠檬汽水的牛的最少数量.
题目分析
因为牛一定会排队,但可以控制先后入队顺序.所以,让容忍数量大的牛先进队伍中,容忍数量小的牛后进队伍中,这样就会导致部分牛最后根本无法进入队伍,所以John给出的柠檬汽水就是最少的了.
具体代码
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int cow[100005];
int main()
{
int n;
cin >> n ;
for(int i = 1; i <= n; i++)
{
cin >> cow[i] ;
}
sort(cow+1 , cow+1+n);//排序(虽然从小到大,但入队牛从后面开始选取就可以了
int cnt = 0;
while(cow[n] >= cnt && n > 0)//边界条件(有可能全部牛的容忍程度都>=n
{
cnt++;
n--;
}
cout << cnt << endl;
return 0;
}