forked from hackerkid/LightOJ-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1127-Funny-Knapsack.cpp
129 lines (94 loc) · 1.77 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
long long n;
long long w;
long long inp[100];
vector <long long> a;
vector <long long> b;
long long create_a(long long i,long long sum, long long max)
{
if(sum > w) {
return 0;
}
if( i == max) {
a.push_back(sum);
return 0;
}
create_a(i+1, sum + inp[i], max);
create_a(i+1, sum,max);
}
long long create_b(long long i,long long sum, long long max)
{
if(sum > w) {
return 0;
}
if( i == max) {
b.push_back(sum);
return 0;
}
create_b(i+1, sum + inp[i], max);
create_b(i+1, sum,max);
}
long long bs(long long j, long long k, long long targ)
{
long long i;
i = (j + k) / 2;
if(j > k) {
return -1;
}
if((i == a.size() - 1 || a[i+1] > targ) and a[i] <= targ) {
return i;
}
else {
if(a[i] > targ) {
return bs(j, i-1, targ);
}
else {
return bs(i+1, k, targ);
}
}
}
int bs(long long val)
{
int low = 0 , high =(int ) a.size() - 1 , mid , ans =low -1;
while (low <= high ) {
mid = (low + high ) >>1;
if(a[mid] <= val) {
ans = mid ;
low = mid + 1;
}
else high = mid - 1;
}
return ans ;
}
int main()
{
long long t;
long long ans;
scanf("%lld", &t);
for (long long cs = 1; cs <= t; cs++) {
scanf("%lld", &n);
scanf("%lld", &w);
ans = 0;
long long k;
vector <long long> ta;
vector <long long> tb;
swap(ta, a);
swap(tb, b);
for (long long i = 0; i < n; i++) {
scanf("%lld", &inp[i]);
}
create_a(0, 0, n/2);
create_b(n/2, 0, n);
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for (long long i = 0; i < b.size(); i++) {
k = bs(0, a.size() -1, w - b[i]);
ans = ans + k+1;
}
printf("Case %lld: %lld\n", cs, ans);
}
}