P4799 [CEOI2015 Day2]世界冰球锦标赛

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
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXn = 40;

template <typename T>
inline void read(T &a) {
char c;for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar());bool f = c == '-';T x = f ? 0 : (c ^ '0');for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) {x = x * 10 + (c ^ '0');}a = f ? -x : x;
}
template <typename T, typename ...Argv>
inline void read(T &a, Argv &...argv) {
read(a), read(argv...);
}

map<int, int> map1, map2;
int n, midn, W, w[MAXn + 10];
signed main() {
read(n, W);
midn = n >> 1;
for (int i = 1; i <= n; ++i) {
read(w[i]);
}
map1[0] = map2[0] = 1;
for (int i = 1; i <= midn; ++i) {
auto j = end(map1), bottomj = begin(map1);
for (--j; ; --j) {
map1[(*j).first + w[i]] += map1[(*j).first];
if (j == bottomj) break;
}
}
for (int i = midn + 1; i <= n; ++i) {
auto j = end(map2), bottomj = begin(map2);
for (--j; ; --j) {
map2[(*j).first + w[i]] += map2[(*j).first];
if (j == bottomj) break;
}
}
int sum = 0, ans = 0;
auto j = begin(map2), topj = end(map2);
auto i = end(map1), bottomi = begin(map1);
for (--i; ; --i) {
int topmapj = W - (*i).first;
while (j != topj && (*j).first <= topmapj) {
sum += (*j).second;
++j;
}
ans += (*i).second * sum;
if (i == bottomi) break;
}
printf("%lld\n", ans);
return 0;
}