A.Divisible(循环)
题意
给出一个包含NNN个正整数的数组AAA以及一个正整数KKK,请你输出AAA数组所有KKK的倍数除以KKK的结果。
分析
输入后判断AiA_iAi是否为KKK的倍数,如果是输出Ai/KA_i / KAi/K即可。
代码
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (a % k == 0) {
cout << a / k << ' ';
}
}
cout << endl;
}
int main() {
solve();
return 0;
}
B.Substring(枚举,set)
题意
给出一个字符串SSS,问字符串SSS包含多少种不同的子串。
分析
由于数据量较小,可以枚举字符串SSS所有的子串,然后对子串进行去重(使用set,map,unique等方法均可),去完重后剩余的子串数量即为答案。
代码
#include<bits/stdc++.h>
using namespace std;
set<string> St;
void solve() {
string s;
cin >> s;
int n = s.size();
for (int i = 0; i < n; i++) {
string str = "";
for (int j = i; j < n; j++) {
str += s[j];
St.insert(str);
}
}
cout << St.size() << endl;
}
int main() {
solve();
return 0;
}
C.Ideal Holidays(哈希)
题意
在AtCoder王国中,一周包含A+BA + BA+B天,其中前AAA天为周末,第A+1∼A+BA+1 \sim A+BA+1∼A+B天为工作日。
高桥有NNN个计划,第iii个计划将在DiD_iDi天后执行。
但是他忘了今天是本周的第几天,问,这NNN个计划全部都在周末?
分析
坑点:没有告诉你当天是星期几,那么我们可以认为,只要存在一种方案使得计划全部成立即可,开始是星期几我们可以随意决定。
由于星期数是循环的,即以A+BA + BA+B为一个周期进行循环,那么我们可以利用类似哈希表的思想,让所有的天数通过取模落在A+BA + BA+B天内,并记录所有取模后的结果。
然后,只要满足以下两个条件之一,就表示一定存在合法的方案:
条件1:取模后的最大最小值之差小于AAA
如下图,由于我们可以决定开始时是星期几,那么只要取模后的区间大小小于等于AAA(即取模后的最大最小值之差小于AAA),那么就可以将区间最小值修改为一周的开始,此时所有取模后的结果均会落在前AAA天内。

条件2:将数组排序,存在数组中相邻两项之差大于BBB
如下图,虽然最大最小值之差很大,但由于中间空出了一段长度大于等于BBB的区间,那么通过循环让这段空出的区间来到后半部分,那么所有计划也会落在前AAA天内。

