http://codeforces.com/gym/102219/problem/K
Nina works as an IT support in a software company and always busy. The biggest issue she is facing is that sometimes she misses some deadlines. In their team the time needed to finish each customer request is estimated based on experience, 1≤x≤105. The moment a request is submitted, she has double of the estimated time to respond to the request, 2x. Meaning if the request A was submitted at 12pm and takes 2 hours to finish, she can wait 2 hours and then work on it for 2 hours and still finish the job on time, by 4
pm and the customer would be satisfied.
Sometimes there is not enough capacity and she has to pick up a lot of requests, and it is expected to miss some deadlines. But she needs your help, to see if arrange correctly what are the maximum requests she can finish before their deadlines.
Let's assume that she has the list of the requests and their deadline immediately as she starts working every day and she doesn't take any break until she is done with all of them.
Input
The first line contains integer m(1≤m≤20). Number of cases.The second line contains integer n(1≤n≤105). Number of the requests.The last line contains nintegers ti (1≤ti≤109), separated by spaces. Estimated time each request should be responded so the customer would be happy.
Output
Print the case number and a single number - the maximum number of satisfied customer for each case.
Example
Input
1 5 15 2 1 5 3
Output
Case #1: 4
Note
If she responds to the request with this order 1, 2, 3, 5, 15, the only customer with the request that requires 5 hours wouldn't be happy.
题意分析:
Nina每天工作的时候有n个待完成的工作,每个工作需要x时间来完成,可以在2x之前提交,求最多可以成功提交的工作有多少个。
解题思路:
首先要从时间短的开始做,因为先做时间长的在做短的,短的就有可能超时,所以先按时间排序,
如果一项工作完成之后超过了提交时间,那么这项工作从一开始就不用做,可以结束时间去做其他工作,
所以贪心策略是先判断做这项工作能否在2x之前提交,如果可以,做这项工作,否则,跳过这项工作。
#include <stdio.h>
#include <algorithm>
#define N 100020
using namespace std;
long long a[N];
int main()
{
int T, t=0, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i=0; i<n; i++)
scanf("%lld", &a[i]);
sort(a, a+n);
long long sum=0;
int ans=0;
for(int i=0; i<n; i++)
{
if(sum<=a[i])
{
sum+=a[i];
ans++;
}
}
printf("Case #%d: %d
", ++t, ans);
}
return 0;
}