题目链接 https://codeforces.com/contest/1666
C Connect the Points
题意
给出三个点,找任意条平行x轴或y轴线段,使得线经过3个点,且线段长度和最小。
思路
手玩可以发现,线段和最长可以不超过3点x坐标最大差值加上3点y坐标最大差值,所以我们可以找到y坐标第二大的点,过它作一条从x最小值到x最大值的平行x轴线段,然后从另外两个点作到这个线段的垂线即可。
Accepted code
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y;
} a[3];
bool cmp(node a, node b) {
return a.y < b.y; }
int main() {
int lx = 1000000000 + 7, rx = -1;
for (int i = 0; i < 3; i++) {
cin >> a[i].x >> a[i].y;
lx = min(lx, a[i].x);
rx = max(rx, a[i].x);
}
sort(a, a + 3, cmp);
int num = 0;
for (int i = 0, b = -1; i < 3; i++) {
if (a[i].y != b) ++num;
b = a[i].y;
}
cout << num << '\n';
cout << lx << ' ' << a[1].y << ' ' << rx << ' ' << a[1].y << '\n';
if (a[0].y != a[1].y)
cout << a[

本篇博客主要解析2021-2022 ICPC及NERC Northern Eurasia比赛中的三道题目,包括C题'Connect the Points'、D题'Deletive Editing'和L题'Labyrinth'。C题通过找到y坐标第二大的点作平行x轴线段,实现线段长度和最小。D题利用从右往左匹配策略判断s串是否能变成t串。L题则通过有向图的深度优先搜索寻找两条不同路径到达终点的方法。
最低0.47元/天 解锁文章
801

被折叠的 条评论
为什么被折叠?



