# BZOJ3827: [Poi2014]Around the world && CF526E Transmitting Levels

为了便于思考，倒着考虑，计算从一个点往回能到哪

代码：

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cstdio>
using namespace std;

const int MAX_N = 2000001;

int n, m, max_val;
int pre_sum[MAX_N], bgn[1000001];
int f[1000001];

inline int rd() {
register int x = 0, c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) {
x = x * 10 + (c ^ 48);
c = getchar();
}
return x;
}

int main() {
n = rd(); m = rd();
register int v1, v2, v3, v4;
for (int i = 1; i + 3 <= n; i += 4) {
pre_sum[i] = pre_sum[i - 1] + (v1 = rd());
pre_sum[i + 1] = pre_sum[i] + (v2 = rd());
pre_sum[i + 2] = pre_sum[i + 1] + (v3 = rd());
pre_sum[i + 3] = pre_sum[i + 2] + (v4 = rd());
max_val = max(max_val, max(v1, max(v2, max(v3, v4))));
}
for (int i = ((n >> 2) << 2) + 1; i <= n; ++i) {
pre_sum[i] = pre_sum[i - 1] + (v1 = rd());
max_val = max(max_val, v1);
}
int maxi = (n << 1);
for (int i = n + 1; i <= maxi; ++i)
pre_sum[i] = pre_sum[i - 1] - pre_sum[i - n - 1] + pre_sum[i - n];
int d, lef;
while (m--) {
d = rd(); lef = 1;
if (max_val > d) {
puts("NIE");
continue;
}
for (int i = n + 1; i <= maxi; ++i) {
while (pre_sum[i] - pre_sum[lef] > d) ++lef;
if (lef > n) {
f[i - n] = f[lef - n] + 1;
bgn[i - n] = bgn[lef - n];
} else {
f[i - n] = 1;
bgn[i - n] = lef;
}
if (i - bgn[i - n] >= n) {
printf("%d\n", f[i - n]);
break;
}
}
}
return 0;
}


posted @ 2018-11-02 08:10  AWordThatWeDefine  阅读(193)  评论(0编辑  收藏  举报