A.Divisible(循环)
题意
给出一个包含 N N N个正整数的数组 A A A以及一个正整数 K K K,请你输出 A A A数组所有 K K K的倍数除以 K K K的结果。
分析
输入后判断 A i A_i Ai是否为 K K K的倍数,如果是输出 A i / K A_i / K Ai/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)
题意
给出一个字符串 S S S,问字符串 S S S包含多少种不同的子串。
分析
由于数据量较小,可以枚举字符串 S S S所有的子串,然后对子串进行去重(使用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 + B A + B A+B天,其中前 A A A天为周末,第 A + 1 ∼ A + B A+1 \sim A+B A+1∼A+B天为工作日。
高桥有 N N N个计划,第 i i i个计划将在 D i D_i Di天后执行。
但是他忘了今天是本周的第几天,问,这 N N N个计划全部都在周末?
分析
坑点:没有告诉你当天是星期几,那么我们可以认为,只要存在一种方案使得计划全部成立即可,开始是星期几我们可以随意决定。
由于星期数是循环的,即以 A + B A + B A+B为一个周期进行循环,那么我们可以利用类似哈希表的思想,让所有的天数通过取模落在 A + B A + B A+B天内,并记录所有取模后的结果。
然后,只要满足以下两个条件之一,就表示一定存在合法的方案:
条件1:取模后的最大最小值之差小于 A A A
如下图,由于我们可以决定开始时是星期几,那么只要取模后的区间大小小于等于 A A A(即取模后的最大最小值之差小于 A A A),那么就可以将区间最小值修改为一周的开始,此时所有取模后的结果均会落在前 A A A天内。
条件2:将数组排序,存在数组中相邻两项之差大于 B B B
如下图,虽然最大最小值之差很大,但由于中间空出了一段长度大于等于 B B B的区间,那么通过循环让这段空出的区间来到后半部分,那么所有计划也会落在前 A A A天内。
如果上述两种情况均不满足,那么就表示当前情况无解。
代码
#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 , C a, b, C a,b,C,请你找出是否存在一组非负整数 ( X , Y ) (X,Y) (X,Y)满足以下所有条件,如果存在多组答案,输出任意一组即可。
-
0 ≤ X < 2 60 0 \le X < 2^{60} 0≤X<260
-
0 ≤ Y < 2 60 0 \le Y < 2^{60} 0≤Y<260
-
p o p c o u n t ( X ) = a popcount(X) = a popcount(X)=a
-
p o p c o u n t ( Y ) = b popcount(Y) = b popcount(Y)=b
-
X ⊕ Y = C X \oplus Y = C X⊕Y=C
⊕ \oplus ⊕表示二进制异或运算, p o p c o u n t ( i ) popcount(i) popcount(i)表示统计数字 i i i对应的二进制数字有多少数字位上是 1 1 1。
分析
由于 C C C是通过异或(二进制同一位上相同出 0 0 0,不同出 1 1 1)得到的,那么将 C C C转化为二进制串后,每一个二进制位上如果为 1 1 1,就需要 X X X和 Y Y Y中其中一个数对应二进制位上一个为 0 0 0,一个为 1 1 1,那么所需的 1 1 1的总数必然为 p o p c o u n t ( C ) popcount(C) popcount(C),如果 a + b < p o p c o u n t ( C ) a + b < popcount(C) a+b<popcount(C)必然无解。当 a + b − p o p c o u n t ( C ) a + b - popcount(C) a+b−popcount(C)的结果大于等于 0 0 0且是偶数时,将多余的二进制位放在 X X X和 Y Y Y两数同一个二进制位上即可。
代码
#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
题意
给出一个包含 N N N个数字的数组 A = ( A 1 , A 2 , … , A N ) A = (A_1, A_2, \ldots, A_N) A=(A1,A2,…,AN),开始时数组中所有元素均为0。同时,还有一个集合 S S S,开始也是空的。你将按顺序执行 Q Q Q个操作,请你找到 Q Q Q次操作后的数组 A A A。
第 i i i个操作将遵循以下规则:
-
对于一个给出的数字 x i x_i xi。如果 x i x_i xi在集合 S S S中,在集合 S S S中将 x i x_i xi删除。如果不在集合中,将该数字放入集合中。
-
然后将所有 A j ( j ∈ S ) A_j(j \in S) Aj(j∈S)加上 ∣ S ∣ |S| ∣S∣。
其中, ∣ S ∣ |S| ∣S∣表示集合 S S S中的元素数量。
分析
对于每个元素,我们只需要考虑他什么时候入集合,什么时候出集合即可。在进入集合到离开集合前,所有的集合大小均需要被加到这个下标上,因此,可以预处理第 i i i次操作后集合的大小 s z [ i ] sz[i] sz[i],并维护前缀和 p r e [ i ] pre[i] pre[i]表示 ∑ j = 1 i s z [ j ] \sum\limits_{j = 1}^{i}sz[j] j=1∑isz[j]。
在模拟集合操作时,使用两个vector
同步记录每个数字进入集合和离开集合的时间(最后对所有数字补上一个离开集合的时间
Q
+
1
Q + 1
Q+1,保证后面的操作也被计算到)。
结束预处理后,遍历每个数字进出集合的vector
,令
i
n
i
,
j
in_{i, j}
ini,j表示数字
i
i
i第
j
j
j次进入集合的时间,
o
u
t
i
,
j
out_{i, j}
outi,j为数字
i
i
i第
j
j
j次离开集合的时间,那么结束时:
- A i = ∑ j = 0 i n [ i ] . s i z e ( ) − 1 p r e [ o u t [ i ] [ j ] − 1 ] − p r e [ i n [ 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 × N N \times N N×N的网格和一个数字 M M M,请你找出三个不重叠的 M × M M \times M M×M的网格,使得三个网格的数字总和最大。
分析
对于所有的方案,必然可以将三个网格的位置分为以下六种。
可以对于所有的 N × N N \times N N×N条划线方案进行枚举,并找到每个区域中最大的 M × M M \times M M×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,赛后大家可以一起交流做题思路,分享做题技巧,欢迎大家的加入。