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

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

题解: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+;
int dp[maxn][],a[maxn],n;
ll tmp1,tmp2;
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a[i];
}
sort(a+,a++n);
dp[][]=;dp[][]=;
tmp1=a[];
for(int i=;i<=n;i++)
{
ll t=tmp1;
if(a[i]>=tmp1&&a[i]>=tmp2)
{
if(dp[i-][]>dp[i-][])
{
dp[i][]=dp[i-][]+;tmp1+=a[i];
}
else
{
dp[i][]=dp[i-][]+;tmp1=tmp2+a[i];
}
}
else if(a[i]>=tmp1)
{
if(dp[i-][]+>dp[i-][])
{
dp[i][]=dp[i-][]+;tmp1+=a[i];
}
else
{
dp[i][]=dp[i-][];tmp1=tmp2+a[i];
}
}
else if(a[i]>=tmp2)
{
if(dp[i-][]>dp[i-][]+)
{
dp[i][]=dp[i-][];tmp1+=a[i];
}
else
{
dp[i][]=dp[i-][]+;tmp1=tmp2+a[i];
}
}
else
{
if(dp[i-][]>dp[i-][])
{
dp[i][]=dp[i-][];tmp1+=a[i];
}
else
{
dp[i][]=dp[i-][];tmp1=tmp2+a[i];
}
}
if(dp[i-][]>dp[i-][])
{
dp[i][]=dp[i-][];tmp2=t;
}
else
{
dp[i][]=dp[i-][];
}
}
int ans=max(dp[n][],dp[n][]);
cout<<ans<<endl;
}

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

  1. http://codeforces.com/problemset/problem/594/A

    A. Warrior and Archer time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

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

    D. Memory and Scores time limit per test 2 seconds memory limit per test 512 megabytes input standar ...

  3. codeforces.com/problemset/problem/213/C

    虽然一开始就觉得从右下角左上角直接dp2次是不行的,后面还是这么写了WA了 两次最大的并不一定是最大的,这个虽然一眼就能看出,第一次可能会影响第二次让第二次太小. 这是原因. 5 4 32 1 18 ...

  4. http://codeforces.com/problemset/problem/847/E

    E. Packmen time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  5. codeforces 340C Tourist Problem

    link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...

  6. codeforces B. Routine Problem 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...

  7. Codeforces 527D Clique Problem

    http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...

  8. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  9. Codeforces 1096D - Easy Problem - [DP]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...

随机推荐

  1. JDK源码阅读——ArrayList

    序 如同C语言中字符数组向String过渡一样,作为面向对象语言,自然而然的出现了由Object[]数据形成的集合.本文从JDK源码出发简单探讨一下ArrayList的几个重要方法. Fields / ...

  2. SQL菜鸟学习札记(二)

    五月份一直在写SQL,之后写了一个期末大作业的项目,现在才有时间把之前遇到的各种奇怪的问题整理出来.下一部分札记应该是大作业中使用到的SQL的整理. 一.UPDATE SET语句后面可以并列赋值. 之 ...

  3. NPOI创建EXCEL(NOPI系列1)

    private void button1_Click(object sender, EventArgs e) { //创建一个工作薄 HSSFWorkbook wk = new HSSFWorkboo ...

  4. base(function strchr)

    函数原型:extern char *strchr(char *str,char character) 参数说明:str为一个字符串的指针,character为一个待查找字符.        所在库名: ...

  5. 换个角度审视NAT技术

    NAT (Network address translation,网络地址转换 )是局域网连接到互联网的一个对接工作. 首先要知道NAT是一个技术或者说软件而不是协议 后面你会知道NAT 是偏应用层但 ...

  6. 自制ACL+DHCP实验(初版)

    (实验用gns模拟器) ACL 实验拓扑: 实验要求: 1.1.1.1→3.3.3.3 不通 11.11.11.11→3.3.3.3 通 2.2.2.2→3.3.3.3 通 实验步骤: 步骤一:基本配 ...

  7. 个人作业2——英语学习APP案例分析(必应词典的使用)

    第一部分 调研, 评测 1.使用环境:window 10 词典版本: 2.使用体验: 打开词典出现下面这一界面: 词典模块:出现了每日一词,每日一句,每日阅读板块,还提供了生词本,个人觉得最喜欢的是这 ...

  8. 201521123076 《Java程序设计》第6周学习总结

    1.本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖面 ...

  9. 学号:201521123116 《java程序设计》第二周学习总结

    1. 本章学习总结 一:学习了string的类型,string的对象是不可变的,创建之后不能再修改 二:SET PATH/CLASSPATH和-cp的用法. 三:学习了Java API 文档的使用方法 ...

  10. 201521123050 《Java程序设计》第12周学习总结

    1. 本周学习总结 2. 书面作业 将Student对象(属性:int id, String name,int age,double grade)写入文件student.data.从文件读出显示. 1 ...