Codeforces Round #675 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1422
A. Fence
题意
给出三条边 $a,b,c$,构造第四条边使得四者可以围成一个四边形。
题解
$d = max(a,b,c)$,可以将四条边中最长的两条边想象成一把可以开合的尺子。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int a, b, c;
cin >> a >> b >> c;
cout << max({a, b, c}) << "\n";
}
return 0;
}
B. Nice Matrix
题意
定义各行列数值均对称的矩阵为好矩阵,给出一个 $n \times m$ 的矩阵,每次操作可以给一个数加一或减一,计算将它转为好矩阵的最少操作次数。
题解
因为矩阵有两条中轴线,所以一个数最多需要与三个数对称相等,至于等的值,考虑 $1, 2, 6, 10$ :
对于 $1,10$,只要等的值位于 $[1,10]$ 内,将二者变为相等的操作次数是相同的;
同理,对于 $2,6$,只要等的值位于 $[2,6]$ 内,将二者变为相等的操作次数也是相同的。
所以,最终等的值选取所有数排序后的中间区间内的任一值均可。
代码
#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, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int> (m));
for (auto &v : a)
for (auto &x : v) cin >> x;
long long ans = 0;
vector<vector<bool>> vis(n, vector<bool> (m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
vector<int> v;
for (int x : {i, n - 1 - i}) {
for (int y : {j, m - 1 - j}) {
if (!vis[x][y]) {
v.push_back(a[x][y]);
vis[x][y] = true;
}
}
}
if (v.size() >= 2) {
sort(v.begin(), v.end());
for (auto i : v) ans += abs(i - v[1]);
}
}
}
cout << ans << "\n";
}
return 0;
}
C. Bargain
题意
给出一个数并抹去它的一段连续区间后将余下的数连接,计算所有可能得到的数之和。
题解
个位前的 $n-1$ 个数位有 $\frac{n(n-1)}{2}$ 个连续区间,所以个位可以作为个位加 $\frac{n(n-1)}{2}$ 次,个位之前的每个数位因为要抹去后面所有的数位,所以作为个位只可以加 $1$ 次;
同理:
十位可以作为十位加 $\frac{(n-1)(n-2)}{2}$ 次,十位之前的每个数位可以作为十位加 $2$ 次;
千位可以作为千位加 $\frac{(n-2)(n-3)}{2}$ 次,千位之前的每个数位可以作为千位加 $3$ 次;
…………
即每次将当前数位与之前数位的总和分别计算。
对于每次之前数位的总和可以维护一个前缀和。
代码
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
constexpr int MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
ll sum = 0;
for (char c : s) sum += c - '0';
ll ans = 0, p = 1;
for (ll i = s.size() - 1; i >= 0; i--) {
ll val = (s[i] - '0');
ll mul = i * (i + 1) / 2;
ll pre_val = (sum -= s[i] - '0');
ll pre_mul = s.size() - i;
ans += (val * mul + pre_val * pre_mul) % MOD * p % MOD;
ans %= MOD;
p = (p * 10) % MOD;
}
cout << ans << "\n";
return 0;
}
D. Returning Home
题意
有一个 $n \times n$ 的方阵,每次可以花 $1$ 秒走到相邻的方格,方阵中有一些传送点,传送到所在行或列的传送点不花费时间,计算从 $(sx,sy)$ 走到 $(fx,fy)$ 的最短时间。
题解
共可分为两种情况:
- 不经传送点直接从起点走到终点
- 经过一个或多个传送到走到终点
第一种情况:所花时间即 $abs(sx-fx)+abs(sy-fy)$ 。
第二种情况:以相同的端点分别对传送点的横、纵坐标从小到大两两相邻建边,然后计算从起点到每个传送点的最短距离,最后枚举最终转折点,答案即 $min(dis_i + abs(fx - x_i) + abs(fy - y_i))$ 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int sx, sy, fx, fy;
cin >> sx >> sy >> fx >> fy;
vector<int> x(m), y(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i];
}
vector<vector<pair<int, int>>> G(m);
vector<int> p(m);
iota(p.begin(), p.end(), 0);
//横坐标从小到大相邻建边
sort(p.begin(), p.end(), [&](int i, int j) {
return x[i] < x[j];
});
for (int i = 0; i + 1 < m; i++) {
int u = p[i];
int v = p[i + 1];
int w = x[p[i + 1]] - x[p[i]];
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
//纵坐标从小到大相邻建边
sort(p.begin(), p.end(), [&](int i, int j) {
return y[i] < y[j];
});
for (int i = 0; i + 1 < m; i++) {
int u = p[i];
int v = p[i + 1];
int w = y[p[i + 1]] - y[p[i]];
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
//初始答案为不经过传送点直接走到终点
long long ans = abs(sx - fx) + abs(sy - fy);
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pque;
vector<long long> dis(m);
vector<bool> vis(m);
//计算起点到每个传送点的初始距离
for (int i = 0; i < m; i++) {
pque.emplace(min(abs(sx - x[i]), abs(sy - y[i])), i);
}
//计算起点到每个传送点的最短距离
while (!pque.empty()) {
auto [d, u] = pque.top();
pque.pop();
if (vis[u]) continue;
vis[u] = true;
dis[u] = d;
for (auto [v, w] : G[u]) {
pque.emplace(d + w, v);
}
}
//枚举以哪个传送点为最后转折点走到终点
for (int i = 0; i < m; i++) {
ans = min(ans, dis[i] + abs(fx - x[i]) + abs(fy - y[i]));
}
cout << ans << "\n";
return 0;
}
Codeforces Round #675 (Div. 2)【ABCD】的更多相关文章
- Codeforces Round #682 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1438 A. Specific Tastes of Andre 题意 构造一个任意连续子数组元素之和为子数组长度倍数的数组. ...
- Codeforces Round #678 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1436 A. Reorder 题解 模拟一下这个二重循环发现每个位置数最终都只加了一次. 代码 #include <bi ...
- Codeforces Round #676 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1421 A. XORwice 题意 给出两个正整数 \(a.b\),计算 \((a \oplus x) + (b \oplus ...
- Codeforces Round #668 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1405 A. Permutation Forgery 题意 给出一个大小为 $n$ 的排列 $p$,定义 \begin{equ ...
- Codeforces Round #732 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1546 A. AquaMoon and Two Arrays 题意 给出两个大小为 \(n\) 的数组 \(a, b\) ,每 ...
- Codeforces Round #677 (Div. 3)【ABCDE】
比赛链接:https://codeforces.com/contest/1433 A. Boring Apartments 题解 模拟即可. 代码 #include <bits/stdc++.h ...
- Codeforces Round #382 Div. 2【数论】
C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...
- Codeforces Round #684 (Div. 2)【ABC1C2】
比赛链接:https://codeforces.com/contest/1440 A. Buy the String 题解 枚举字符串中 \(0\) 或 \(1\) 的个数即可. 代码 #includ ...
- Codeforces Round #658 (Div. 2)【ABC2】
做完前四题还有一个半小时... 比赛链接:https://codeforces.com/contest/1382 A. Common Subsequence 题意 给出两个数组,找出二者最短的公共子序 ...
随机推荐
- html 垂直并列显示
笔者在制作登陆网页的时候,发现让input居中十分困难,笔者在网上搜了好久都没有结果,所以就想出了一个硬核的纯html的解决方法 直接上代码: <div style="text-ali ...
- 手把手教你用C语言编写一个哈希表
原文链接:https://www.swack.cn/wiki/001558681974020669b912b0c994e7090649ac4846e80b2000/001572849111298ae3 ...
- kafka 异步双活方案 mirror maker2 深度解析
mirror maker2背景 通常情况下,我们都是使用一套kafka集群处理业务.但有些情况需要使用另一套kafka集群来进行数据同步和备份.在kafka早先版本的时候,kafka针对这种场景就有推 ...
- 你一定需要知道的高阶JAVA枚举特性!
JAVA枚举,比你想象中还要有用! 我经常发现自己在Java中使用枚举来表示某个对象的一组潜在值. 在编译时确定类型可以具有什么值的能力是一种强大的能力,它为代码提供了结构和意义. 当我第一次了解枚举 ...
- LeetCode225 用队列实现栈
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...
- [Usaco2006 Nov]Corn Fields牧场的安排
题目描述 Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧场上的某几格土 ...
- scrapy-redis非多网址采集的使用
问题描述 默认RedisSpider在启动时,首先会读取redis中的spidername:start_urls,如果有值则根据url构建request对象. 现在的要求是,根据特定关键词采集. 例如 ...
- Go Concurrency Patterns: Context At Google, we require that Go programmers pass a Context parameter as the first argument to every function on the call path between incoming and outgoing requests.
小结: 1. Background is the root of any Context tree; it is never canceled: 2. https://blog.golang. ...
- springboot项目-声明式事务失效
1.项目背景 集成了shiro配置 2. 项目分析 由于ShiroFilterFactoryBean实现了FactoryBean接口,所以它会提前被初始化.又因为SecurityManager,Sec ...
- 【Coursera】Internet History 读书笔记
前言 这个Internet History 有些令人劝退.电脑无法播放视频.手机不能播放.最后百度了改了hosts文件才可以. 附上解决方法: 解决coursera可以登录但无法播放视频 第一周 第三 ...