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 题意 给出两个数组,找出二者最短的公共子序 ...
随机推荐
- NOIP初赛篇——02计算机系统的基本结构
引言 计算机系统由硬件和软件两部分组成,硬件系统是计算机的"躯干",是物质基础.而软件系统则是建立在这个"躯干"上的"灵魂". 计算机硬件 ...
- Syn_Flood攻击&防御手段
Syn_Flood攻击原理 攻击者首先伪造地址对服务器发起SYN请求(我可以建立连接吗?),服务器就会回应一个ACK+SYN(可以+请确认).而真实的IP会认为,我没有发送请求,不作回应.服务器没有收 ...
- #2020征文-开发板# 用鸿蒙开发AI应用(三)软件篇
目录: 前言 HarmonyOS 简介 DevEco Device Tool(windows下) 获取源码(切换到ubuntu) 烧录程序(切换回windows) 前言上一篇,我们在 Win10 上用 ...
- Flutter 应用入门:包管理
pubspec.yaml name: flutter_combat description: A Flutter combat application. # The following defines ...
- SonarQube学习(六)- SonarQube之扫描报告解析
登录http://192.16.1.105:9000,加载项目扫描情况 点击项目名称,查看报告总览 开发人员主要关注为[问题]标签页. 类型 主要关注为bug和漏洞. 其中bug是必须要修复的,漏洞是 ...
- MySQL select join on 连表查询和自连接查询
连表查询 JOIN ON 操作 描述 inner join 只返回匹配的值 right join 会从右表中返回所有的值, 即使左表中没有匹配 left join 会从左表中返回所有的值, 即使右表中 ...
- Can't locate Time/HiRes.pm in @INC (@INC contains
Can't locate Time/HiRes.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/ ...
- 【Linux】centos 7中,开机不执行rc.lcoal中的命令
最近将一些需要开机启动的命令添加到了rc.local中 本想着开机就启动了,很省事 但是一次意外的重启,发现rc.local中的全部命令都没有执行 发现问题后,及时查找 参考:https://blog ...
- mysql—make_set函数
使用格式:MAKE_SET(bits,str1,str2,-) 1 返回一个设定值(含子字符串分隔字符串","字符),在设置位的相应位的字符串.str1对应于位0,str2到第1位 ...
- HTML5与CSS3知识点总结
好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star 原文链接:https://blog.csdn.net/we ...