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; }