R3_D_Lemonade Line

Lemonade Line

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

It’s a hot summer day out on the farm, and Farmer John is serving lemonade to his NN cows! All NN cows (conveniently numbered 1…N1…N) like lemonade, but some of them like it more than others. In particular, cow ii is willing to wait in a line behind at most wiwi cows to get her lemonade. Right now all NN 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 ii arrives, she will join the line if and only if there are at most wiwi 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 NN, and the second line contains the NN space-separated integers w1,w2,…,wNw1,w2,…,wN. It is guaranteed that 1≤N≤1051≤N≤105, and that 0≤wi≤1090≤wi≤109 for each cow ii.

Output

Print the minimum possible number of cows who might join the line, among all possible orders in which the cows might arrive.

Example

input

Copy

5
7 1 400 2 2

output

Copy

3

Note

In this setting, only three cows might end up in line (and this is the smallest possible).

Suppose the cows with w=7w=7 and w=400w=400 arrive first and wait in line.

Then the cow with w=1w=1 arrives and turns away, since 2 cows are already in line. The cows with w=2w=2 then arrive, one staying and one turning away.

题目大意

给定n头牛,然后n头牛有一个耐受值,它只能忍受前面最多w个牛,多于w个牛的时候,牛就会走掉。问队伍的最短长度是多少。

题目分析

肯定要让牛多跑掉,所有,把耐受值大的放在前面,小的放在后面。排个序,然后模拟计算一下有多少个牛的在排队就是答案了。说起来好像是一个贪心。直接贪过去。(从大到小,sort其实可以不用写cmp,用greater就行了,若非遇到结构体,其实都可以用greater)

代码

#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1e5;
int arr[maxn];
int main(int argc, char const *argv[]) {
  int n;
  scanf("%d", &n);
  for(int i = 0; i < n; i++){
    scanf("%d", &arr[i]);
  }
  sort(arr, arr + n, greater<int>());
  int ans = 0;
  for(int i = 0; i < n; i++){
    if(arr[i] >= i)
      ans++;
    else
      break;
  }
  printf("%d\n", ans);
  return 0;
}

猜你喜欢

转载自blog.csdn.net/IT_w_TI/article/details/88557931