AtCoder ARC070D No Need

Subject to the effect

Gives a {ai} by the integer K and a set of N integers of the set if a non-empty set and the sum of all elements K greater than or equal, the subset is said to be good

If a number does not remove the influence of a good number of the sets, called the number of the unnecessary
number of the number of requests that are unnecessary in the number N

N,K≤5000,ai≤10^9

 

answer

Being difficult the contrary, consider what is necessary when a number of.

If there is a subset of all the subsets is less than the number of K, but the addition of a subset of all the ai and the number of not less than K, it is necessary to ai.

Then for each ai, we just need to consider if you can find such a subset, if not found, ai is unnecessary in.

Not difficult to find, if ai≥K, all or a subset of the number and ≥K, these are meaningless.

If you set the number N of S, you ask its subset element and which may be, it is good to seek, open VIS bool array, each of a number added a [j], when vis [i] == true, the vis [i + a [j]] = true to.

But here there is a limit, we are currently considering ai addition to the rest of the numbers together can be less than K, and the ai plus than or equal to K, you can not put ai also count.

So we set Pre [i] [j] represents the number from the first to the i-th vis this prefix, provided Suf [i] [j] denotes the number of N-vis to the i-th suffix.

So considering ai, we only need to consider the possibility of using Pre [i-1] [j] and Suf [i + 1] [k] is less than Couchu a number K, greater than or equal it with ai K

Greedy think we Couchu this number must be less than the maximum in the case of K

Violence is a time to look for the words of K ^ 2, N times to find the time complexity is O (N * K ^ 2) is clearly time out.

Consider scale borrowing the idea of ​​maintaining two pointers p1, p2, p1 point Pre [i-1], p2 point Suf [i + 1]

p1 K-1 to 0 from moving forward, p2 from 0 to k-1 moves backward

For a fixed p1, p2 has been moved back until p1 + p2≥K, p2 stop moving, and then to move p1

Because p1 move forward, after moving p1 must become smaller, so p2 continues to move backward

If p1 + p2 <K, and Pre [i-1] [p1] == true, Suf [i + 1] [p2] == true, p1 + p2 + ai≥K, it is necessary to ai

In this case p1, p2 are moving monotonic, single lookup is O (K), the total time complexity is O (NK) of

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6 
 7 bool Pre[5002][5002],Suf[5002][5002],Ans[5002];
 8 int Data[5005];
 9 int N,K;
10 
11 template<typename elemType>
12 inline void Read(elemType &T){
13     elemType X=0,w=0; char ch=0;
14     while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
15     while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
16     T=(w?-X:X);
17 }
18 
19 int main(){
20     Read(N);Read(K);
21     for(register int i=1;i<=N;++i)
22         Read(Data[i]);
23     sort(Data+1,Data+N+1);
24     Pre[0][0]=Suf[N+1][0]=true;
25     for(register int i=1;i<=N;++i){
26         for(register int j=0;j<K;++j){
27             Pre[i][j]=Pre[i-1][j];
28             Suf[N-i+1][j]=Suf[N-i+2][j];
29         }
30         for(register int j=0;j<K;++j){
31             if(Suf[N-i+2][j] && j+Data[N-i+1]<K) Suf[N-i+1][j+Data[N-i+1]]=true;
32             if(Pre[i-1][j] && j+Data[i]<K) Pre[i][j+Data[i]]=true;
33         }
34     }
35     for(register int i=1;i<=N;++i){
36         if(Data[i]>=K){Ans[i]=true;continue;}
37         int p1=K-1,p2=0;
38         bool flag=false;
39         while(p1>=0 && p2<K){
40             if(!Pre[i-1][p1]){--p1;continue;}
41             while(p1+p2<K){
42                 if(Pre[i-1][p1] && Suf[i+1][p2] && p1+p2+Data[i]>=K){
43                     Ans[i]=true;
44                     flag=true;
45                     break;
46                 }
47                 ++p2;
48             }
49             if(flag) break;
50             --p1;
51         }
52     }
53     int Count=0;
54     for(register int i=1;i<=N;++i)
55         if(!Ans[i]) ++Count;
56     printf("%d\n",Count);
57 
58     return 0;
59 }

Guess you like

Origin www.cnblogs.com/AEMShana/p/12209767.html