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 题意 给出两个数组,找出二者最短的公共子序 ...
随机推荐
- 使用CSS的clip-path实现图片剪切效果
最近有个业务需求:校对图片文本信息,如下图所示,当鼠标点击文本中某一行的时候,文本上会显示对应行图片同时左侧会显示对应位置的画框. clip-path 今天要说的主题是:如何剪切原图中的部分图片?(前 ...
- OOP、封装、继承、多态,真的懂了吗?
平时只要一提起来面向对象编程OOP的好处,随口就能说出来,不就是封装.继承.多态么,可他们的含义是什么呢,怎么体现,又有什么非用不可的好处啊.可能平时工作中天天在用OOP,仅仅是在用OOP语言,就是一 ...
- 如何写一个自己的组件库,打成NPM包,并上传到NPM远程
1.首先使用vue create my_project 构建一个自己的Vue项目 2.vue.config.js和package.json配置如下,做了些修改 const path = require ...
- 【C++】《Effective C++》第九章
杂项讨论 条款53:不要轻忽编译器的警告 请记住 严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取"无任何警告"的容易. 不要过度依赖编译器的报警能力, ...
- 【Linux】在文件的指定位置插入数据
今天遇到一个似乎很棘手的问题,要在文件的中间,插入几条配置 这里就以my.cnf这个文件为例 1 [mysqld] 2 datadir=/var/lib/mysql 3 socket=/var/lib ...
- C语言流程图画法(C语言学习笔记)
常用符号及其含义 图片来自百度文库 https://wenku.baidu.com/view/beb410dea216147916112853.html 常用结构 N-S图
- k8s中教你快速写一条yaml文件
一条yaml中有很多字段,如果去背这些字段,其实也能背过,但是去写一条yaml,也往往浪费很多的时间,也会出错,其实我们可以用一条命令就能快速来写一段自定义的yaml,工作中去修改相应的yaml也得心 ...
- 24V转3.3V稳压芯片,高效率同步降压DC-DC变换器3A输出电流
PW2312是一个高频,同步,整流,降压,开关模式转换器与内部功率MOSFET.它提供了一个非常紧凑的解决方案,以实现1.5A的峰值输出电流在广泛的输入电源范围内,具有良好的负载和线路调节. PW23 ...
- 浅谈JavaScript代码性能优化2
一.减少判断层级 从下图代码中可以明显看出,同样的效果判断层级的减少可以优化性能 二.减少作用域链查找层级 简单解释下,下图中第一个运行foo函数,bar函数内打印name,bar作用域内没有name ...
- 配置 containerd 镜像仓库完全攻略
作者简介 王海龙,Rancher中国社区技术经理,负责Rancher中国技术社区的维护和运营.拥有6年的云计算领域经验,经历了OpenStack到Kubernetes的技术变革,无论底层操作系统Lin ...