ARC070D No Need

本文解析了AtCoder竞赛中一道关于寻找数组中可有可无元素的问题,通过排序和动态规划的方法,高效地解决了判断哪些元素在满足特定条件的子集中是不必要的。

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

原题链接: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 i-th card (1iN) ( 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 i, 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 i from the subset is also good, card i i is unnecessary.

Otherwise, card i 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.
1N5000 1 ≤ N ≤ 5000
1K5000 1 ≤ K ≤ 5000
1ai109(1iN) 1 ≤ a i ≤ 10 9 ( 1 ≤ i ≤ N )

Partial Score

300 300 points will be awarded for passing the test set satisfying N,K400 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 {1,2,3}, and this set without card 1 1 , {2,3}, is also good. Thus, card 1 1 is unnecessary.

For card 2, a good set {2,3} { 2 , 3 } without card 2 2 , {3}, is not good. Thus, card 2 2 is NOT unnecessary.

Neither is card 3 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

题目大意

输入N 个数字 A1,A2,,AN A 1 , A 2 , ⋯ , A N ,输入 K K

如果对于所有包含 Ai,且和大于 K K 的集合,去掉Ai 后和还大于 K K ,那么 Ai 就是可有可无的。

问有多少个元素是可有可无的。

题解

将所有数从小到大排序,可有可无的数一定是一段前缀。

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 无法凑出 KsumK1 K − s u m ∼ K − 1 中的任意一个数,那么 A1Ai A 1 ∼ A i 就全是可有可无的。

因为我们要凑的只是 [0,K] [ 0 , K ] 范围内的数,所以 Ai=min(Ai,K) A i = m i n ( A i , K ) ,最后背包的容量只有 K K ,复杂度为O(NK)

代码
#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();}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值