原题链接:https://beta.atcoder.jp/contests/arc070/tasks/arc070_b
No Need
Problem Statement
AtCoDeer the deer has N N cards with positive integers written on them. The number on the -th card (1≤i≤N) ( 1 ≤ i ≤ N ) is ai a i . 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 K K or greater.
Then, for each card , he judges whether it is unnecessary or not, as follows:
If, for any good subset of the cards containing card i i , the set that can be obtained by eliminating card from the subset is also good, card i i is unnecessary.
Otherwise, card 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≤5000
1
≤
N
≤
5000
1≤K≤5000
1
≤
K
≤
5000
1≤ai≤109(1≤i≤N)
1
≤
a
i
≤
10
9
(
1
≤
i
≤
N
)
Partial Score
300
300
points will be awarded for passing the test set satisfying
N,K≤400
N
,
K
≤
400
.
Input
The input is given from Standard Input in the following format:
N K N K
a1 a2 ⋯ aN a 1 a 2 ⋯ a N
Output
Print the number of the unnecessary cards.
Sample Input 1
3 6
1 4 3
Sample Output 1
1
There are two good sets: {2,3} { 2 , 3 } and {1,2,3} { 1 , 2 , 3 } .
Card 1 1 is only contained in , and this set without card 1 1 , , is also good. Thus, card 1 1 is unnecessary.
For card , a good set {2,3} { 2 , 3 } without card 2 2 , , is not good. Thus, card 2 2 is NOT unnecessary.
Neither is card for a similar reason, hence the answer is
1
1
.
Sample Input 2
5 400
3 1 4 1 5
Sample Output 2
5
In this case, there is no good set. Therefore, all the cards are unnecessary.
Sample Input 3
6 20
10 4 3 10 25 2
Sample Output 3
3
题目大意
输入 个数字 A1,A2,⋯,AN A 1 , A 2 , ⋯ , A N ,输入 K K 。
如果对于所有包含 ,且和大于 K K 的集合,去掉 后和还大于 K K ,那么 就是可有可无的。
问有多少个元素是可有可无的。
题解
将所有数从小到大排序,可有可无的数一定是一段前缀。
设 A1+A2+⋯+Ai=sum A 1 + A 2 + ⋯ + A i = s u m ,如果 Ai+1,+Ai+2+⋯+AN A i + 1 , + A i + 2 + ⋯ + A N 无法凑出 K−sum∼K−1 K − s u m ∼ K − 1 中的任意一个数,那么 A1∼Ai A 1 ∼ A i 就全是可有可无的。
因为我们要凑的只是 [0,K] [ 0 , K ] 范围内的数,所以 Ai=min(Ai,K) A i = m i n ( A i , K ) ,最后背包的容量只有 K K ,复杂度为。
代码
#include<bits/stdc++.h>
using namespace std;
const int M=5005;
int n,k,sum,a[M],dp[M];
void in()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;++i)scanf("%d",&a[i]),a[i]=min(a[i],k),sum+=a[i];
}
void ac()
{
sort(a,a+n+1);dp[0]=1;
for(int i=n,flag=1;i>=0;flag=1,sum-=a[i--])
{
for(int j=k-1;j>=k-sum&&j>=0;--j)if(dp[j])flag=0;
if(flag)printf("%d",i),exit(0);
for(int j=k;j>=a[i];--j)dp[j]|=dp[j-a[i]];
}
}
int main(){in();ac();}