维护上一个保留的位置 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;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; long long k, x; cin >> n >> k >> x;
vector<long long> a(n); long long ans = 0;
for (auto &v : a) {
cin >> v; long long take = min(k, v / x);
v -= take * x; k -= take; ans += v;
}
sort(a.rbegin(), a.rend());
for (int i = 0; i < n && k; ++i, --k) ans -= a[i];
cout << ans << '\n';
}
B. 噜噜的 DTOJ 挑战
从左到右扫描,维护已经选中、但尚未完成的三类前缀数量:d、dt、dto。遇到 d 就建立一个新前缀;遇到 t,若还有 d,就将一个 d 延长为 dt;遇到 o 类似地延长一个 dt;遇到 j,若还有 dto,就完成一个答案。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n; vector<vector<int>> g(n + 1);
for (int i = 1, u, v; i < n; ++i) {
cin >> u >> v; g[u].push_back(v); g[v].push_back(u);
}
vector<int> parent(n + 1), order; order.reserve(n); order.push_back(1);
for (int i = 0; i < (int)order.size(); ++i) {
int u = order[i];
for (int v : g[u]) if (v != parent[u]) { parent[v] = u; order.push_back(v); }
}
vector<int> size(n + 1, 1);
for (int i = n - 1; i > 0; --i) {
int u = order[i]; size[parent[u]] += size[u];
}
for (int i = 1; i <= n; ++i) cout << size[i] << (i == n ? '\n' : ' ');
}
D. 优美数组(kkeeu)
满足要求的数组必然是在所有奇数下标上放同一个数 x,所有偶数下标上放另一个数 y,且 x=y。若奇数位置中 x 出现 co(x) 次,偶数位置中 y 出现 ce(y) 次,那么能保留的元素数为 co(x)+ce(y),修改次数就是 n 减去这个数量。
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,long long>> top_two(const map<long long,int>& cnt, long long dummy) {
vector<pair<int,long long>> v;
for (auto p : cnt) v.push_back({p.second, p.first});
v.push_back({0, dummy}); v.push_back({0, dummy - 2});
sort(v.rbegin(), v.rend()); v.resize(2); return v;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n; map<long long,int> odd, even;
for (int i = 1; i <= n; ++i) {
long long x; cin >> x; if (i & 1) ++odd[x]; else ++even[x];
}
auto a = top_two(odd, -1), b = top_two(even, -2);
int keep = 0;
for (auto x : a) for (auto y : b) if (x.second != y.second)
keep = max(keep
#include <bits/stdc++.h>
using namespace std;
int gcd_int(int a, int b) { while (b) { int t = a % b; a = b; b = t; } return a; }
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n; vector<int> pos(1001, 0);
for (int i = 1, x; i <= n; ++i) { cin >> x; pos[x] = i; }
int ans = -1;
for (int u = 1; u <= 1000; ++u) if (pos[u])
for (int v = u; v <= 1000; ++v) if (pos[v] && gcd_int(u,v)==1)
ans = max(ans, pos[u] + pos[v]);
cout << ans << '\n';
}
所有增量相加会望远镜消去,等于最终的 N 减去初始值。操作会让 N 超过最初的 32 位范围,因此使用 64 位无符号整数存储,移位写为 1ULL << (k-1)。
时间复杂度 O(Q),额外空间复杂度 O(1)。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
unsigned long long n; int q; cin >> n >> q;
auto initial = n;
while (q--) {
int k; cin >> k;
unsigned long long b = 1ULL << (k - 1);
if ((n & b) == 0) n += b - n % (2 * b);
}
cout << n - initial << '\n';
}
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
long long n; cin>>n; long long k=1;
while (k*k<n) ++k;
cout<<k<<'\n';
}
B. 宝石接力
令 ansi 表示第 i 位精灵的最早接收时间,先初始化为直接收到宝石的时间 Ti。若已经知道精灵 i 的答案,就能更新下一位:
ansi+1=min(ansi+1,ansi+Si).
选择 Ti 最小的位置 k 作为起点。由于传递需要正时间,任何宝石都不可能在全局最早的直接发放时刻之前出现,所以 ansk=Tk 已经确定。
从 k 沿传递方向走一圈即可。把环在 k 处切开:任何绕过切口、再次到达 k 的传递,都会晚于 Tk,用在 k 直接收到的宝石代替它只会更早。因此最优传播不需要绕过这个切口。切开后的每个位置只有前一个位置能向它传递,按顺序松弛即可在处理后确定其最优值。
一圈完成后输出全部答案。时间复杂度 O(N),空间复杂度 O(N)。使用 long long 保存时间和相加结果。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin>>n; vector<long long>s(n),ans(n);
for (auto &x:s) cin>>x;
for (auto &x:ans) cin>>x;
int start=min_element(ans.begin(),ans.end())-ans.begin();
for (int step=0;step<n;++step) {
int u=(start+step)%n,v=(u+1)%n;
ans[v]=min(ans[v],ans[u]+s[u]);
}
for (auto x:ans) cout<<x<<'\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 pos = 0, l = 1; bool ok = true;
while (pos < n && ok) {
int r = a[pos];
if (r < l) { ok = false; break; }
for (int x = r; x >= l; --x) {
if (pos >= n || a[pos] != x) { ok = false; break; }
++pos;
}
l = r + 1;
}
cout << (ok ? "yes\n" : "no\n");
}
}
竖放的胡萝卜占据同一列的上下两个格子,因此看到 U 时另一行对应位置是 D,看到 D 时另一行对应位置是 U。
横放时,当前行的 LR 已经占据相邻两列。另一行这两格不能与当前行组成竖放胡萝卜,只能也横放为 LR。于是逐字符把 U、D 互换,L、R 保持不变即可。输入保证合法,因此不会出现无法配对的横放片段。
每个位置都按其所在胡萝卜的方向确定另一行,所得结果覆盖全部格子且没有重叠,满足题意。
每组时间复杂度 O(n),存储字符串占用 O(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; string s; cin >> n >> s;
for (char &c : s) {
if (c == 'U') c = 'D'; else if (c == 'D') c = 'U';
}
cout << s << '\n';
}
}
分别尝试这两个候选值 x:把数组中所有等于 x 的元素跳过,再判断剩余序列是否回文。这里允许删掉全部 x,不会缩小可行方案范围:若原先存在仅删除部分 x 的回文方案,从该回文中继续删掉全部 x,剩余部分仍是回文。因此只需检查“删除全部 x”这一种情况。
两次检查总时间复杂度 O(n),存储数组占用 O(n) 空间。
#include <bits/stdc++.h>
using namespace std;
bool check(const vector<int>& a, int x) {
int l = 0, r = (int)a.size() - 1;
while (l < r) {
if (a[l] == x) { ++l; continue; }
if (a[r] == x) { --r; continue; }
if (a[l] != a[r]) return false;
++l; --r;
}
return true;
}
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 l = 0, r = n - 1;
while (l < r && a[l] == a[r]) { ++l; --r; }
bool ok = l >= r || check(a, a[l]) || check(a, a[r]);
cout << (ok ? "YES\n" : "NO\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 dp[2] = {0, 0};
auto left = [&](int i, int s) { return s ? a[n-1-i] : a[i]; };
auto right = [&](int i, int s) { return s ? a[i] : a[n-1-i]; };
int m = n / 2;
for (int i = 1; i < m; ++i) {
int nd[2] = {INT_MAX
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n;
while (n--) {
long long a; cin >> a;
if (a <= 10000) cout << a << '\n';
else if (a <= 50000) cout << a * 7 / 10 << '\n';
else cout << a / 2 << '\n';
}
}
C. 编程对决
先判断 ∣a−b∣≤k。题目规定分差恰好等于 k 时也要重赛,因此这一分支必须包含等号。未触发重赛时,再比较 a,b,较大者获胜。
三个分支覆盖所有可能:分差在阈值内、分差超出阈值且 a>b、分差超出阈值且 b>a。按规则输出对应字符串即可。输入可达 1018,使用 long long。
时间复杂度 O(1),额外空间复杂度 O(1)。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
long long a, b, k; cin >> a >> b >> k;
if (llabs(a - b) <= k) cout << "again\n";
else if (a > b) cout << "karl\n";
else cout << "baozi\n";
}
排序后,排除一个位置时,剩余的两个最小值一定在原数组最前面的三个位置中;两个最大值一定在最后面的三个位置中。因此枚举 k 时,各检查至多三个候选位置,就能取得最优的 ai+aj,同时保证三个下标互不相同。
任意三元组都有一个对应的 k,而程序为该 k 选择了最优的另外两个位置,故枚举所得最小值不会遗漏最优方案。ak=0 时任取另外两个位置都会得到 0,也包含在上述处理内。
时间复杂度 O(nlogn),空间复杂度 O(n)。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n; vector<long long> a(n);
for (auto &x : a) cin >> x;
sort(a.begin(), a.end());
long long ans = LLONG_MAX;
for (int k = 0; k < n; ++k) {
long long sum = 0; int used = 0;
if (a[k] >= 0) {
for (int i = 0; used < 2; ++i) if (i != k) { sum += a[i]; ++used; }
} else {
for (int i = n - 1; used < 2; --i) if (i != k) { sum += a[i]; ++used; }
}
ans = min(ans, sum * a[k]);
}
cout << ans << '\n';
}
n 可达 109,但只有出现不可用卡槽的通道需要处理。先令答案为 2n,用映射为这些通道记录一个十位二进制掩码,再将每条受影响通道的贡献从 2 改成实际值。
使用有序映射时,时间复杂度 O(mlogm),空间复杂度 O(m)。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
ll n; int m; cin >> n >> m; map<ll, int> blocked;
for (int i = 0; i < m; ++i) {
ll r; int c; cin >> r >> c; blocked[r] |= 1 << (c - 1);
}
int L = 0, C = 0, R = 0;
for (int c = 2; c <= 5; ++c) L |= 1 << (c - 1);
for (int c = 4; c <= 7; ++c) C |= 1 << (c - 1);
for (int c = 6; c <= 9; ++c) R |= 1 << (c - 1);
ll ans = 2 * n;
for (auto item : blocked) {
int mask = item.second;
bool l = !(mask & L), c = !(mask & C), r = !(mask & R);
int best = l && r ? 2 : (l || c || r ? 1 : 0);
ans += best - 2;
}
cout << ans << '\n';
}
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n;
long long ans = LLONG_MAX;
while (n--) { long long x; cin >> x; ans = min(ans, x); }
cout << ans << '\n';
}
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
long long a; vector<long long> p(3);
cin >> a >> p[0] >> p[1] >> p[2];
sort(p.begin(), p.end());
long long ans = LLONG_MAX;
do {
ans = min(ans, llabs(a - p[0]) + llabs(p[0] - p[1]) + llabs(p[1] - p[2]));
} while (next_permutation(p.begin(), p.end()));
cout << ans << '\n';
}
C. 菠萝包
按时间顺序维护当前价格。时刻 0 先判断是否已经买得起;经过第 t 小时后,减去 ⌈t/10⌉=(t+9)/10,再判断是否不超过 y。
每次检查的价格恰好是题目定义的 p(t)。按照 0,1,…,n 的顺序检查,第一次满足条件的时刻就是最早购买时刻。第 n 小时仍可购买,因此必须检查到 t=n;全部不满足才输出 IMPOSSIBLE。
时间复杂度 O(n),额外空间复杂度 O(1)。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
long long x, y; int n; cin >> x >> n >> y;
if (x <= y) { cout << 0 << '\n'; return 0; }
for (int t = 1; t <= n; ++t) {
x -= (t + 9) / 10;
if (x <= y) { cout << t << '\n'; return 0; }
}
cout << "IMPOSSIBLE\n";
}
每一对下标要么相等、要么不等,两种情况不重不漏,所以上式得到所求答案。答案可能超过 32 位整数,乘法和累计值均用 long long。
排序占用 O(nlogn) 时间,存储序列占用 O(n) 空间。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n; vector<long long> a(n);
for (auto &x : a) cin >> x;
sort(a.begin(), a.end());
long long ans = 1LL * n * (n - 1) / 2;
for (int i = 0; i < n;) {
int j = i; while (j < n && a[j] == a[i]) ++j;
long long c = j - i; ans -= c * (c - 1) / 2; i = j;
}
cout << ans << '\n';
}
E. 高坚果
每次移动都会让列号增加 1,所以一个高坚果至多访问 n 个格子。直接逐个高坚果、逐列模拟,总移动次数至多为 kn≤108。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, m, k; cin >> n >> m >> k;
vector<vector<unsigned char>> alive(n + 1, vector<unsigned char>(n + 1, 0));
for (int i = 0, x, y; i < m; ++i) { cin >> x >> y; alive[x][y] = 1; }
int left = m;
while (k--) {
int x, d; cin >> x >> d;
int step = d == 0 ? -1 : 1;
for (int y = 1; y <= n; ++y) {
if (alive[x][y]) { alive[x][y] = 0; --left; step = -step; }
if (y == n) break;
if (x + step < 1 || x + step > n) step = -step;
x += step;
}
}
cout << left << '\n';
}