Problem Description

AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is ai. 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 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, the set that can be obtained by eliminating card i from the subset is also good, card 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.
  • 1≤N≤5000
  • 1≤K≤5000
  • 1≤ai≤109(1≤iN)

Partial Score

300 points will be awarded for passing the test set satisfying N,K≤400.

Input

The input is given from Standard Input in the following format:

N K
a1 a2 ... aN

Output

Print the number of the unnecessary cards.

Example

Sample Input 1

3 6
1 4 3

Sample Output 1

1
There are two good sets: {2,3} and {1,2,3}.

Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.

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

Neither is card 3 for a similar reason, hence the answer is 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 个数,对于这个数组中的任意一个子集,如果他的元素和大于等于 k,那么这个子集就是一个好的子集,如果将一个数所在的所有的好的子集都删去这个数,但这些集合仍是好的子集,那么这个数就称为无用数,问这 n 个数中无用数的个数

思路:

首先,如果一个数不是无用数,由于若在子集中删除大于等于他的数后无法保证集合仍是好的子集,因此所有大于等于这个无用数的数都不是无用数

根据以上推论,首先将数组进行排序,然后二分即可,对于 mid>=k 的数,这些数肯定不是无用数,而对于 mid<k 的数,则需要将这些数去掉

在将 mid<k 的数去掉的过程中,需要去在数组中判断构成的集合的和是否存在属于 [k-a[mid],k-1] 的数,在这个过程中,暴力求解时间复杂度会达到 O(n^3),由于 N 最大到 5000,那么从整体的复杂度来说会超时,因此在求和过程需要进行优化

考虑使用二进制优化,利用 bitset 容器,每次用 sum|=sum<<a[i] 来求和,最后枚举一遍,看 [sum[k-a[mis]],sum[k-1]] 中是否存在 1 即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 5000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;int n,k,a[N];
bitset<N> sum;
bool judge(int x) {if(a[x]>=k)return 1;sum.reset();sum[0]=1;for(int i=1;i<=n;i++)if(i!=x)sum|=sum<<a[i];for(int i=k-1;i>=k-a[x];i--)if(sum[i])return true;return false;
}
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 left=0,right=n+1;while(left<right) {int mid=(left+right)>>1;if(judge(mid))right=mid;elseleft=mid+1;}printf("%d\n",left-1);return 0;
}

No Need(AtCoder-2346)相关推荐

  1. AtCoder Beginner Contest 198 (A ~ F)题解

    目录 A. Div B. Palindrome with leading zeros C. Compass Walking D. Send More Money E. Unique Color F. ...

  2. UPC个人训练赛第十五场(AtCoder Grand Contest 031)

    传送门: [1]:AtCoder [2]:UPC比赛场 [3]:UPC补题场 参考资料 [1]:https://www.cnblogs.com/QLU-ACM/p/11191644.html B.Re ...

  3. Mynavi Programming Contest 2021(AtCoder Beginner Contest 201)题解

    文章目录 A - Tiny Arithmetic Sequence B - Do you know the second highest mountain? C - Secret Number D - ...

  4. KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200)题解

    文章目录 A - Century B - 200th ABC-200 C - Ringo's Favorite Numbers 2 D - Happy Birthday! 2 E - Patisser ...

  5. freee Programming Contest 2022(AtCoder Beginner Contest 264)A~D题详细讲解

    目录 博主赛情 网站链接 比赛简介 Contest Information Reason why freee needs AtCoder users freee's business content ...

  6. 【解题报告系列】超高质量题单 + 题解(ACM / OI)超高质量题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我新写的超高质量的题解和代码,题目难度不 ...

  7. 【转】误差矩阵(混淆矩阵)评价法

    原文地址:erdas分类精度评价作者:依暧白鲸 误差矩阵(混淆矩阵)评价法 基于误差矩阵的分类精度评价方法 误差矩阵(error matrix)又称混淆矩阵(confusion matrix),是一个 ...

  8. 依赖注入框架(DI Framework)

    在采用依赖注入实现的 Notification 类中,虽然我们不需要用类似 hard code 的方式,在类内部通过 new 来创建 MessageSender 对象,但是,这个创建对象.组装(或注入 ...

  9. python疫情监控(爬虫+可视化)

    大家好,作为一名互联网行业的小白,写博客只是为了巩固自己学习的知识,但由于水平有限,博客中难免会有一些错误出现,有不妥之处恳请各位大佬指点一二! 博客主页:链接: https://blog.csdn. ...

  10. Arduino语法详解,部分带注释(学习笔记)

    Arduino 的程序可以划分为三个主要部分:结构.变量(变量与常量).函数 函数部分参考官方:Arduino Reference - Arduino Reference 结构部分  一.结构 1.1 ...

最新文章

  1. R可视化ggplot2中绘制趋势线
  2. 消除软硬件鸿沟,芯客网完美支持智能硬件在移动互联时代的爆发
  3. c# 无法将类型隐式转换_C#中的隐式类型数组
  4. 【JAVA基础篇】枚举
  5. L1正则化与嵌入式特征选择(稀疏性)
  6. OpenCV-人像—酷感冷艳滤镜
  7. CentOS下部署Hadoop高性能集群
  8. FPGA基础知识之主要的FPGA生产厂商介绍
  9. 拓端tecdat|R语言最优化问题中的共轭函数
  10. 大型太阳能电池板在卫星上的使用,目前没有查到
  11. dos 批处理for循环
  12. BlackBerry Internet Service故障:公司内部同事无法互通邮件,对外联络没有问题
  13. Git基础-生成SSH密钥+配置密钥到远程仓库中
  14. 个人博客系统的设计与实现
  15. 能测试手机信号不好的软件,买手机别只看性能!教你测试手机信号好坏
  16. xp电脑对ajax的兼容性,xp系统iE11兼容性问题的详细技巧
  17. Python月份格式转化
  18. 文本文件转excel文件
  19. 跃居超导和离子阱的量子计算黑马,可编程可扩展的光量子硬件
  20. 湖南文旅数据中心:湖南文旅数据早知道(9月9日)

热门文章

  1. Javascript滑动菜单(一)
  2. SCSF 系列:Smart Client Software Factory 与 ObjectBuilder
  3. 关于自绘CListBox的一些疑惑
  4. 发际线预警!10本程序员必读烧脑经典,你敢挑战一本吗?
  5. python怎么用matplotlib_用Matplotlib在Python中绘制时间
  6. Redis——史上最强【集群】入门实践教程
  7. struts2核心配置
  8. SpringBoot2.0 整合 RocketMQ ,实现请求异步处理
  9. Mac 实现keras网络模型可视化【conda】
  10. 再不懂ZooKeeper,就安安心心把这篇文章看完