http://codeforces.com/problemset/problem/545/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.

Examples

input

515 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.

题意;给你一个数组表示每个人所要消耗的时间,当一个人排队时间的时间没超过他要消耗的时间他就不会感到厌烦,让你重新给人排队,问使得不感到厌烦的人最多为多少;

题解:dp[i][0]表示第i个人站在队列里不感到厌烦的人的最大数dp[i][1]表示第i个人不站在队列里不感到厌烦的人的最大数,然后用太tmp1,tmp2来维护dp[i][0]和dp[i][1]

状态下已经所需要的时间和,用tmp1,tmp2与a[i]的大小关系来状态转移看是否不厌烦的人数能否加一(应该好理解),具体转移方程看代码,最后答案就是

dp[n][0]和dp[n][1]的较大值;

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=1e5+5;
int dp[maxn][2],a[maxn],n;
ll tmp1,tmp2;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    dp[1][0]=1;dp[1][1]=0;
    tmp1=a[1];
    for(int i=2;i<=n;i++)
    {
        ll t=tmp1;
        if(a[i]>=tmp1&&a[i]>=tmp2)
        {
            if(dp[i-1][0]>dp[i-1][1])
            {
                dp[i][0]=dp[i-1][0]+1;tmp1+=a[i];
            }
            else
            {
                dp[i][0]=dp[i-1][1]+1;tmp1=tmp2+a[i];
            }
        }
        else if(a[i]>=tmp1)
        {
            if(dp[i-1][0]+1>dp[i-1][1])
            {
                dp[i][0]=dp[i-1][0]+1;tmp1+=a[i];
            }
            else
            {
                dp[i][0]=dp[i-1][1];tmp1=tmp2+a[i];
            }
        }
        else if(a[i]>=tmp2)
        {
            if(dp[i-1][0]>dp[i-1][1]+1)
            {
                dp[i][0]=dp[i-1][0];tmp1+=a[i];
            }
            else
            {
                dp[i][0]=dp[i-1][1]+1;tmp1=tmp2+a[i];
            }
        }
        else
        {
            if(dp[i-1][0]>dp[i-1][1])
            {
                dp[i][0]=dp[i-1][0];tmp1+=a[i];
            }
            else
            {
                dp[i][0]=dp[i-1][1];tmp1=tmp2+a[i];
            }
        }
        if(dp[i-1][0]>dp[i-1][1])
        {
            dp[i][1]=dp[i-1][0];tmp2=t;
        }
        else
        {
            dp[i][1]=dp[i-1][1];
        }
    }
     int ans=max(dp[n][0],dp[n][1]);
     cout<<ans<<endl;
} 
时间: 2024-10-03 14:55:41

http://codeforces.com/problemset/problem/545/D的相关文章

http://codeforces.com/problemset/problem/712/D

D. Memory and Scores time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Memory and his friend Lexa are competing to get higher score in one popular computer game. Memory starts with score a a

CodeForces 687A NP-Hard Problem

Portal:http://codeforces.com/problemset/problem/687/A 二分图染色 好模板题 有SPJ 值得注意的是,因为C++的奇妙的运算机制 若在vector变量x.size()=0,则x.size()-1会溢出(此处坑了我3h) 这不禁让我想起了文明2里的甘地 1 #include<iostream> 2 #include<algorithm> 3 #include<set> 4 #include<cstdio> 5

codeforces 706C Hard problem DP(动态规划)问题

题目链接:http://codeforces.com/problemset/problem/706/C 题目大意:  给定n个字符串, 每个字符串可以颠倒前后位置(第一个字母到最后一个,第二个字母到倒数第二位) 每次颠倒需要花费ci的力气, 要求将所给的n个字符串用最小力气按字典序排列, 输出力气值, 如果无法按字典序排列, 则输出-1 数据范围:2?≤?n?≤?100?000 . ci (0?≤?ci?≤?1e9) 所有字符串总长度不会超过1000000. 解题思路: 这是一道DP题, dp[

codeforces Round 286# problem A. Mr. Kitayuta&#39;s Gift

Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked to insert exactly one lowercase English letter into s to make it a palindrome. A palindrome is a string that reads the same forward and backward. For

codeforces 17A Noldbach problem

Noldbach problem time limit per test 2 seconds memory limit per test 64 megabytes input standard input output standard output Nick is interested in prime numbers. Once he read about Goldbach problem. It states that every even integer greater than 2 c

Codeforces Round#413 Problem A - C

[写在前面感(乱)叹(七)人(八)生(糟)的话] 本想借此机会一口气玩到蓝名,结果,A题写炸(少判了一种情况),C题写炸(辜负了我5分钟狂敲出来的线段树),结果又掉Rating...内心好绝望... Problem#A Carrot Cakes vjudge链接[here] (偷个懒,cf链接就不给了) 题目大意是说,烤面包,给出一段时间内可以考的面包数,建第二个炉子的时间,需要达到的面包数,问建炉子是否合理. 玄学 & 智商题,可能是因为我智商不够,所以在我决定休息的时候被hank掉了...

Codeforces Gym 100342C Problem C. Painting Cottages 转化题意

Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description The new cottage settlement is organized near the capital of Flatland. The construction company that is building the settl

Codeforces Gym 100610 Problem A. Alien Communication Masterclass 构造

Problem A. Alien Communication Masterclass Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Andrea is a famous science fiction writer, who runs masterclasses for her beloved readers. The most popular one is the

Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Robots are becoming more and more popular. They are used nowadays not only in manufacturing plants, but also at home. One programmer wit