Problem Statement
AtCoDeer the deer has NN cards with positive integers written on them. The number on the ii-th card (1≤i≤N)(1≤i≤N) is aiai. Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is KK or greater.
Then, for each card ii, he judges whether it is unnecessary or not, as follows:
- If, for any good subset of the cards containing card ii, the set that can be obtained by eliminating card ii from the subset is also good, card ii is unnecessary.
- Otherwise, card ii is NOT unnecessary.
Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.
Constraints
- All input values are integers.
- 1≤N≤50001≤N≤5000
- 1≤K≤50001≤K≤5000
- 1≤ai≤109(1≤i≤N)1≤ai≤109(1≤i≤N)
Partial Score
- 300300 points will be awarded for passing the test set satisfying N,K≤400N,K≤400.
Input
The input is given from Standard Input in the following format:
NN KK a1a1 a2a2 ... aNaN
Output
Print the number of the unnecessary cards.
Sample Input 1 Copy
Copy
3 6 1 4 3
Sample Output 1 Copy
Copy
1
There are two good sets: {2,32,3} and {1,2,31,2,3}.
Card 11 is only contained in {1,2,31,2,3}, and this set without card 11, {2,32,3}, is also good. Thus, card 11 is unnecessary.
For card 22, a good set {2,32,3} without card 22, {33}, is not good. Thus, card 22 is NOT unnecessary.
Neither is card 33 for a similar reason, hence the answer is 11.
Sample Input 2 Copy
Copy
5 400 3 1 4 1 5
Sample Output 2 Copy
Copy
5
In this case, there is no good set. Therefore, all the cards are unnecessary.
Sample Input 3 Copy
Copy
6 20 10 4 3 10 25 2
Sample Output 3 Copy
Copy
3
题意:
给你n个数的数组a[],和一个k,对于a[i]如果所有包含a[i]且大于k的集合,如果这些集合去掉a[i]之后,每个集合的剩余元素的和都大于等于k那么a[i]可有可无。问你有几个可有可无的数。
思路:
可有可无的元素一定是最小的若干个,于是在排序之后看看如果有ii满足a[i],a[i+1],...,a[n]a[i],a[i+1],...,a[n]这些数凑不出[max(k−sum,0),k−1][max(k−sum,0),k−1]中的任何数,说明11~i−1i−1这些数都是可有可无的。
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int f[5009];
int a[5009];
ll sum=0;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum+=min(a[i],k);
a[i]=min(a[i],k);//这里可以自己验证一下,如果a[i]>k,令a[i]=k没有影响。否则如果a[i]>k
不能传递。
}
sort(a+1,a+1+n);
f[0]=1;
for(int i=n;i>=0;i--)
{
int flag=1;
for(int j=k-1;j>=k-sum&&j>=0;j--)
{
if(f[j])
{
flag=0;
}
}
if(flag)
{
cout<<i<<endl;
return 0;
}
for(int j=k-1;j>=a[i];j--)f[j]|=f[j-a[i]];//因为从i这个位置向前移动了一位,所以只要看
j-a[i]是否出现了。
sum-=a[i];
}
}