2014-2015 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Prefer...

I. Sale in GameStore(贪心)
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

A well-known Berland online games store has announced a great sale! Buy any game today, and you can download more games for free! The only constraint is that the total price of the games downloaded for free can't exceed the price of the bought game.

When Polycarp found out about the sale, he remembered that his friends promised him to cover any single purchase in GameStore. They presented their promise as a gift for Polycarp's birthday.

There are n games in GameStore, the price of the i-th game is pi. What is the maximum number of games Polycarp can get today, if his friends agree to cover the expenses for any single purchase in GameStore?

Input

The first line of the input contains a single integer number n (1 ≤ n ≤ 2000) — the number of games in GameStore. The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ 105), where pi is the price of the i-th game.

Output

Print the maximum number of games Polycarp can get today.

Sample test(s)
input
5
5 3 1 5 6
output
3
input
2
7 7
output
2
Note

In the first example Polycarp can buy any game of price 5 or 6 and download games of prices 1 and 3 for free. So he can get at most 3 games.

In the second example Polycarp can buy any game and download the other one for free.

代码能力还是太弱了(这题是贪心),排序后找

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int max_size = 2010;
 7 
 8 void quick_sort(int arry[], int l, int r)
 9 {
10     if(l < r)
11     {
12         int i = l, j = r, k = arry[l];
13         while(i < j)
14         {
15             while(i < j && arry[j] >= k)
16                 j--;
17             if(i < j)
18                 arry[i++] = arry[j];
19             while(i < j && arry[i] < k)
20                 i++;
21             if(i < j)
22                 arry[j--] = arry[i];
23         }
24 
25         arry[i] = k;
26         quick_sort(arry, l, i-1);
27         quick_sort(arry, i+1, r);
28     }
29 }
30 
31 int main()
32 {
33     int n;
34     int arry[max_size];
35     /// (1 ≤ n ≤ 2000)
36     /// (1 ≤ pi ≤ 105)
37     while(scanf("%d", &n) != EOF)
38     {
39         memset(arry, 0, sizeof(arry));
40         for(int i = 0; i < n; ++i)
41         {
42             scanf("%d", &arry[i]);
43         }
44 
45         quick_sort(arry, 0, n-1);
46 
47         int max_price = arry[n-1];
48         int total = 0;
49         int num = 0;
50         int j = 0;
51          while(j < n-1)
52          {
53              total += arry[j++];
54              if(total <= max_price)
55                 num++;
56          }
57         printf("%d\n", num+1);
58     }
59     return 0;
60 }
View Code

F题感觉学长代码写得太风骚了,看不太懂,再加油

转载于:https://www.cnblogs.com/ya-cpp/p/4050425.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值