【CodeForces】Codeforces Round 625

本文详细解析了CodeForces Round 625中的六道算法问题,包括Journey Planning、Navigation System、World of Darkraft: Battle for Azathoth、Reachable Strings、Treeland and Viruses以及Blocks and Sensors。每个问题提供了最优解法,涉及BFS、最短路径、区间维护、字符串哈希等技术,时间复杂度从O(NLogN)到O(N×M×K)不等。
摘要由CSDN通过智能技术生成

比赛链接

点击打开链接

官方题解

点击打开链接

Problem A. Journey Planning

显然可以枚举 b i b_i bi i i i 的差值,并选取所有合法的 b i b_i bi

时间复杂度 O ( N L o g N ) O(NLogN) O(NLogN)

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
map <int, ll> sum;
int main() {
	int n; read(n);
	for (int i = 1; i <= n; i++) {
		int x; read(x);
		sum[x - i] += x;
	}
	ll ans = 0;
	for (auto x : sum) {
		chkmax(ans, x.second);
	}
	cout << ans << endl;
	return 0;
}

Problem B. Navigation System

考虑将边反向, BFS 求出终点到所有点的最短路。

对于重构次数最小的问题,显然只有在走过一条不是最短路图上的边时需要重构。
对于重构次数最多的问题,若当前点在最短路图上的出边不唯一,则也可以重构。

时间复杂度 O ( N + M ) O(N+M) O(N+M)

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
int n, m, k, q, b[MAXN];
vector <int> a[MAXN];
int dist[MAXN], cnt[MAXN];
void work(int pos) {
	static int q[MAXN];
	int l = 0, r = 0; q[0] = pos, dist[pos] = 1, cnt[pos] = 1;
	while (l <= r) {
		int tmp = q[l++];
		for (auto x : a[tmp]) {
			if (dist[x] == 0) {
				dist[x] = dist[tmp] + 1;
				q[++r] = x;
			}
			if (dist[x] == dist[tmp] + 1) {
				cnt[x] = min(2, cnt[x] + 1);
			}
		}
	}
}
int main() {
	read(n), read(m);
	for (int i = 1; i <= m; i++) {
		int x, y; read(x), read(y);
		a[y].push_back(x);
	}
	read(q);
	for (int i &
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值