如果上述两种情况均不满足,那么就表示当前情况无解。
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> v;
void solve() {
ll n, a, b;
cin >> n >> a >> b;
ll minn = 1e18, maxn = -1e18;
for (int i = 0; i < n; i++) {
ll t;
cin >> t;
t--;
t %= (a + b);
v.push_back(t);
maxn = max(maxn, t);
minn = min(minn, t);
}
sort(v.begin(), v.end());
for (int i = 1; i < n; i++) {
if (v[i] - v[i - 1] > b) {
cout << "Yes" << endl;
return;
}
}
if (maxn - minn >= a) cout << "No" << endl;
else cout << "Yes" << endl;
}
int main() {
solve();
return 0;
}
D.Popcount and XOR(思维)
题意
给出三个非负整数a,b,Ca, b, Ca,b,C,请你找出是否存在一组非负整数(X,Y)(X,Y)(X,Y)满足以下所有条件,如果存在多组答案,输出任意一组即可。
-
0≤X<2600 \le X < 2^{60}0≤X<260
-
0≤Y<2600 \le Y < 2^{60}0≤Y<260
-
popcount(X)=apopcount(X) = apopcount(X)=a
-
popcount(Y)=bpopcount(Y) = bpopcount(Y)=b
-
X⊕Y=CX \oplus Y = CX⊕Y=C
⊕\oplus⊕表示二进制异或运算,popcount(i)popcount(i)popcount(i)表示统计数字iii对应的二进制数字有多少数字位上是111。
分析
由于CCC是通过异或(二进制同一位上相同出000,不同出111)得到的,那么将CCC转化为二进制串后,每一个二进制位上如果为111,就需要XXX和YYY中其中一个数对应二进制位上一个为000,一个为111,那么所需的111的总数必然为popcount(C)popcount(C)popcount(C),如果a+b<popcount(C)a + b < popcount(C)a+b<popcount(C)必然无解。当a+b−popcount(C)a + b - popcount(C)a+b−popcount(C)的结果大于等于000且是偶数时,将多余的二进制位放在XXX和YYY两数同一个二进制位上即可。
代码
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
int x[105], y[105], c[105];
void solve() {
LL a, b, C;
cin >> a >> b >> C;
int pos = 0, cnt = 0;
while (C) {
if (C & 1) cnt++;
c[pos++] = C % 2;
C /= 2;
}
for (int i = 0; i <= 62; i++) {
if (c[i] == 1) {
if (a > b) {
x[i] = 1;
a--;
} else {
y[i] = 1;
b--;
}
}
}
for (int i = 0; i <= 62; i++) {
if (x[i] + y[i] == 0 && a > 0 && b > 0) {
a--, b--;
x[i] = y[i] = 1;
}
}
if (a != 0 || b != 0) {
cout << -1 << endl;
return;
}
LL X = 0, Y = 0;
for (int i = 62; i >= 0; i--) {
X = X * 2 + x[i];
Y = Y * 2 + y[i];
}
cout << X << ' ' << Y << endl;
}
int main() {
solve();
return 0;
}
E.Set Add Query
题意
给出一个包含NNN个数字的数组A=(A1,A2,…,AN)A = (A_1, A_2, \ldots, A_N)A=(A1,A2,…,AN),开始时数组中所有元素均为0。同时,还有一个集合SSS,开始也是空的。你将按顺序执行QQQ个操作,请你找到QQQ次操作后的数组AAA。
第iii个操作将遵循以下规则:
-
对于一个给出的数字xix_ixi。如果xix_ixi在集合SSS中,在集合SSS中将xix_ixi删除。如果不在集合中,将该数字放入集合中。
-
然后将所有Aj(j∈S)A_j(j \in S)Aj(j∈S)加上∣S∣|S|∣S∣。
其中,∣S∣|S|∣S∣表示集合SSS中的元素数量。
分析
对于每个元素,我们只需要考虑他什么时候入集合,什么时候出集合即可。在进入集合到离开集合前,所有的集合大小均需要被加到这个下标上,因此,可以预处理第iii次操作后集合的大小sz[i]sz[i]sz[i],并维护前缀和pre[i]pre[i]pre[i]表示∑j=1isz[j]\sum\limits_{j = 1}^{i}sz[j]j=1∑isz[j]。
在模拟集合操作时,使用两个vector同步记录每个数字进入集合和离开集合的时间(最后对所有数字补上一个离开集合的时间Q+1Q + 1Q+1,保证后面的操作也被计算到)。
结束预处理后,遍历每个数字进出集合的vector,令ini,jin_{i, j}ini,j表示数字iii第jjj次进入集合的时间,outi,jout_{i, j}outi,j为数字iii第jjj次离开集合的时间,那么结束时:
- Ai=∑j=0in[i].size()−1pre[out[i][j]−1]−pre[in[i][j]−1]A_i = \sum\limits_{j = 0}^{in[i].size() - 1}pre[out[i][j] - 1] - pre[in[i][j] - 1]Ai=j=0∑in[i].size()−1pre[out[i][j]−1]−pre[in[i][j]−1]。
hint: 记得开long long!!!.
代码
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int N = 2e5 + 5e2;
set<int> S;
LL n, q, sz[N], pre[N], ans[N];
vector<int> in[N], out[N];
void solve() {
cin >> n >> q;
for (int i = 1; i <= q; i++) {
int x;
cin >> x;
if (S.find(x) == S.end()) {
S.insert(x);
in[x].push_back(i);
} else {
S.erase(x);
out[x].push_back(i);
}
sz[i] = S.size();
pre[i] = pre[i - 1] + sz[i];
}
for (int i = 1; i <= n; i++) out[i].push_back(q + 1);
for (int i = 1; i <= n; i++) {
int len = in[i].size();
for (int j = 0; j < len; j++) {
ans[i] += pre[out[i][j] - 1] - pre[in[i][j] - 1];
}
cout << ans[i] << ' ';
}
cout << endl;
}
int main() {
solve();
return 0;
}
F.Non-overlapping Squares(思维)
题意
有一个N×NN \times NN×N的网格和一个数字MMM,请你找出三个不重叠的M×MM \times MM×M的网格,使得三个网格的数字总和最大。
分析
对于所有的方案,必然可以将三个网格的位置分为以下六种。

