日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

No Need(AtCoder-2346)

發(fā)布時(shí)間:2025/3/17 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 No Need(AtCoder-2346) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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≤i≤N)

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 個(gè)數(shù),對(duì)于這個(gè)數(shù)組中的任意一個(gè)子集,如果他的元素和大于等于 k,那么這個(gè)子集就是一個(gè)好的子集,如果將一個(gè)數(shù)所在的所有的好的子集都刪去這個(gè)數(shù),但這些集合仍是好的子集,那么這個(gè)數(shù)就稱為無用數(shù),問這 n 個(gè)數(shù)中無用數(shù)的個(gè)數(shù)

思路:

首先,如果一個(gè)數(shù)不是無用數(shù),由于若在子集中刪除大于等于他的數(shù)后無法保證集合仍是好的子集,因此所有大于等于這個(gè)無用數(shù)的數(shù)都不是無用數(shù)

根據(jù)以上推論,首先將數(shù)組進(jìn)行排序,然后二分即可,對(duì)于?mid>=k 的數(shù),這些數(shù)肯定不是無用數(shù),而對(duì)于 mid<k 的數(shù),則需要將這些數(shù)去掉

在將 mid<k 的數(shù)去掉的過程中,需要去在數(shù)組中判斷構(gòu)成的集合的和是否存在屬于 [k-a[mid],k-1] 的數(shù),在這個(gè)過程中,暴力求解時(shí)間復(fù)雜度會(huì)達(dá)到 O(n^3),由于 N 最大到 5000,那么從整體的復(fù)雜度來說會(huì)超時(shí),因此在求和過程需要進(jìn)行優(yōu)化

考慮使用二進(jìn)制優(yōu)化,利用?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; }

?

總結(jié)

以上是生活随笔為你收集整理的No Need(AtCoder-2346)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。