#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){int ans=0;for(int i=1;i<=5;i++)for(int j=1;j<=5;j++){int x;cin>>x;if(x)ans=abs(i-3)+abs(j-3);}cout<<ans<<'\n';}
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){string s,t="byye";cin>>s;int p=0;for(char c:s)if(p<4&&c==t[p])p++;cout<<(p==4?"YES":"NO")<<'\n';}
同一位从数字 a 变成 b 时,必须且只需翻转两个掩码不同的位,所以代价为 popcount(mask[a] ^ mask[b])。所有位置互不影响,累加即可。应以字符串读取,保留前导零。时间 O(L),空间 O(L)。
参考程序
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){int n;string a,b;cin>>n>>a>>b;int m[]={63,6,91,79,102,109,125,7,127,111};int ans=0;for(int i=0;i<n;i++)ans+=__builtin_popcount(m[a[i]-'0']^m[b[i]-'0']);cout<<ans<<'\n';}
再比较连续除以质数 p,q 的顺序。若 p≤q,两种顺序的最终值相同,而先除以 p 的中间值较大。所以质因子应从小到大依次除去。试除分解的同时,把每次除之前的数加入答案,最后再加入 1。
时间 O(n),额外空间 O(1);答案用 64 位整数。
参考程序
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){ll n,ans=0;cin>>n;for(ll p=2;p*p<=n;p++)while(n%p==0){ans+=n;n/=p;}if(n>1)ans+=n;cout<<ans+1<<'\n';}
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){int T;cin>>T;while(T--){ll x,ans=0;cin>>x;for(int d:{100,50,20,10,5,1})ans+=x/d,x%=d;cout<<ans<<'\n';}}
C. 月球🐑校验算法
合法的 Token 是 n 除以 M=998244353 的非负余数,也就是区间 [0,M−1] 中与 n 同余的唯一整数。
C++ 对负数取余可能得到负值,故使用 ((nmodM)+M)modM。它与 n 同余且落在规定区间,满足两个验证条件。使用 long long 保存输入。时间、额外空间均为 O(1)。
参考程序
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){ll n,M=998244353;cin>>n;cout<<(n%M+M)%M<<'\n';}
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){int T;cin>>T;while(T--){int n,x=0,y=0;string s;cin>>n>>s;bool ok=false;for(char c:s){if(c=='U')++y;if(c=='D')--y;if(c=='L')--x;if(c=='R')++x;if(x==1&&y==1)ok=true;}cout<<(ok?"YES":"NO")<<'\n';}}
E. XRH的赛车统计
一辆车在 [a,b] 中往返,单程长度为 d=b−a,周期为 2d。令 r=tmod(2d),它在时刻 t 的位置为
特殊性质 A 中 p=1,所有操作都是前缀翻转。可以从右向左确定每一盏灯:如果当前这一盏的状态与目标不同,就必须翻转以它为右端点的前缀。只需保存之前翻转次数的奇偶性,不必真的逐灯翻转。等价地,在字符串右侧补一个 0,统计从右向左遇到多少次字符变化,时间复杂度为 O(n)。
特殊性质 B 中,目标状态关于中枢对称。只使用形如 [i,n+1−i] 的对称区间,就能从外向内逐对确定灯的状态。目标字符每变化一次,就需要切换一次翻转的奇偶性,操作次数等于从左侧虚拟的 0 走到中枢时遇到的字符变化次数。任何操作在中枢左侧都只能改变一个区间左端点,因此这些变化不能由更少的操作完成。时间复杂度为 O(n)。
将搜索和这两种特殊做法放在同一份程序中,可以覆盖测试点 1∼12,本轮独立 Linux 沙箱正式 20 点实测 60 分。
#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, p;
string s;
cin >> n >> p;
cin >> s;
if (p == 1) {
int ans = 0;
char c = '0';
for (int i = n - 1; i >= 0; i--) {
if (s[i] != c) {
ans++;
c = s[i];
}
}
cout << ans << '\n';
return 0;
}
bool ok = n == 2 * p - 1;
for (int i = 0; i < n; i++) {
if (s[i] != s[n - 1 - i]) {
ok = false;
}
}
if (ok) {
int ans = 0;
char c = '0';
for (int i = 0; i < p; i++) {
if (s[i] != c) {
ans++;
c = s[i];
}
}
cout << ans << '\n';
return 0;
}
queue<pair<string, int>> q;
set<string> h;
string st(n, '0');
q.push({st, 0});
h.insert(st);
while (!q.empty()) {
string u = q.front().first;
int d = q.front().second;
q.pop();
if (u == s) {
cout << d << '\n';
return 0;
}
for (int l = 0; l < p; l++) {
for (int r = p - 1; r < n; r++) {
string v = u;
for (int i = l; i <= r; i++) {
v[i] ^= 1;
}
if (h.insert(v).second) {
q.push({v, d + 1});
}
}
}
}
return 0;
}
算法二:把一次操作的两端分开看
在第 1 盏灯左侧和第 n 盏灯右侧,各补一盏不会参与操作、始终熄灭的虚拟灯。把相邻两盏灯状态不同的位置称为一个变化边界。
从左侧虚拟灯走到第 p 盏灯,设目标状态中共有 L 个变化边界;从右侧虚拟灯走到第 p 盏灯,设共有 R 个变化边界。也就是
#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, p;
string s;
cin >> n >> p;
cin >> s;
int l = 0;
char c = '0';
for (int i = 0; i < p; i++) {
if (s[i] != c) {
l++;
c = s[i];
}
}
int r = 0;
c = '0';
for (int i = n - 1; i >= p - 1; i--) {
if (s[i] != c) {
r++;
c = s[i];
#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, x;
string s;
cin >> n >> m >> x;
cin >> s;
int l = x;
int r = x;
for (int i = 0; i < m; i++) {
char c;
int k;
cin >> c >> k;
if (c == 'R') {
x += k;
} else {
x -= k;
}
l = min(l, x);
r = max(r, x);
}
int ans = r - l;
for (int i = 0; i < l; i++) {
ans += s[i] == 'R';
}
for (int i = r; i < n; i++) {
ans += s[i] == 'L';
}
cout << ans << '\n';
return 0;
}
所有乘法结果都不超过 2(n−1),且 a+b−2≤2×109−2,使用 C++ 的 long long 即可。时间复杂度为每组 O(1)。期望得分: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--) {
ll n, a, b;
cin >> n >> a >> b;
if (a < b) {
swap(a, b);
}
ll s = a + b - 2;
ll q = (n - 1) / s;
ll r = (n - 1) % s;
ll ans = q * 2;
if (r > 0) {
ans++;
}
if (r > a - 1) {
ans++;
}
cout << max(1LL, ans) << '\n';
}
return 0;
}
复杂度分析
每组数据的时间复杂度为 O(1),额外空间复杂度为 O(1)。全部 T 组数据的时间复杂度为 O(T)。
B. 分组登车(split)
题意概括
n 个小组按给定顺序登上容量为 k 的车辆。每辆车乘坐连续的一段同学,至多一个小组可以被拆到相邻的两辆车,求最少车辆数。
假设当前车已有 u 人,新来的一组有 x 人。若 u+x≤k,这一组本来就能坐进当前车,特意把它拆到下一辆车只会多占车;保留整组且不消耗拆组机会至少同样好。若 u+x>k,决定拆组时,第一部分应尽量填满当前车。否则把第二部分的一些人提前移到当前车,只会减小下一辆车的占用人数,不会影响顺序。因此只需考虑拆成
#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;
string s;
cin >> n >> s;
char pre[2] = {0, 0};
int cur = 0, ans = 0;
for (char c : s) {
if (c == '#') {
cur = 1 - cur;
} else if (c != pre[cur]) {
ans++;
pre[cur] = c;
}
}
cout << ans << '\n';
return 0;
}
复杂度分析
时间复杂度为 O(n)。输入字符串占 O(n) 空间,除输入存储之外的额外空间为 O(1)。
B. 补一盏灯(lamp)
题意概括
数轴上给定两列有序位置,分别表示 n 个等车位置和 m 盏已有路灯。允许在任意整数坐标增设一盏路灯,求所有路灯使用的最小整数半径,使每个等车位置都被照到。只要求覆盖给定的等车位置。
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m;
vector<ll> a, b, d;
bool chk(ll r) {
ll lo = -1, hi = -1;
for (int i = 0; i < n; i++) {
if (d[i] > r) {
if (lo == -1) {
lo = a[i];
}
hi = a[i];
}
}
return lo == -1 || hi - lo <= 2 * r;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
a.resize(n);
b.resize(m);
d.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i
也可以从总方格数中扣除两张纸条覆盖的数量,再补回被重复扣除的 ab 个交叠方格,得到 nm−am−bn+ab。两种计算方式等价,均为满分方法。本题不要求学习一般的面积求并算法或容斥公式。
若整行整列的方格枚举实现正确,也允许通过公布的限制,不靠压缩时限排除这种基础做法。
参考实现
#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, a, b;
cin >> n >> m >> a >> b;
cout << (n - a) * (m - b) << '\n';
}
#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 c, w, x, t, r;
cin >> c >> w >> x >> t >> r;
w -= x;
if (w <= t) w += r;
if (w > c) w = c;
cout << w << '\n';
}
复杂度分析
时间复杂度为 O(1),空间复杂度为 O(1)。
C. 重开的节拍(beat)
题意概括
每小节有 k 拍,初始下一拍为第 1 拍。静音与敲击都会经过一拍,重新开始将下一拍改为第 1 拍但不经过一拍。统计重拍上的敲击次数。
#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;
int p = 1, ans = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (x == 2) {
p = 1;
} else {
if (x == 1 && p == 1) ans++;
p++;
if (p > k) p = 1;
}
}
cout << ans << '\n';
return 0;
}
复杂度分析
每条记录只处理一次,时间复杂度为 O(n),额外空间复杂度为 O(1)。
D. 双色试板(shade)
题意概括
第 i 张工作台的试板包含颜色 ai,api,aqi。所有请求都查询原始色卡。统计这三个颜色恰好有两种不同值的工作台数。
#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, l = 0, r, x;
int q;
cin >> n >> q;
r = n;
for (int i = 0; i < q; i++) {
cin >> x;
if (x % 2) l = max(l, x + 1);
else r = min(r, x - 1);
}
cout << max(0LL, r - l + 1) << '\n';
return 0;
}