Discount
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 984 Accepted Submission(s): 591
Problem Description
All the shops use discount to attract customers, but some shops doesn’t give direct discount on their goods, instead, they give discount only when you bought more than a certain amount of goods. Assume a shop offers a 20% off if your bill is more than 100 yuan, and with more than 500 yuan, you can get a 40% off. After you have chosen a good of 400 yuan, the best suggestion for you is to take something else to reach 500 yuan and get the 40% off.
For the customers’ convenience, the shops often offer some low-price and useful items just for reaching such a condition. But there are still many customers complain that they can’t reach exactly the budget they want. So, the manager wants to know, with the items they offer, what is the minimum budget that cannot be reached. In addition, although the items are very useful, no one wants to buy the same thing twice.
Input
The input consists several testcases.
The first line contains one integer N (1 <= N <= 1000), the number of items available.
The second line contains N integers P (0 <= P <= 10000), represent the ith item’s price.
The first line contains one integer N (1 <= N <= 1000), the number of items available.
The second line contains N integers P (0 <= P <= 10000), represent the ith item’s price.
Output
Print one integer, the minimum budget that cannot be reached.
Sample Input
4 1 2 3 4
Sample Output
11
Source
Recommend
lcy
这道题,开始以为是母函数,后来觉得数据处理貌似不行。又改为背包,内存直接爆掉了!!,我勒个去,没得办法,之后去了一趟度娘那,
才发现是一道.....貌似不能归于哪一类,就是杂谈吧!
方法是这样的...先进行排序(由小到大)也就是升序....然后用他后面n-1一项的和加1来与当前这项比较,如果当前这项小于前者之和加1,那么就是这个数
,是不能被组合出来的...
就拿 1 2 3 4 这组数据来讲吧.... 开始 sum=0; sum+1<1 ,不满足,所以跳到下一个数,此时sum=1; sum+1<2.。又不满足,所以继续...依次-----最后..还是不满足。
所以就输出 sum+1( 注意此时的sum=1+2+3+4=10),所以 输出的是10.
再比如 1 3 4 5 begin sum=0; sum+1<1 不满足,继续...sum+=1;sum+1<3;满足,所以终止,输出;
依据这一原理:
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#define maxn 1001
using namespace std;
int value[maxn];
int main()
{
int n,i,sum;
while(~scanf("%d",&n))
{
for(i=;i<n;i++)
scanf("%d",value+i);
sort(value,value+n);
sum=;
for(i=;i<n;i++)
{
if(sum+<value[i])
break;
sum+=value[i];
}
cout<<sum+<<endl;
}
return ;
}
要成为高手千万不要复制