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 题意 给出两个数组,找出二者最短的公共子序 ...
随机推荐
- .NET Core 处理 WebAPI JSON 返回烦人的null为空
前言 项目开发中不管是前台还是后台都会遇到烦人的null,数据库表中字段允许空值,则代码实体类中对应的字段类型为可空类型Nullable<>,如int?,DateTime?,null值字段 ...
- 【JavaWeb】书城项目
书城网站 项目说明 项目地址 阶段一 登录.注册的验证 使用 jQuery 技术对登录中的用户名.密码进行非空验证: 使用 jQuery 技术和正则表达式对注册中的用户名.密码.确认密码.邮箱进行格式 ...
- linux网关服务器
问题 多台服务器在内网网段,其中只有一台有公网ip可以上外网,需要让所有服务器都能连接外网 解决思路 使用路由转发的方式,将拥有公网ip的服务器搭建为网关服务器,即作为统一的公网出口 所谓转发即当主机 ...
- 2021年正确的Android逆向开发学习之路
2021年正确的Android逆向开发学习之路 说明 文章首发于HURUWO的博客小站,本平台做同步备份发布.如有浏览或访问异常或者相关疑问可前往原博客下评论浏览. 原文链接 2021年正确的Andr ...
- 超精讲-逐例分析CS:LAB2-Bomb!(上)
0. 环境要求 关于环境已经在lab1里配置过了这里要记得安装gdb 安装命令 sudo yum install gdb 实验的下载地址 http://csapp.cs.cmu.edu/3e/labs ...
- React中的合成事件
React中的合成事件 React自己实现了一套高效的事件注册.存储.分发和重用逻辑,在DOM事件体系基础上做了很大改进,减少了内存消耗,简化了事件逻辑,并最大程度地解决了IE等浏览器的不兼容问题. ...
- eCATT使用前的配置
如果想在SAP中使用eCATT,必须做一下相关的配置才行,下面简单介绍这几步:1.SM30,输入表T000,然后点击维护,或者是进入事物SCC4,进入对应的clint属性编辑视图下,将CATT and ...
- playwright自动化项目搭建
这是关于playwright系列介绍的最后一篇.搭建基于 playwright 的自动化项目. GitHub地址: https://github.com/defnngj/playwright-pro ...
- springmvc 字符串转日期格式
http://www.mamicode.com/info-detail-2485490.html
- 前端知识(二)03-Webpack-谷粒学院
目录 一.什么是Webpack 二.Webpack安装 1.全局安装 2.安装后查看版本号 三.创建项目 1.初始化项目 2.创建src文件夹 3.src下创建common.js 4.src下创建ut ...