2014-2015 ACM-ICPC, NEERC, Southern Subregional Contest----I. Sale in GameStore

在游戏商店的促销活动中,通过合理选择购买和免费获取的游戏,玩家Polycarp能够最大化获得的游戏数量。本文章详细介绍了如何利用贪心算法解决此问题,并提供了AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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.






题目大意:A要买一个游戏光盘,由于做活动,所以买一个价值为w的游戏盘,可以免费获赠总价值小于w的任意游戏组合(个数不限),现求能买到的游戏盘的总个数最大为多少。


解题思路:贪心。最大的是自己肯定买价值最大的盘,然后再选择获赠价值最少的盘,这样买到的总游戏盘数就会是最大的。





AC代码:

#include <cstdio>
#include <algorithm>
using namespace std;

int a[2005];

int main(){
//	freopen("in.txt","r",stdin);
	int n;
	while(scanf("%d", &n)==1){
		for(int i=0; i<n; i++)
			scanf("%d", &a[i]);
		sort(a, a+n);
		int ans = 1;          //加上自己买的一个
		int tt = 0;
		for(int i=0; i<n-1; i++){      //如果还可以选择
			if(tt + a[i] <= a[n-1])  { tt += a[i]; ans++; }
			else break;
		}
		printf("%d\n", ans);
	}
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值