比赛链接
官方题解
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 &