Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P3092 [USACO13NOV]No Change G

P3092 [USACO13NOV]No Change G

预处理每个物品从每个起点最多可以买到哪个位置,可以双指针,记 $f _ i$ 表示使用状态 $i$ 最多可以买到 $[1, f _ i]$ ,可以 DP。

查看代码
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
#include <cstdio>
using namespace std;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write(Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar(x % 10 + '0');
}
template <class Type>
void chkmax (Type &x, Type k)
{
k > x && (x = k);
}
const int N = 1e5 + 10, K = 16, M = 1 << K;
int n, m, v[N], w[K], f[M], g[K][N];
int main ()
{
read(m), read(n);
for (int i = 0; i < m; i++)
read(w[i]);
for (int i = 1; i <= n; i++)
read(v[i]);
for (int i = 0; i < m; i++)
for (int j = 1, s = 0, t = 1; j <= n; s -= v[j++])
{
while (t <= n && s + v[t] <= w[i])
s += v[t++];
g[i][j] = t - 1;
}
for (int i = 0; i < 1 << m; i++)
for (int j = 0; f[i] < n && j < m; j++)
!(i >> j & 1) && (chkmax(f[i | 1 << j], g[j][f[i] + 1]), 0);
int res = -1;
for (int i = 0; i < 1 << m; i++)
if (f[i] == n)
{
int s = 0;
for (int j = 0; j < m; j++)
s += !(i >> j & 1) * w[j];
chkmax(res, s);
}
write(res);
return 0;
}