Lemonade Line [洛谷] (贪心) /*rank*/

在炎热的夏日,农夫John为他的N头奶牛提供柠檬水。每头奶牛对柠檬水的喜爱程度不同,最多能等待wi头奶牛。当John摇铃时,所有奶牛会按顺序到达并加入队伍。帮John找出最小可能的排队奶牛数量,以减少柠檬水的浪费。
摘要由CSDN通过智能技术生成

原题传送门


题面:

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值