【BZOJ3827】[Poi2014]Around the world【尺取法】【并查集】

【题目链接】

对于每个询问,设油量为d。

先预处理出每个点走一次最多走到哪,这个用尺取法可以O(n)。

然后得到一颗树,算一下每个点的深度。

枚举起点,在树上一直向上爬,直到距离超过n,爬的过程同时用并查集合并。


傻逼题卡内存。

/* Pigonometry */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1000005, inf = 0x7FFFFFFF;

int n, m, dis[maxn], next[maxn << 1], depth[maxn << 1];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int solve(int x) {
	int fa = x;
	for(; fa < x + n; fa = next[fa]);
	for(int i = next[x], j = x; i != fa; j = i, i = next[i]) next[j] = fa;
	return depth[x] - depth[fa];
}

int main() {
	n = iread(); m = n << 1;
	int k = iread(), lwb = 0;
	for(int i = 1; i <= n; i++) {
		dis[i] = iread();
		lwb = max(lwb, dis[i]);
	}
	while(k--) {
		int d = iread();
		if(d < lwb) {
			printf("NIE\n");
			continue;
		}
		for(int i = 1, j = 1, sum = 0; i <= m; i++) {
			while(j < m)
				if(sum + dis[j > n ? j - n : j] <= d) {
					sum += dis[j > n ? j - n : j];
					j++;
				}
				else break;
			sum -= dis[i > n ? i - n : i];
			next[i] = j;
		}
		depth[m] = 0;
		for(int i = m; i >= 1; i--) depth[i] = depth[next[i]] + 1;
		int ans = inf;
		for(int i = 1; i <= n; i++) ans = min(ans, solve(i));
		printf("%d\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值