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

时间:2022-09-12 14:12:50
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&colon;&sol;&sol;codeforces&period;com&sol;problemset&sol;problem&sol;594&sol;A

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

  2. http&colon;&sol;&sol;codeforces&period;com&sol;problemset&sol;problem&sol;712&sol;D

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

  3. codeforces&period;com&sol;problemset&sol;problem&sol;213&sol;C

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

  4. http&colon;&sol;&sol;codeforces&period;com&sol;problemset&sol;problem&sol;847&sol;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&period; 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 - &lbrack;DP&rsqb;

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

  9. Codeforces 1096D - Easy Problem - &lbrack;DP&rsqb;

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

随机推荐

  1. iOS&period;DistributionApp&period;1-mobile-provision-file&lbrack;draft&rsqb;

    .mobileprovision file 0. .mobileprovision file的作用 .mobileprovision file作用以及扮演的角色 1. 如何删除旧的.mobilepro ...

  2. Eclipse设置不格式化注释

    Eclipse设置不格式化注释 注释中写点带格式的文字,format后全乱了,解决办法如下: Windows -> Preferces -> java -> Code Style - ...

  3. 字符串 - 近似回文词 --- csu 1328

    近似回文词 Problem's Link:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 analyse: 直接暴力枚举每一个终点,然后枚举 ...

  4. Python之property装饰器

    参考: http://www.cnblogs.com/lovemo1314/archive/2011/05/03/2035600.html http://joy2everyone.iteye.com/ ...

  5. Django Form组件 学生管理系统

    from django.db import models # Create your models here. class Classes(models.Model): title=models.Ch ...

  6. mongoDB-Explain

    新版的MongoDB中的Explain已经变样了 Explain支持三种Mode queryPlanner Mode db.collection.explain() 默认mode是queryPlann ...

  7. Docker 持续集成初次体验

    背景 在家的时候,实在不想做其他的,想起之前参加的一场关于docker的座谈会,于是想搞以下docker. 开始 在道客云上搞了一下持续集成,总体来说,比较好用的. 写了一个Go程序,就是之前写的发邮 ...

  8. Codeforces Round &num;477 &lpar;rated&comma; Div&period; 2&comma; based on VK Cup 2018 Round 3&rpar; E 贪心

    http://codeforces.com/contest/967/problem/E 题目大意: 给你一个数组a,a的长度为n 定义:b(i) = a(1)^a(2)^......^a(i), 问, ...

  9. 正向工程、逆向工程与MDA

    正向工程.逆向工程与MDA 正向工程:从UML图形生成代码: 逆向工程:从代码和成UML图形: //不要依赖于正向或逆向工程,仅是一种辅助手段.画图的目的不是为了生成代码:而写代码的目的也不是为了生成 ...

  10. Linux运维学习第一天!

    第一步: 申请了一个腾讯的云主机!!!过程还是挺复杂的...配置有点低,满足初步学习需求就行啦(报了个培训班给送的,感觉不咋地道,太抠门) 服务器:北京 机型:标准型 镜像:公共镜像 系统:CentO ...