也就是说,两个位置能配对,当且仅当它们的 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;
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
int N,p=0;cin>>N;long long ans=0;
for(int i=0;i<N;i++){cin>>p;int q=(p%2?p+1:p-1);ans+=21-p-q;}
cout<<ans+p<<'\n';
}
枚举时让 X 从小到大、同一个 X 下让 Y 从小到大,仅在差值严格变小时更新答案。这样差值相同的点会保留字典序最小的一个。枚举覆盖了所有合法整点,故最终结果满足全部最优条件。距离平方要用 long long,不需要开方或浮点数。
复杂度
时间 O(L2),额外空间 O(1)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
long long A,B;int L;cin>>A>>B>>L;long long D=A*A+B*B,best=LLONG_MAX;
int ax=-1,ay=-1;
for(int x=0;x<=L;x++)for(int y=0;y<=L;y++){
long long d=1LL*x*x+1LL*y*y-D;
if(d>=0&&d<best){best=d;ax=x;ay=y;}
}
cout<<ax<<' '<<ay<<'\n';
}
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
int T;cin>>T;while(T--){int n;cin>>n;vector<int>a(n);for(int &x:a)cin>>x;
int i=n-1;while(i>0&&a[i-1]>=a[i])--i;while(i>0&&a[i-1]<=a[i])--i;
cout<<i<<'\n';
}
}
二进制恰好有 d 位的正整数组成区间 [2d−1,2d−1]。因此只需枚举偶数 d=2,4,…,60,计算这个区间与 [l,r] 的交集长度,再把所有长度相加。
两个整数区间 [L,R] 与 [l,r] 的交集大小为 max(0,min(R,r)−max(L,l)+1)。不同位数对应的区间互不相交,并且每个符合条件的整数恰好落入其中一个偶数位区间,所以不会重复或遗漏。1018<260,枚举到 60 位已经足够。计算幂和答案均使用 long long。
复杂度
时间 O(logr),额外空间 O(1)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
long long l,r,ans=0;cin>>l>>r;
for(int d=2;d<=60;d+=2){
long long L=1LL<<(d-1),R=(1LL<<d)-1;
L=max(L,l);R=min(R,r);
if(L<=R)ans+=R-L+1;
}
cout<<ans<<'\n';
}
B. 校园活动清单
解题思路
读入喜爱值时,累加每个活动的总受欢迎度,同时在第 b 行找出特殊同学最喜欢的活动 fav。按总受欢迎度从大到小排序,取前 m 个编号作为初始清单。
每个旧位置 p 都唯一属于组首 ((p−1)modk)+1,因此一次操作中恰好访问每个元素一次。组的顺序和组内的顺序都与题目一致,所以构造出的新数组就是该指令执行后的队伍。必须使用新数组接收结果,不能一边读取旧位置一边覆盖旧队伍。
复杂度
每次操作 O(n),总时间 O(nT),额外空间 O(n)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
int n,T;cin>>n>>T;vector<int>a(n),b(n);
for(int &x:a)cin>>x;
while(T--){int k;cin>>k;int p=0;
for(int s=0;s<k;s++)for(int j=s;j<n;j+=k)b[p++]=a[j];
a.swap(b);
}
for(int i=0;i<n;i++)cout<<a[i]<<(i+1==n?'\n':' ');
}
D. 塔台超频
解题思路
信号必须依次从 i 号塔台传给 i+1 号塔台,因此对每一段都有必要条件
ai+1−ai≤bi+k.
注意这里使用的是发送方 i 的半径,不能使用两座塔台半径之和,也不要求反向通信。
因此 k 至少为所有 ai+1−ai−bi 的最大值,同时 k≥0。取这个最大值后,每一段均满足通信条件,信号就能依次传到终点,说明这个下界也可以达到。答案为 max(0,maxi<n(ai+1−ai−bi))。
复杂度
时间 O(n),额外空间 O(1)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
int n;cin>>n;long long pa,pb;cin>>pa>>pb;long long ans=0;
for(int i=1;i<n;i++){
long long a,b;cin>>a>>b;ans=max(ans,a-pa-pb);pa=a;pb=b;
}
cout<<ans<<'\n';
}
E. 噜噜的能量行
解题思路
记第 i 行初始行和为 si。一次行操作只使该行的行和增加 m,一次列操作则使所有行的行和各增加 1,选哪一列不会影响这些行和。因此可以先确定列操作总次数 c,再分别补足需要达标的行。
固定 c 后,第 i 行还需要的行操作次数为
ti=max(0,⌈mS−si−c⌉).
si 越大,ti 越小,所以无论 c 取何值,选择初始行和最大的 k 行都最优。将行和降序排列,枚举 c=0,1,…,S,计算 c+∑i=1kti 的最小值。
任意操作方案都可以按“列操作数量”和“各行操作数量”描述,因为加法顺序不影响结果;固定列操作数时,上面的取整给出了每行不可再减少的代价。枚举覆盖了最优方案:若列操作超过 S 次,单独进行 S 次列操作就已经使所有行达标,额外列操作没有必要。
复杂度
时间 O(nm+nlogn+kS),额外空间 O(n)。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
int n,m,k,S;cin>>n>>m>>k>>S;vector<long long>s(n);
for(int i=0;i<n;i++)for(int j=0;j<m;j++){int x;cin>>x;s[i]+=x;}
sort(s.rbegin(),s.rend());long long ans=S;
for(int c=0;c<=S;c++){
long long cost=c;
for(int i=0;i<k;i++){long long d=S-s[i]-c;if(d>0)cost+=(d+m-1)/m;}
ans=min
#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。一棵树可以向左倒、向右倒或不砍,任何两棵树占据的位置不能相交,要求最多砍倒多少棵树。
维护上一个保留的位置 p。扫描下一块石头 xi 时,如果 xi−p<d,这块石头不能保留,否则会产生一段过短的距离,只能把它移走;如果 xi−p≥d,就保留它,并更新 p=xi。扫描完中间石头后,还要检查终点 L 与最后保留位置的距离,如果小于 d,相当于最后一块保留的中间石头也必须被移走。
这个判定得到的是让距离都至少为 d 时需要移走的最少石头数。若最少移走数不超过 k,说明 d 可行;否则不可行。可行性随着 d 变大只会变难,因此可以二分最大可行值。时间复杂度为 O(nlogL),期望得分: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 n, k;
ll l;
cin >> n >> k >> l;
vector<ll> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
auto ok = [&](ll d) {
int c = 0;
ll pre = 0;
for (int i = 1; i <= n; i++) {
if (a[i] - pre < d) c++;
else pre = a[i];
}
if (l - pre < d) c++;
return c <= k;
};
ll lo = 1, hi = l, ans = 0;
while (lo <= hi) {
ll mid = (lo + hi) / 2;
if (ok(mid)) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 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);
int n, k;
string s;
cin >> n >> k >> s;
int l = 0, c = 0, 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);
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