#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';
}
#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';}
本题的核心在于对给定函数 f(x) 进行多次复合运算。给定函数 f(x) 是一个定义在整数集 Z 上的映射:
f:x↦x2+2x+3
由于 t∈[0,10],即便经过四层嵌套,结果的量级依然在 long long 可表示的范围内。具体计算过程遵循代数替换原则,首先计算内层的 f(t),随后依次向外层展开。我们可以将计算过程拆解为如下步骤:设 a=f(t),b=f(a+t),c=f(a),d=f(c),则最终结果为 f(b+d)。这种递归式的结构可以通过定义一个简单的函数 f 并在主程序中按序调用来实现。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll f(ll x) {
return x * x + 2 * x + 3;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t;
cin >> t;
ll a = f(t);
ll b = f(a + t);
ll c = f(a);
ll d = f(b + c);
cout << d << endl;
return 0;
}
解释说明
计算 f(t)。
计算 f(f(t)+t) 和 f(f(t))。
将上述两个结果相加,最后再次调用 f 得到最终输出。
时间复杂度为 O(1),空间复杂度为 O(1)。
【模拟、条件统计】森林服务站平均处理时间统计
题意简述
给定 n 个小动物的类型集合 V={v1,v2,…,vn},其中 vi∈{′A′,′B′,′C′},以及对应的办理时间集合 T={t1,t2,…,tn}。要求针对每种类型 k∈{′A′,′B′,′C′},计算其所有对应个体办理时间的算术平均值,并向下取整输出。
数据范围:3≤n≤100,0≤ti≤100。
题目分析
本题属于分类统计问题。由于每种类型的小动物在对应的窗口按顺序办理,其办理时间相互独立,我们只需要维护三组累加器和计数器。设 Sk 为类型 k 的时间总和,Ck 为类型 k 的动物总数,则该类别的平均时间为 ⌊Sk/Ck⌋。
在数学表达上,对于类型 k∈{A,B,C},我们求解:
Ansk=⌊∑i=1n[vi=k]∑i=1n[vi=k]⋅ti⌋
其中 [condition] 为艾弗森括号(Iverson bracket)。由于 n 规模极小,直接遍历序列进行 O(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);
int n;
cin >> n;
char tp[105];
for (int i = 0; i < n; i++) {
cin >> tp[i];
}
ll s[3] = {0, 0, 0};
ll c[3] = {0, 0, 0};
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
int k = tp[i] - 'A';
s[k] += x;
c[k]++;
}
for (int i = 0; i < 3; i++) {
if (c[i] > 0) {
cout << s[i] / c[i] << endl;
}
}
return 0;
}
解释说明
时间复杂度为 O(n),空间复杂度为 O(n)。
【集合、排序】街道植树问题
题意简述
给定一个规模为 n 的整数集合 U={1,2,…,n},以及一个目标闭区间 I=[L,R]⊆U。已知集合 S={a1,a2,…,ak} 中的元素已被标记。求区间 I 中未被标记的元素个数,即计算集合差的基数:
ans=∣I∖S∣=∣I∣−∣I∩S∣
数据范围:n,L,R,ai≤1018,k≤105。注意 S 可能包含重复元素,且元素可能落在 I 之外。
题目分析
由于 n 的量级高达 1018,我们无法建立布尔数组或进行线性扫描。然而,已知标记点数量 k 较小(105),这暗示问题的复杂度应与 k 相关。
首先,目标区间 I 的总长度为 ∣I∣=R−L+1。为了求得 ∣I∩S∣,我们需要提取 S 中所有落在 [L,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);
ll n, k, l, r;
cin >> n >> k >> l >> r;
vector<ll> a;
for (int i = 0; i < k; i++) {
ll x;
cin >> x;
if (x >= l && x <= r) {
a.push_back(x);
}
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
ll res = (r - l + 1) - (ll)a.size();
cout << res << endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct E {
int t, u, v, id;
};
bool cmp(E a, E b) {
if (a.t != b.t) return a.t < b.t;
return a.id < b.id;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int x, n;
cin >> x >> n;
vector<E> q(n);
for (int i = 0; i < n; ++i) {
cin >> q[i].t >> q[i].u >> q[i].v;
q[i].id = i;
}
sort(q.begin(), q.end(), cmp);
ll cur = q[0].t;
int pos = x;
ll st = cur;
for (int i = 0; i < n; ++i) {
ll wat = 0;
if (q[i].t > cur) {
wat = q[i].t - cur;
cur += wat;
}
cur += abs(pos - q[i].u) * 3LL;
pos = q[i].u;
ll lod = 6;
if (i > 0 && q[i].u == q[i-1].v && wat == 0) {
lod = 0;
}
cur += lod;
cur += abs(q[i].u - q[i].v) * 3LL;
pos = q[i].v;
cur += 6;
}
cout << cur - st << "\n";
return 0;
}
代码解释
代码首先定义了一个结构体 E 来存储请求的时间、起点、终点和原始输入顺序 ID。使用 sort 函数配合自定义比较器 cmp 对请求进行排序,确保处理顺序符合题目要求(先按时间,时间相同按输入顺序)。
在模拟循环中:
变量 cur 维护当前全局时间,pos 维护卡车当前位置。
wat 计算卡车等待当前订单的时间。
在计算装货时间 lod 时,利用逻辑 i > 0 && q[i].u == q[i-1].v && wat == 0 来判断是否触发特殊规则(连续作业且地点衔接)。如果触发,装货时间设为 0,否则为 6。
所有距离计算均使用 abs 取绝对值并乘以 3。
最后输出 cur - st,即结束时间减去第一个订单的开始时间。
B【BFS、连通块】围棋
题目简述
在一个 19×19 的网格棋盘上进行围棋模拟。
给定 M 步落子操作,黑先白后。每一步落子后:
提对方子:检查并移除所有气数为 0 的对方连通组。
提己方子:检查并移除所有气数为 0 的己方连通组。
需输出每一步落子后,分别被移除的黑子和白子数量。
数据范围:M≤5×105,坐标 1≤x,y≤19。
题目分析
棋盘大小固定且较小 (19×19),但操作数 M 较大。这要求每次落子后的处理必须高效。
核心逻辑是计算连通块的气。我们可以利用 BFS(广度优先搜索)来查找连通块并判断是否有气。
对于每一步落子 (x,y)(颜色为 C,对手颜色为 O):
将 (x,y) 标记为 C。
处理对手:遍历 (x,y) 的上下左右四个相邻位置。若相邻位置有颜色为 O 的棋子,则对该棋子所在的连通块进行 BFS。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int g[25][25];
int vs[25][25];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
struct Res {
vector<pair<int, int>> p;
bool ok;
};
Res bfs(int r, int c, int col) {
Res ret;
ret.ok = false;
que
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int dp[505][512];
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, p, m;
cin >> n >> p >> m;
int k = n - p;
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int v = 1; v <= k; ++v) {
for (int j = 2 * v; j <= k; ++j) {
for (int x = 0; x < 512; ++x) {
dp[j][x] += dp[j - 2 * v][x];
if (dp[j][x] >= m) dp[j][x] -= m;
}
}
for (int j = k; j >= v; --j) {
for (int x = 0; x < 512; ++x) {
int px = x ^ v;
dp[j][x] += dp[j - v][px];
if (dp[j][x] >= m) dp[j][x] -= m;
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int n, m, k;
string s;
char a[105][105];
void go(char d) {
if (d == 'L') {
for (int i = 0; i < n; ++i) {
string t = "";
for (int j = 0; j < m; ++j) {
if (a[i][j] != '.') t += a[i][j];
}
for (int j = 0; j < m; ++j) {
if (j < t.size()) a[i][j] = t[j];
else a[i][j] = '.';
}
}
} else if (d == 'R') {
for (int i = 0; i < n; ++i) {
string t = "";
for (int j = 0; j < m; ++j) {
if (a[i][j] != '.') t += a[i][j];
}
for (int j = 0; j < m; ++j) {
if (j < m - t.size()) a[i][j] = '.';
else a[i][j] = t[j - (m - t.size())];
}
}
} else if (d == 'U') {
for (int j = 0; j < m; ++j) {
string t = "";
for (int i = 0; i < n; ++i) {
if (a[i][j] != '.') t += a[i][j];
}
for (int i = 0; i < n; ++i) {
if (i < t.size()) a[i][j] = t[i];
else a[i][j] = '.';
}
}
} else if (d == 'D') {
for (int j = 0; j < m; ++j) {
string t = "";
for (int i = 0; i < n; ++i) {
if (a[i][j] != '.') t += a[i][j];
}
for (int i = 0; i < n; ++i) {
if (i < n - t.size()) a[i][j] = '.';
else a[i][j] = t[i - (n - t.size())];
}
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> k;
if (k) cin >> s;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
for (char c : s) {
go(c);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cout << a[i][j];
}
cout << '\n';
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int len, n, m;
int a[50005];
// 贪心判定函数:判断最小距离为 x 时,移除数量是否 <= m
bool chk(int x) {
int cnt = 0; // 移除的小行星数量
int lst = 0; // 上一个落脚点的坐标(初始为起点0)
for(int i = 1; i <= n; ++i) {