Solution Set -「CF 1534」
这 1+2?
「CF1534 A」Colour the Flag
Link.
把 W / R 拉出来广搜,注意判断全空的情况。
#include <bits/stdc++.h>
using ll = long long;
#define all(x) (x).begin(), (x).end()
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T, n, m;
std::vector<std::vector<int>> DIR({{1, 0}, {0, 1}, {-1, 0}, {0, -1}});
for (std::cin >> T; T; --T) {
std::cin >> n >> m;
std::vector<std::vector<char>> a(n, std::vector<char>(m));
std::vector<std::vector<bool>> vis(n, std::vector<bool>(m));
std::queue<std::pair<int, int>> que;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
std::cin >> a[i][j];
if (a[i][j] != '.') {
que.emplace(i, j);
vis[i][j] = true;
}
}
}
if (que.empty()) {
a[0][0] = 'R';
que.emplace(0, 0);
vis[0][0] = true;
}
auto check = [&] (std::pair<int, int> x) {
return x.first < 0 || x.first >= n || x.second < 0 || x.second >= m;
};
bool flag = 0;
while (!que.empty()) {
auto x = que.front();
que.pop();
for (auto d : DIR) {
auto y = std::make_pair(x.first + d[0], x.second + d[1]);
if (check(y)) continue;
if (a[x.first][x.second] == a[y.first][y.second]) {
flag = true;
break;
}
if (vis[y.first][y.second]) continue;
vis[y.first][y.second] = true;
if (a[y.first][y.second] == '.') {
if (a[x.first][x.second] == 'R') a[y.first][y.second] = 'W';
else a[y.first][y.second] = 'R';
}
que.emplace(y);
}
if (flag) break;
}
if (flag) std::cout << "No\n";
else {
std::cout << "Yes\n";
for (auto x : a) {
for (auto y : x) std::cout << y;
std::cout << "\n";
}
}
}
return 0;
}
「CF1534 B」Histogram Ugliness
Link.
我们只会对比 \(i+1\) & \(i-1\) 都高 \(i\) 进行操作,然后答案显然。
#include <bits/stdc++.h>
using ll = long long;
#define all(x) (x).begin(), (x).end()
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T, n;
for (std::cin >> T; T; --T) {
std::cin >> n;
std::vector<int> a(n);
for (int &x : a) std::cin >> x;
a.emplace(a.begin(), 0);
a.emplace_back(0);
ll ans = 0;
for (int i = 1; i <= n + 1; ++i) ans += std::abs(a[i] - a[i - 1]);
for (int i = 1; i <= n; ++i) {
if (a[i] > std::max(a[i - 1], a[i + 1])) {
ans -= a[i] - std::max(a[i - 1], a[i + 1]);
a[i] = std::max(a[i - 1], a[i + 1]);
}
}
std::cout << ans << "\n";
}
return 0;
}
「CF1534 C」Little Alawn's Puzzle
Link.
钦定研究第一行。考虑 \(i\) 这个下标,我们对在第二行的 \(i\) 连个边,同时对在第二行的 \(p_{i}\) 的所在下标连边。
然后数出图里面有多少环,答案就是 \(2\) 的环数量次方。
#include <bits/stdc++.h>
using ll = long long;
#define all(x) (x).begin(), (x).end()
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T, n;
for (std::cin >> T; T; --T) {
std::cin >> n;
std::vector<std::vector<int>> a(2, std::vector<int>(n)), idx(2, std::vector<int>(n));
int cur = 0;
for (int &x : a[0]) {
std::cin >> x;
--x;
idx[0][x] = cur++;
}
for (int &x : a[1]) {
std::cin >> x;
--x;
idx[1][x] = cur++;
}
bool flag = false;
for (int i = 0; i < n; ++i) {
if (a[0][i] == a[1][i]) {
flag = true;
break;
}
}
if (flag) {
std::cout << "0\n";
continue;
}
std::vector<int> fa(n * 2);
std::iota(all(fa), 0);
auto find = [&] (int x) {
while (fa[x] != x) {
x = fa[x] = fa[fa[x]];
}
return x;
};
auto merge = [&] (int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
};
for (int i = 0; i < n; ++i) {
merge(i, i + n);
merge(idx[0][a[0][i]], idx[1][a[0][i]]);
}
int num = 0;
for (int i = 0; i < n * 2; ++i) {
if (fa[i] == i) ++num;
}
constexpr int P = 1e9 + 7;
auto power = [&] (int x, int y) {
int res = 1;
for (; y; y >>= 1, x = ll(x) * x % P)
if (y & 1) res = ll(res) * x % P;
return (res + P) % P;
};
std::cout << power(2, num) << "\n";
}
return 0;
}
「CF1534 D」Lost Tree
Link.
首先肯定要钦定一个根,对其进行一次询问。
然后查询出来的相当于是深度。观察次数限制 \(\lceil\frac{n}{2}\rceil\),大概是一半的节点。
考虑如何规划一半的节点去询问。一次询问能确定的边就是查询出来距离为 \(1\) 的。
注意到相邻奇数偶数之间总是相差 \(1\)。
然后把节点进行关于深度的奇偶分层,查询 奇 / 偶 中数量较少的节点即可。
#include <bits/stdc++.h>
using ll = long long;
#define all(x) (x).begin(), (x).end()
int main() {
// std::ios_base::sync_with_stdio(false);
// std::cin.tie(nullptr);
// std::cout.tie(nullptr);
int n;
std::cin >> n;
auto ask = [&] (int x) {
std::cout << "? " << x + 1 << "\n";
std::vector<int> res(n);
for (int &x : res) std::cin >> x;
return res;
};
std::vector<int> d = ask(0);
std::vector<std::pair<int, int>> ans;
std::vector<std::vector<int>> cat(2);
for (int i = 0; i < n; ++i) {
if (d[i] == 1) ans.emplace_back(std::make_pair(0, i));
}
for (int i = 1; i < n; ++i) {
if (d[i] & 1) cat[1].emplace_back(i);
else cat[0].emplace_back(i);
}
std::vector<int> point;
if (cat[0].size() > cat[1].size()) point = cat[1];
else point = cat[0];
for (int x : point) {
d = ask(x);
for (int i = 0; i < n; ++i) {
if (d[i] == 1) ans.emplace_back(std::make_pair(x, i));
}
}
for (auto &x : ans) {
if (x.first > x.second) std::swap(x.first, x.second);
}
std::sort(all(ans));
ans.erase(std::unique(all(ans)), ans.end());
std::cout << "!\n";
for (auto x : ans) std::cout << x.first + 1 << " " << x.second + 1 << "\n";
return 0;
}
「CF1534 E」Lost Array
Link.
将询问的次数看作一个状态,我们考虑它每次往哪里跑。
如果我们想知道新的 \(\texttt{XOR}\) 和可以选择全部查没选过的也可以部分选择。
那么就可以做了,因为 \(k\) 很小,BFS 搜即可。
#include <bits/stdc++.h>
using ll = long long;
#define all(x) (x).begin(), (x).end()
int main() {
// std::ios_base::sync_with_stdio(false);
// std::cin.tie(nullptr);
// std::cout.tie(nullptr);
constexpr int INF = std::numeric_limits<int>::max() / 2;
int n, k;
std::cin >> n >> k;
auto ask = [&] (std::vector<int> v) {
std::cout << "?";
for (int x : v) std::cout << " " << x + 1;
std::cout << "\n";
int res;
std::cin >> res;
return res;
};
auto link = [&] (std::vector<int> a, std::vector<int> b) {
std::vector<int> v;
for (int x : a) v.emplace_back(x);
for (int x : b) v.emplace_back(x);
return v;
};
std::vector<int> pre(n + 1, 0), dis(n + 1, INF);
std::queue<int> que;
pre[0] = -1;
dis[0] = 0;
que.emplace(0);
while (!que.empty()) {
int x = que.front();
que.pop();
for (int i = 1; i <= k; ++i) {
if (i <= n - x && k - i <= x) {
int y = x + i * 2 - k;
if (dis[y] == INF) {
dis[y] = dis[x] + 1;
pre[y] = x;
que.emplace(y);
}
}
}
}
if (dis[n] == INF) {
std::cout << "-1\n";
return 0;
}
std::vector<int> t, f(n), p;
for (int i = n; ~i; i = pre[i]) p.emplace_back(i);
std::reverse(all(p));
int ans = 0;
std::iota(all(f), 0);
for (size_t i = 0; i < p.size() - 1; ++i) {
int x = (p[i + 1] - p[i] + k) / 2, y = k - x;
std::vector<int> mt, mf;
for (int j = 0; j < x; ++j) {
mt.emplace_back(f.back());
f.pop_back();
}
for (int j = 0; j < y; ++j) {
mf.emplace_back(t.back());
t.pop_back();
}
ans ^= ask(link(mt, mf));
t.insert(t.end(), all(mt));
f.insert(f.end(), all(mf));
}
std::cout << "! " << ans << "\n";
return 0;
}
「CF1534 F1」Falling Sand (Easy Version)
Link.
考虑将一块沙块向其能影响到的沙块连有向边。
具体来讲,我们设 \(\textit{last}(i,j)\) 为第 \(i\) 行第 \(j\) 列往下望见的第一个沙块,若没有则设为 \(-1\)。然后连边方式就是(研究 \((i,j)\)):
- 首先 \((i,j)\) 本身是沙块;
- 向 \(\textit{last}(i,j)\) 连边(如果存在,下同);
- 若 \((i,j+1)\) 存在,则连 \((i,j+1)\),否则连 \(\textit{last}(i,j+1)\);
- \((i,j-1)\) 同理。
连出来一张图,你可能觉得这张图里面所有出度为 \(0\) 的就是答案,实则需要缩个点,然后才成立(样例 #2 就能 hack 非常良心)。
#include <bits/stdc++.h>
using ll = long long;
#define all(x) (x).begin(), (x).end()
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int n, m, cnt = 0;
std::cin >> n >> m;
std::vector<std::vector<char>> a(n, std::vector<char>(m));
std::vector<std::vector<int>> last(n, std::vector<int>(m)), id = last;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
std::cin >> a[i][j];
if (a[i][j] == '#') id[i][j] = cnt++;
}
}
for (int j = 0; j < m; ++j) {
int pos = -1;
for (int i = n - 1; ~i; --i) {
last[i][j] = pos;
if (a[i][j] == '#') pos = i;
}
}
int col = 0, tot = 0;
std::vector<std::pair<int, int>> edgeSet;
std::vector<std::vector<int>> e(cnt);
std::vector<int> color(cnt), order(cnt), low(cnt);
std::vector<bool> inStk(cnt);
std::stack<int> stk;
auto add = [&] (int x, int y) {
e[x].emplace_back(y);
edgeSet.emplace_back(std::make_pair(x, y));
};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[i][j] == '#') {
if (j < m - 1) {
if (a[i][j + 1] == '#') add(id[i][j], id[i][j + 1]);
else if (~last[i][j + 1]) add(id[i][j], id[last[i][j + 1]][j + 1]);
}
if (j > 0) {
if (a[i][j - 1] == '#') add(id[i][j], id[i][j - 1]);
if (~last[i][j - 1]) add(id[i][j], id[last[i][j - 1]][j - 1]);
}
if(~last[i][j]) add(id[i][j], id[last[i][j]][j]);
if (i > 0) {
if (a[i - 1][j] == '#') add(id[i][j], id[i - 1][j]);
}
}
}
}
std::function<void(int)> compress = [&] (int x) {
order[x] = low[x] = tot++;
inStk[x] = true;
stk.emplace(x);
for (int y : e[x]) {
if (!order[y]) {
compress(y);
low[x] = std::min(low[x], low[y]);
}
else if (inStk[y]) low[x] = std::min(low[x], order[y]);
}
if (order[x] == low[x]) {
int y = 0;
++col;
while (x != y) {
y = stk.top();
stk.pop();
color[y] = col;
inStk[y] = false;
}
}
};
for (int i = 0; i < cnt; ++i) {
if (!order[i]) compress(i);
}
std::vector<int> deg(col);
for (std::pair<int, int> edge : edgeSet) {
if (color[edge.first] != color[edge.second]) ++deg[color[edge.second]];
}
std::cout << std::count(all(deg), 0) << "\n";
return 0;
}
「CF1534 F2」Falling Sand (Hard Version)
Link.
「CF1534 G」A New Beginning
Link.
「CF1534 H」Lost Nodes
Link.
Solution Set -「CF 1534」的更多相关文章
- Diary / Solution Set -「WC 2022」线上冬眠做噩梦
大概只有比较有意思又不过分超出能力范围的题叭. 可是兔子的"能力范围" \(=\varnothing\) qwq. 「CF 1267G」Game Relics 任意一个 ...
- Solution Set -「ARC 107」
「ARC 107A」Simple Math Link. 答案为: \[\frac{a(a+1)\cdot b(b+1)\cdot c(c+1)}{8} \] 「ARC 107B」Quadrup ...
- Solution -「CF 1342E」Placing Rooks
\(\mathcal{Description}\) Link. 在一个 \(n\times n\) 的国际象棋棋盘上摆 \(n\) 个车,求满足: 所有格子都可以被攻击到. 恰好存在 \(k\ ...
- Solution -「CF 1622F」Quadratic Set
\(\mathscr{Description}\) Link. 求 \(S\subseteq\{1,2,\dots,n\}\),使得 \(\prod_{i\in S}i\) 是完全平方数,并最 ...
- Solution -「CF 923F」Public Service
\(\mathscr{Description}\) Link. 给定两棵含 \(n\) 个结点的树 \(T_1=(V_1,E_1),T_2=(V_2,E_2)\),求一个双射 \(\varph ...
- Solution -「CF 923E」Perpetual Subtraction
\(\mathcal{Description}\) Link. 有一个整数 \(x\in[0,n]\),初始时以 \(p_i\) 的概率取值 \(i\).进行 \(m\) 轮变换,每次均匀随机 ...
- Solution -「CF 1586F」Defender of Childhood Dreams
\(\mathcal{Description}\) Link. 定义有向图 \(G=(V,E)\),\(|V|=n\),\(\lang u,v\rang \in E \Leftrightarr ...
- Solution -「CF 1237E」Balanced Binary Search Trees
\(\mathcal{Description}\) Link. 定义棵点权为 \(1\sim n\) 的二叉搜索树 \(T\) 是 好树,当且仅当: 除去最深的所有叶子后,\(T\) 是满的: ...
- Solution -「CF 623E」Transforming Sequence
题目 题意简述 link. 有一个 \(n\) 个元素的集合,你需要进行 \(m\) 次操作.每次操作选择集合的一个非空子集,要求该集合不是已选集合的并的子集.求操作的方案数,对 \(10^9 ...
- Solution -「CF 1023F」Mobile Phone Network
\(\mathcal{Description}\) Link. 有一个 \(n\) 个结点的图,并给定 \(m_1\) 条无向带权黑边,\(m_2\) 条无向无权白边.你需要为每条白边指定边权 ...
随机推荐
- 自然语言处理 Paddle NLP - 文本语义相似度计算(ERNIE-Gram)
基于预训练模型 ERNIE-Gram 实现语义匹配 1. 背景介绍 文本语义匹配任务,简单来说就是给定两段文本,让模型来判断两段文本是不是语义相似. 在本案例中以权威的语义匹配数据集 LCQMC 为例 ...
- 深入理解Go语言接口
1. 引言 接口是一种定义了软件组件之间交互规范的重要概念,其促进了代码的解耦.模块化和可扩展性,提供了多态性和抽象的能力,简化了依赖管理和替换,方便进行单元测试和集成测试.这些特性使得接口成为构建可 ...
- EDP转LVDS屏转接板方案芯片CS5211替代CH7511B电路设计
CS5211替代CH7511B电路设计: CS5211用于设计DP转LVDS转换器,DP转LVDS控制板,DP转LVDS转接板等产品设计,其性能和参数可以替代与兼容PS8622,PS8625,CH75 ...
- 配置http协议访问Harbor镜像仓库
解决http: server gave HTTP response to HTTPS client问题,此问题在上传与下载时均可能出现. 由于docker镜像拉取与推送服务使用的是https协议,但是 ...
- java开发的配置文件配置到数据库(配置到配置文件里面个人感觉修改较麻烦,故配置到数据库)
配置文件的创建表sql CREATE TABLE `checkwork_tab_properties` ( `id` varchar(50) NOT NULL COMMENT '主键id', `typ ...
- java后台导出表格文件
Java类所需jar包 import java.io.File; import java.io.IOException; import java.io.InputStream; import java ...
- Web网页音视频通话之基于sipjs功能扩展
在上一篇开发基础上,已经实现了音视频通话.本文是在此基础上继续完成以下内容 关闭/开启音频 开启/关闭视频 屏幕共享 开启/关闭音频 javaScript /** * 静音 */ mute() { i ...
- 零基础实现Java直播(二):实现流程
一.前提条件 在实现Java直播前,请确保: 已在项目中集成 ZEGO Express SDK,详情请参考 快速开始 - 集成. 已在 ZEGO 控制台 创建项目,并申请有效的 AppID 和 App ...
- Matlab2014a 找不到 vs2015编译器解决方法
准备工作 前提: 电脑已经安装 1.Matlab版本2014a 2.VS版本2015 目标: 结合Matlab和VS2015,实现Matlab的GUI文件和.m文件转化为.exe文件,然后可以单独运行 ...
- 69.9K Star,最强开源内网穿透工具:frp
作为一名开发者,有很多场景需要用到内网穿透,比如:我们在接入一些大平台做第三方应用时,在本地开发微信公众号工具的时候需要让微信平台能否访问到本地提供的接口.除此之外,还有很多其他场景,也会用到,比如: ...