也就是说,两个位置能配对,当且仅当它们的 ai−i 相等。顺序扫描数组,用哈希表记录每个值已经出现了多少次。处理第 i 个位置时,之前相同值的出现次数就是能和它配对的数量,然后再把当前位置加入计数。每个位置只处理一次,能通过全部数据。时间复杂度为 O(n),期望得分:100分。
参考实现
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
unordered_map<ll, ll> mp;
ll ans = 0;
for (int i = 1; i <= n; i++) {
ll x;
cin >> x;
ll v = x - i;
ans += mp[v];
mp[v]++;
}
cout << ans << '\n';
}
return 0;
}
复杂度分析
最终算法对每个元素进行一次哈希表查询和更新,总时间复杂度为 O(∑n),空间复杂度为 O(∑n)。
B. 模段(mseg)
题意概括
给定多组序列和模数 m,统计有多少个非空连续区间的元素和能被 m 整除。所有测试的长度总和不超过 2×105。
算法3:
设 si 为前 i 个数的和对 m 取模后的结果,区间 [l,r] 的和能被 m 整除,当且仅当 sr=sl−1。于是问题转化为统计相同前缀余数的二元组。
顺序扫描数组,初始时余数 0 出现一次。每得到一个新的前缀余数,就把之前相同余数的出现次数加入答案,再把当前余数记入哈希表。因为 m 可以很大,仍然使用哈希表而不是长度为 m 的数组。时间复杂度为 O(n),期望得分:100分。
参考实现
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
int n;
ll m;
cin >> n >> m;
unordered_map<ll, ll> mp;
mp[0] = 1;
ll s = 0, ans = 0;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
s = (s + x) % m;
ans += mp[s];
mp[s]++;
}
cout << ans << '\n';
}
return 0;
}
算法2:
当 k=2 时,只需要统计奇偶性,相同奇偶的两个数相加才是偶数。特殊性质 B 中所有数已经小于 k,可以直接按数值当作余数计数。这两类数据都指向同余分类。时间复杂度为 O(n),期望得分:30分。
算法3:
对一个数 x,只关心它的余数 r=xmodk。为了让两数之和能被 k 整除,另一个数的余数必须是 (k−r)modk。顺序扫描数组,处理当前数时先查询之前已经出现的互补余数个数,把它加入答案,再记录当前余数。
由于 k 可能很大,不能开长度为 k 的数组,使用哈希表保存实际出现过的余数即可。时间复杂度为 O(n),期望得分:100分。
参考实现
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
int n;
ll k;
cin >> n >> k;
unordered_map<ll, ll> mp;
ll ans = 0;
for (int i = 0; i < n; i++) {
ll
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 300005;
int n, ec;
ll a[N], b[N];
vector<pair<int, int> > g[N];
bool ok
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, t;
string s;
cin >> n >> t >> s;
while (t--) {
for (int i = 0; i + 1 < n; ) {
if (s[i] == 'B' && s[i + 1] == 'A') {
swap(s[i], s[i + 1]);
i += 2;
} else {
i++;
}
}
}
cout << s << '\n';
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<ll> a(n);
ll sm = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sm += a[i];
}
if (sm % 3 != 0) {
cout << 0 << '\n';
return 0;
}
ll t = sm / 3;
ll cur = 0, cnt = 0, ans = 0;
for (int i = 0; i + 1 < n; i++) {
cur += a[i];
if (cur == 2 * t) ans += cnt;
if (cur == t) cnt++;
}
cout << ans << '\n';
return 0;
}
复杂度分析
最终正解的时间复杂度为 O(n)。
空间复杂度为 O(n)。
B. 公园(park)
题意概括
给定一棵以 1 为根的树,每个点标记是否有障碍。统计从根到叶子的路径中,连续障碍点数量从不超过 m 的叶子个数。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vector<vector<int>> g(n + 1);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int ans = 0;
vector<array<int,3>> st;
st.push_back({1, 0, a[1]});
while (!st.empty()) {
auto t = st.back();
st.pop_back();
int u = t[0], p = t[1], c = t[2];
if (c > m) continue;
bool leaf = true;
for (int v : g[u]) {
if (v == p) continue;
leaf = false;
st.push_back({v, u, a[v] ? c + 1 : 0});
}
if (leaf) ans++;
}
cout << ans << '\n';
return 0;
}
复杂度分析
最终正解的时间复杂度为 O(n)。
空间复杂度为 O(n)。
C. 花束(flwr)
题意概括
长度为 x 的花束可以由单朵红花和长度为 k 的白花组拼成。多次询问区间 [l,r] 内所有长度的方案数之和,答案对 109+7 取模。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
ll s;
cin >> n >> s;
vector<pair<ll,ll>> a(n);
for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
sort(a.begin(), a.end());
for (int i = 0; i < n; i++) {
if (s <= a[i].first) {
cout << "NO\n";
return 0;
}
s += a[i].second;
}
cout << "YES\n";
return 0;
}
复杂度分析
最终正解的时间复杂度为 O(nlogn)。
空间复杂度为 O(n)。
B. 砍树(wood)
题意概括
有 n 棵树排在一条直线上,第 i 棵树在 xi,高度为 hi。一棵树可以向左倒、向右倒或不砍,任何两棵树占据的位置不能相交,要求最多砍倒多少棵树。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
int l = 0;
int c = 0;
int ans = 0;
for(int r = 0; r < n; ++r) {
if (s[r] == '0') {
++c;
}
while (c > k) {
if (s[l] == '0') {
--c;
}
++l;
}
ans = max(ans, r - l + 1);
}
cout << ans << "\n";
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, m;
cin >> n >> m;
vector<ll> v(n);
for(int i = 0; i < n; ++i) {
ll x;
cin >> x;
v[i] = x % m;
}
sort(v.begin(), v.end());
ll ans = 0;
ll c = 1;
for(int i = 1; i < n; ++i) {
if (v[i] == v[i - 1]) {
++c;
} else {
ans += c * (c - 1) / 2;
c = 1;
}
}
ans += c * (c - 1) / 2;
cout << ans << "\n";
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m;
string g[1005];
int ds[1005][1005];
int dt[1005][1005];
int qx[200005];
int qy[200005];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
void bfs(int rx, int ry, int d[1005][1005]) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
d[i][j] = -1;
}
}
int hd = 0, tl = 0;
qx[tl] = rx;
qy[tl] = ry;
++tl;
d[rx][ry] = 0;
while (hd < tl) {
int x = qx[hd];
int y = qy[hd];
++hd;
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] != '#' && d[nx][ny] == -1) {
d[nx][ny] = d[x][y] + 1;
qx[tl] = nx;
qy[tl] = ny;
++tl;
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
int sx = -1, sy = -1, tx = -1, ty
若坐标 i 的环境为木头(L),则可向前跳跃 d∈[1,M] 米。这一过程不增加游泳距离,因此对于所有合法的落点 i+d,有转移方程:
fi+d=min(fi+d,fi)
若坐标 i 的环境为水域(W),则必须向前游动 1 米,导致游泳距离加 1。故对于落点 i+1,有转移方程:
fi+1=min(fi+1,fi+1)
按照坐标从 0 到 N 依次进行上述转移。若转移过程中发现某位置的 fi>K,则其后续分支亦无需继续拓展。最终,只需检验 fN+1 的值是否满足 fN+1≤K 即可判定结果。
参考实现
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
char a[200005];
int f[200005];
void slv() {
int n, m, k;
cin >> n >> m >> k;
string s;
cin >> s;
a[0] = 'L';
for (int i = 1; i <= n; ++i) {
a[i] = s[i - 1];
}
a[n + 1] = 'L';
for (int i = 0; i <= n + 1; ++i) {
f[i] = 1e9;
}
f[0] = 0;
for (int i = 0; i <= n; ++i) {
if (f[i] > k) {
continue;
}
if (i > 0 && a[i] == 'C') {
continue;
}
if (i == 0 || a[i] == 'L') {
for (int j = 1; j <= m; ++j) {
if (i + j <= n + 1) {
if (f[i] < f[i + j]) {
f[i + j] = f[i];
}
}
}
} else if (a[i] == 'W') {
if (f[i] + 1 < f[i + 1]) {
f[i + 1] = f[i] + 1;
}
}
}
if (f[n + 1] <= k) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--) {
slv();
}
return 0;
}
复杂度分析
时间复杂度:设 N 为当前河流长度。算法遍历 0 至 N 的每个位置。当状态为木头时,内层常数级循环最多执行 M 次;当状态为水域时,仅发生一次状态转移。整体状态转移数受界于 O(NM)。鉴于题设中 ∑N≤2×105 且 M≤10,全局运算量严格受制于 ∑NM≤2×106,可在大极短时间内完成执行。时间复杂度为 O(∑NM)。
空间复杂度:算法内存分配仅涉及一个存储状态的字符串对应数组 A 与一个动态规划表 f。所需内存大小与最大单次询问的 N 呈线性关系,峰值不超过 2×105 的整型存储。空间复杂度为 O(max(N))。
【动态规划、贪心】队伍
题意概括
给定一个长度为 N 的正整数序列 A。你可以执行任意次操作:选取索引 i∈[1,N],交换 Ai 与 AN−i+1。请求出经过若干次操作后,序列中相邻且相等的元素对数(即干扰度)的最小值。本题包含 T 组测试数据,满足 ∑N≤2×105。