可以对于所有的N×NN \times NN×N条划线方案进行枚举,并找到每个区域中最大的M×MM \times MM×M的矩阵数字总和
对于上述的方法,可以采用二维线段树或二维ST表来快速找到一个区域中的矩阵最大值。
代码(来自官方题解)
//from atcoder Offcial
#include <iostream>
#include <vector>
#include <ranges>
#include <numeric>
#include <algorithm>
int main() {
using namespace std;
static constexpr auto chmax{[](auto &&x, const auto &y) {
if (x < y) x = y;
return x;
}};
static constexpr auto max{[](const auto &x, const auto &y) {
if (x < y) return y;
return x;
}};
unsigned N, M;
cin >> N >> M;
// sum[i][j] := the sum of the M x M square whose top-left cell is (i, j)
auto sum{[N, M] {
vector A(N + 1, vector<unsigned long>(N + 1));
for (auto &&row: A | views::take(N))
for (auto &&a: row | views::take(N))
cin >> a;
// Let A[i][j] ← ∑_{i≤k,j≤l} A[k][l]
for (unsigned row_index{N}; auto &&row : A | views::reverse){
inclusive_scan(rbegin(row), rend(row), rbegin(row), plus<>{});
if (row_index < N)
ranges::transform(row, A[row_index + 1], begin(row), plus<>{});
--row_index;
}
// sum[i][j] = A[i][j] - A[i+M][j] - A[i][j+M] + A[i+M][j+M]
vector sum(N - M + 1, vector<unsigned long>(N - M + 1));
for (unsigned i{}; i <= N - M; ++i)
for (unsigned j{}; j <= N - M; ++j)
sum[i][j] = A[i][j] - A[i + M][j] - A[i][j + M] + A[i + M][j + M];
return sum;
}()};
// cumulative max of sum[i][j] from the top-left
// m[i][j] = max_{k≤i,l≤j} sum[k][l]
const auto max_UL{[](auto cells) {
for (unsigned row_index{}; auto &&row : cells){
inclusive_scan(begin(row), end(row), begin(row), max);
if (row_index)
ranges::transform(row, cells[row_index - 1], begin(row), max);
++row_index;
}
return cells;
}};
// flip the board horizontally
const auto h_flip{[](auto &&cells) {
for (auto &&row: cells)
ranges::reverse(row);
return cells;
}};
// flip the board vertically
const auto v_flip{[](auto &&cells) {
ranges::reverse(cells);
return cells;
}};
const auto upper_left{max_UL(sum)}; // cumulative max from the top-left
h_flip(sum); // flip horizontally
const auto upper_right{h_flip(max_UL(sum))}; // cumulative max from the top-right
v_flip(sum); // flip vertically
const auto lower_right{h_flip(v_flip(max_UL(sum)))}; // cumulative max from the bottom-right
h_flip(sum); // flip vertically
const auto lower_left{v_flip(max_UL(sum))}; // cumulative max from the bottom-left
v_flip(sum); // return to the original state
unsigned long ans{};
// Fix a square
for (unsigned i{}; i < N - M + 1; ++i)
for (unsigned j{}; j < N - M + 1; ++j) {
if (M <= i && i + M < N - M + 1) // three squares arranged vertically; fix the center square
chmax(ans, upper_left[i - M].back() + sum[i][j] + lower_right[i + M].front());
if (M <= j && j + M < N - M + 1) // three squares arranged horizontally; fix the center square
chmax(ans, upper_left.back()[j - M] + sum[i][j] + lower_right.front()[j + M]);
if (M <= i) { // T-shaped
if (j + M < N - M + 1) // bottom left
chmax(ans, upper_left[i - M].back() + lower_right[i][j + M] + sum[i][j]);
if (M <= j) // bottom right
chmax(ans, upper_left[i - M].back() + lower_left[i][j - M] + sum[i][j]);
}
if (M <= j) { // ト-shaped
if (i + M < N - M + 1) // bottom right
chmax(ans, lower_left.front()[j - M] + lower_right[i + M][j] + sum[i][j]);
if (M <= i) // top right
chmax(ans, lower_left.front()[j - M] + upper_right[i - M][j] + sum[i][j]);
}
if (i + M < N - M + 1) { // 亠 -shaped
if (j + M < N - M + 1) // top-left
chmax(ans, lower_right[i + M].front() + upper_right[i][j + M] + sum[i][j]);
if (M <= j) // top-right
chmax(ans, lower_right[i + M].front() + upper_left[i][j - M] + sum[i][j]);
}
if (j + M < N - M + 1) { // ㅓ -shaped
if (i + M < N - M + 1) // top-left
chmax(ans, upper_right.back()[j + M] + lower_left[i + M][j] + sum[i][j]);
if (M <= i) // bottom-left
chmax(ans, upper_right.back()[j + M] + upper_left[i - M][j] + sum[i][j]);
}
}
cout << ans << endl;
return 0;
}
赛后交流
在比赛结束后,会在交流群中给出比赛题解,同学们可以在赛后查看题解进行补题。
群号: 704572101,赛后大家可以一起交流做题思路,分享做题技巧,欢迎大家的加入。

IT技术下的编程题目集:循环、哈希、子串与逻辑操作
2502

被折叠的 条评论
为什么被折叠?



