Codeforces Round #303 (Div. 2) D. Queue —— 贪心

题目链接:http://codeforces.com/problemset/problem/545/D


题解:

问经过调整,最多能使多少个人满意。

首先是排序,然后策略是;如果这个人对等待时间满意,则将其加入队伍中,更新当前的等候时间;如果不满意,既然等待时间最小了都不满意,那把他扔到最后,即跳过他,接着考虑下一个。


代码如下:

#include<cstdio>//F - F CodeForces - 545D 
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>

using namespace std;
typedef long long ll;

int a[100008];

int main()
{
    int n,ans = 1;
    ll tot;
    scanf("%d",&n);
    for(int i = 0; i<n; i++)
        scanf("%d",&a[i]);

    sort(a,a+n);
    tot = a[0];
    for(int i = 1; i<n; i++)
    {
        if(a[i]>=tot)//如果这样都不满意,那把他放在最后
        {
            ans++;
            tot += a[i];
        }
    }

    printf("%d\n",ans);
    return 0;
}


posted on 2017-03-24 16:03  h_z_cong  阅读(147)  评论(0编辑  收藏  举报

导航