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 题意 给出两个数组,找出二者最短的公共子序 ...
随机推荐
- swack的wiki站上线
swack的个人wiki网址:www.swack.cn [服务器破旧,速度较慢,见谅!]
- mysql 应用 持续更新
1.安装方法 贴出,my.ini # MySQL Server Instance Configuration File # -------------------------------------- ...
- 【剑指 Offer】04.二维数组中的查找
题目描述 在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...
- 为了加快速度,Redis都做了哪些“变态”设计
前言 列表对象是 Redis 中 5 种基础数据类型之一,在 Redis 3.2 版本之前,列表对象底层存储结构有两种:linkedlist(双端列表)和 ziplist(压缩列表),而在 Redis ...
- Nginx(六):配置解析之location解析
nginx成为非常流行的代理服务软件,最根本的原因也许是在于其强悍性能.但还有一些必要的条件,比如功能的完整,配置的易用,能够解决各种各样的实际需求问题,这些是一个好的软件的必备特性. 那么,今天我们 ...
- Upload - Labs (下)
Pass - 11: 1.查看源代码,发现进行了一次对后缀名替换成空格,因此考虑双写绕过, 2.上传成功, 关键代码: $is_upload = false; $msg = null; if (iss ...
- C# datagridview设置标题为汉语
正常情况下,在给datagridview绑定数据源之后,显示的是SQL语句中的栏位,如下 我们想让标题显示汉语,可以有一下两种方法 1.在SQL中设置列别名 SELECT TITLE AS '报警标题 ...
- salesforce零基础学习(一百)Mobile Device Tracking
本篇参考: Mobile Device Tracking (salesforce.com) UserDevice | SOAP API Developer Guide | Salesforce Dev ...
- Django中多表关联的展示问题:
增加一个知识点,当表中有多对多关联时,在前端展示的时候是一个列表,所以在展示的时候需要这样做: 表结构: class ProjectEnv(models.Model): project = model ...
- echarts图表X轴文字过长解决解决方案:根据文字长度自动旋转
Echarts 标签中文本内容太长的时候怎么办 ? 关于这个问题搜索一下,有很多解决方案.无非就是 省略(间隔显示).旋转文字方向.竖排展示 前面两种解决方案,就是echarts暴露的: { ax ...