zoukankan      html  css  js  c++  java
  • codeforces#303div2_D 贪心

    codeforces#303div2_D 贪心

    D. Queue
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Little girl Susie went shopping with her mom and she wondered how to improve service quality.

    There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed.

    Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.

    Input

    The first line contains integer n (1 ≤ n ≤ 105).

    The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces.

    Output

    Print a single number — the maximum number of not disappointed people in the queue.

    Sample test(s)
    input
    5
    15 2 1 5 3
    output
    4
    Note

    Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.

    题意:如题。。。

    思路:贪心,先排序,逐个判断a[i],不符条件者换到最后。这个贪心策略基于一个事实,但a[i]>a[i+1]时,若a[i]<s[i-1],有a[i+1]<a[i]<s[i-1]<s[i+1],即第i个不符,第i+1个若比第i个大,必然不符。碰到贪心题脑子又短路了。。。。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #include<set>
    #include<map>
    #include<string>
    #include<stack>
    #include<queue>
    #include<vector>
    
    using namespace std;
    
    const int maxn=1000100;
    const int INF=(1<<29);
    typedef long long ll;
    
    int n;
    ll a[maxn];
    
    int main()
    {
        while(cin>>n){
            for(int i=1;i<=n;i++){
                scanf("%I64d",&a[i]);
            }
            sort(a+1,a+n+1);
            a[0]=0;
            ll s=0;
            int cnt=0;
            for(int i=1;i<=n;i++){
                if(s<=a[i]){
                    cnt++;
                    s+=a[i];
                }
            }
            cout<<cnt<<endl;
        }
        return 0;
    }
    View Code
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    STM32F10x_ADC三通道逐次转换(单次、单通道软件触发)
    STM32F10x_RTC日历
    STM32F4_TIM输入波形捕获(脉冲频率)
    详解 C 语言开发五子棋游戏以及游戏中的重要算法与思路
    平安银行 深度解析梧州模式 或许是国内医药分开最好的模板!
    屏蔽双绞线和非屏蔽双绞线之间的区别
    如何刷新本地的DNS缓存?
    无线网络发射和接收的物理原理!
    wifi基本原理
    大润发创始人黄明端挥泪离场:我战胜了所有对手,却输给了时代!
  • 原文地址:https://www.cnblogs.com/--560/p/4518659.html
Copyright © 2011-2022 走看看