[洛谷P2742]【模板】二维凸包([USACO5.1]圈奶牛Fencing the Cows)
题目大意:求一个点集凸包边长
题解:求凸包,直接求
卡点:发现在较后面数位上有较小的误差,还以为是浮点数误差,最后发现是构造函数写成了$int$类型
C++ Code:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#define maxn 10010 inline double sqr(double x) { return x * x; }
struct Point {
double x, y;
Point() { }
Point(double __x, double __y) : x(__x), y(__y) { }
inline Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
inline Point operator + (const Point &rhs) const {
return Point(x + rhs.x, y + rhs.y);
}
inline double operator * (const Point &rhs) const {
return x * rhs.y - y * rhs.x;
}
} O, s[maxn], v[maxn];
inline double abs2(const Point &x) { return sqr(x.x) + sqr(x.y); }
inline double dis(const Point &lhs, const Point &rhs) { return sqrt(abs2(rhs - lhs)); }
inline double det(const Point &O, const Point &lhs, const Point &rhs) {
return (lhs - O) * (rhs - O);
}
inline bool operator < (const Point &lhs, const Point &rhs) {
static Point X, Y; X = lhs - O, Y = rhs - O;
static double t; t = X * Y;
return (t > 0) || (t == 0 && abs2(X) > abs2(Y));
} int n, tot;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
std::cin >> n;
if (n <= 1) {
std::cout << "0\n";
return 0;
}
int miny = 0;
for (int i = 0; i < n; ++i) {
std::cin >> s[i].x >> s[i].y;
if ((s[i].y < s[miny].y) || (s[i].y == s[miny].y && s[i].x < s[miny].x)) miny = i;
}
if (n == 2) {
std::cout << std::fixed << std::setprecision(2) << dis(s[0], s[1]) * 2 << '\n';
return 0;
}
O = s[miny]; std::swap(s[0], s[miny]);
std::sort(s + 1, s + n);
v[0] = s[0], v[1] = s[1], v[2] = s[2], tot = 3;
for (int i = 3; i < n; ++i) {
for (Point a = v[tot - 2], b = v[tot - 1]; tot > 2 && det(a, b, s[i]) <= 0; a = v[tot - 2], b = v[tot - 1]) --tot;
v[tot++] = s[i];
}
double ans = 0;
for (int i = 0, nxt = 0; i < tot; ++i) {
nxt += 1 - tot;
nxt += nxt >> 31 & tot;
ans += dis(v[i], v[nxt]);
}
std::cout << std::fixed << std::setprecision(2) << ans << '\n';
return 0;
}
[洛谷P2742]【模板】二维凸包([USACO5.1]圈奶牛Fencing the Cows)的更多相关文章
- luogu P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
题解: 二维凸包裸题 按照x坐标为第一关键字,y坐标为第二关键字排序 然后相邻判断叉积用单调队列搞过去 正反都做一次就好了 代码: #include <bits/stdc++.h> usi ...
- P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
题意:n个点,求凸包周长.(纯板子QAQ) 定义 凸包:用最小的凸多边形将n个点围在里面的图形为凸包 前置 向量:点积:(a,b) (c,d)=(a*c,b*d) =|(a,b)|*|(c,d)|*c ...
- LG2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
题意 题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 ...
- 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
Problem surface 戳我 Meaning 坐标系内有若干个点,问把这些点都圈起来的最小凸包周长. 这道题就是一道凸包的模板题啊,只要求出凸包后在计算就好了,给出几个注意点 记得检查是否有吧 ...
- luoguP2742 【模板】二维凸包 / [USACO5.1]圈奶牛 二维凸包
我们知道,纵坐标最小的点一定在凸包上(如果有多个,那它们都会被取到) 随便找一个纵坐标最小的点,将其他所有点按照这个点为原点极角排序,我们发现极角大的会在极角小的后面加入(感性认知一下) 考虑新(加入 ...
- Luogu P2742 模板-二维凸包
Luogu P2742 模板-二维凸包 之前写的实在是太蠢了.于是重新写了一个. 用 \(Graham\) 算法求凸包. 注意两个向量 \(a\times b>0\) 的意义是 \(b\) 在 ...
- 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows
题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 输入 ...
- 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows || 凸包模板
整篇都是仅做记录... 蓝书上的板子.水平序,单调栈.先求下凸包,再求上凸包.叉积的作用是判定向量的位置关系. 48行的作用是在求上凸包的时候不至于去删下凸包中的点.上凸包中第一个点被认为是t1. 另 ...
- P2742 [USACO5.1]圈奶牛Fencing the Cows
题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 输入 ...
随机推荐
- android 学习十四 探索安全性和权限
1.部署安全性:应用程序必须使用数字证书才能安装到设备上. 2.执行期间的安全性: 2.1 使用独立进程 2.2 使用固定唯一用户ID 2.3 申明性权限模型 3数字证书 ...
- 「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)
题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆:你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路.然后问你最多去几家饭店. 这题我写的 ...
- selenium,unittest——两个class连续运行
将多个class放在一个文件内一起运行,这是一个多用例不同网站进行测试的方法 #encoding=utf-8from selenium import webdriverimport time,unit ...
- 【带 josn参数的测法】
遇到json 参数的情况这样写 ,否则就会报错 cod 415 nocookie post请求 ,","email":"beihe@163.com&quo ...
- Android开发-API指南-<receiver>
<receiver> 英文原文:http://developer.android.com/guide/topics/manifest/receiver-element.html 采集(更新 ...
- TensorFlow入门之MNIST最佳实践-深度学习
在上一篇<TensorFlow入门之MNIST样例代码分析>中,我们讲解了如果来用一个三层全连接网络实现手写数字识别.但是在实际运用中我们需要更有效率,更加灵活的代码.在TensorFlo ...
- Js全反选DataGrid
// **************************************************************** // // function Trim(value) // -- ...
- NMAP-高级用法
1.报文分段 2.偏移 –mtu后面的数字是8的倍数 3.源端口欺骗 4.指定报文长度 5.ttl 6.mac地址伪造 0代表随机伪造 7.正常输出 8.输出为xml 9.输出为grep 10.输出所 ...
- nodejs笔记--express篇(五)
创建一个express + ejs的项目 express -e testEjsWebApp cd testEjsWebApp npm install http://localhost:3000 Usa ...
- 实用的ES6特性
1. 函数参数默认值 不使用ES6 为函数的参数设置默认值: function foo(height, color) { var height = height || 50; var color = ...