http://codeforces.com/problemset/problem/545/D
1 second
256 megabytes
standard input
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.
The first line contains integer n (1 ≤ n ≤ 105).
The next line contains n integers ti (1 ≤ ti ≤ 109), separated by spaces.
Print a single number — the maximum number of not disappointed people in the queue.
5
15 2 1 5 3
4
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的更多相关文章
- 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 ...
- 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 ...
- codeforces.com/problemset/problem/213/C
虽然一开始就觉得从右下角左上角直接dp2次是不行的,后面还是这么写了WA了 两次最大的并不一定是最大的,这个虽然一眼就能看出,第一次可能会影响第二次让第二次太小. 这是原因. 5 4 32 1 18 ...
- 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 ...
- codeforces 340C Tourist Problem
link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...
- codeforces B. Routine Problem 解题报告
题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...
- Codeforces 527D Clique Problem
http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...
- Codeforces 706C - Hard problem - [DP]
题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...
- Codeforces 1096D - Easy Problem - [DP]
题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...
随机推荐
- MySQL(七)MySQL常用函数
前言 上一篇给大家介绍了,MySQL常用的操作符其实已经是非常的详细了,现在给大家分享的是MySQL的常用函数.希望对我和对大家都有帮助. 一.字符串函数 1.1.LOWER.lcase(string ...
- Maven简述
一.前言 以前做过的项目中,没有真正的使用过Maven,只知道其名声很大,其作用是用来管理jar 包的.最近一段时间在项目过程中使用Maven,用Maven构建的web项目,其项目结构只停留在 ...
- distinct和group by 去掉重复数据分析
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp40 用distinct关键字只能过滤查询字段中所有记录相同的(记录集相同) ...
- VS2012启用angularjs智能提示Intelligence(转)
1. 關閉正在執行的 Visual Studio 2012 2. 開啟檔案總管,並進入以下目錄: C:\Program Files (x86)\Microsoft Visual Studio 11.0 ...
- JAVA基础-集合(一)
一.结构 Collection接口为一些单列集合的根接口,其常用子接口为List接口.Set接口.List接口常用实现子类为ArrayList(数组)LinkedList(链表).Set接口常用实现子 ...
- 201521123014 《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 泛型(编写的代码可被不同类型的对象所重用) Java中一个集合可以放任何类型的对象,因为任何对象都 is-a ...
- 201521123048 《Java程序设计》第4周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 1.多态:使用单一接口操作多种类型的对象. 2.private修饰属性,public修饰方法 ...
- 201521123037 《Java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. java异常继承架构 2. 书面作业 本次PTA作业题集异常 1. 常用异常 题目5-1 1.1 截图你的提交结果( ...
- 从JS和jQuery浅谈DOM操作,当我们在获取时,究竟获取了什么
0.写在前面的话 自己对前端的东西一直不是很熟,现在开始要想办法从前端各个地方去获取想要的属性值的时候,也基本是在网上现炒现卖,几周下来,发现自己还是迷迷糊糊,可以算是一无所获. 所以就抽时间,把这一 ...
- JDBC操作数据库之修改数据
使用JDBC修改数据库中的数据,起操作方法是和添加数据差不多的,只不过在修改数据的时候还要用到UPDATE语句来实现的,例如:把图书信息id为1的图书数量改为100,其sql语句是:update bo ...