AtCoder Regular Contest 070 D - No Need(二分+bitset或者背包+思维)

 

D - No Need


Time Limit: 2 sec / Memory Limit: 256 MB

Score : 600600 points

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个整数构成的集合和一个整数K,若该集合中的的非空子集和大于等于K,则称该子集为GOOD的集合

若去掉一个数不会对GOOD的个数产生影响,则称该数字为“可有可无的数字”
请求出在N个数中“可有可无的数字”个数

举例子说明:

6 20
10 4 3 10 25 2

GOOD:

10 10

10 25

10 25

10 10 25 

熟悉的就在以上的基础上添加{4、3、2}的子集,所以4 3 2为可有可无的数。

分析:

第一种方法(二分+bitset)

可以想到如果x不是可有可无的数字,那>=均不是可有可无的数字,即可有可无的数字肯定为若干个小的数字,即满足单调性,这样二分即可。

二分的判断条件:1.如果当前这个数a[mid]>=k 这个数是必须的数,return 1;

                             2.否则,判断剩下子集的和是否有在k-a[mid]~k-1之间的数,有则为必须的数 return  1,否则 return 0;

求和过程需要bitset优化:

sum|=(sum<<a[i]);//求a[1~i]的前缀和sum1,即把sum的前sum1位均赋值1
 

第二种方法(背包)

参考:https://blog.csdn.net/ShadyPi/article/details/81738705

这个背包思想需要好好消化理解

 

#include <bits/stdc++.h>
using namespace std;
const int N=100005;
int a[N];
bitset<N> sum;
int n,k;
bool judge(int mid)
{
	if(a[mid]>=k) return  1;
	sum.reset();sum[0]=1;
	for(int i=1;i<=n;i++)
	{
		if(i!=mid)
			sum|=(sum<<a[i]);//求a[i]的前缀和sum1,即把sum的前sum1位均赋值1
	}
	for(int i=k-a[mid];i<=k-1;i++)
	{
		 if(sum[i]) return 1;
	}
	return 0;
	
}
int main()
{
	
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	int l=1,r=n+1;
	while(l<r)
	{
		int mid=(l+r)>>1;
		if(judge(mid))
			r=mid;
		else
			l=mid+1;
	}
	cout<<l-1<<endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值