-
Notifications
You must be signed in to change notification settings - Fork 12
/
1127-funny-knapsack.cpp
103 lines (67 loc) · 1.54 KB
/
1127-funny-knapsack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
ll arr[40];
ll temp1[20],temp2[20];
vector <ll> haflans;
ll ans =0;
void yooda(ll curr,ll maxi,ll currsum,ll wt)
{
if(curr == maxi){
if(currsum <= wt ){
haflans.push_back(currsum);
}
return;
}
yooda(curr+1,maxi,currsum + temp1[curr],wt);
yooda(curr+1,maxi,currsum,wt);
return;
}
void secondepic(ll curr,ll maxi,ll currsum,ll wt)
{
if(curr == maxi){
if(currsum <= wt ){
// cout << " sum here " << currsum << endl;
// ans++;
vector <ll>::iterator low = upper_bound(haflans.begin(),haflans.end(),wt-currsum);
ans += (low - haflans.begin());
}
return;
}
secondepic(curr+1,maxi,currsum + temp2[curr],wt);
secondepic(curr+1,maxi,currsum,wt);
return;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/placements2016/input.sam","r",stdin);
#endif
ll i,j,k,n,m,t,cont,a,b,w;
scanf("%lld",&t);
ll cases = t;
while(t--){
ans = 0;
haflans.clear();
memset(temp1,0,sizeof temp1);
memset(temp2,0,sizeof temp2);
printf("Case %lld: ",cases-t );
scanf("%lld %lld",&n,&w);
for(i =0;i < n;i++) {
scanf("%lld",&arr[i]);
}
sort(arr,arr+n);
for(i=0;i < n/2;i++){
temp1[i] = arr[i];
}
for(i=n/2;i < n;i++){
temp2[i-n/2] = arr[i];
}
yooda(0,n/2,0,w);
sort(haflans.begin(),haflans.end());
secondepic(0, n-n/2,0,w);
printf("%lld\n",ans);
}
return 0;
}