首页 技术 正文
技术 2022年11月21日
0 收藏 378 点赞 4,630 浏览 2614 个字

Fencing the Cows
Hal Burch

Farmer John wishes to build a fence to contain his cows, but he’s a bit short on cash right. Any fence he builds must contain all of the favorite grazing spots for his cows. Given the location of these spots, determine the length of the shortest fence which encloses them.

PROGRAM NAME: fc

INPUT FORMAT

The first line of the input file contains one integer, N. N (0 <= N <= 10,000) is the number of grazing spots that Farmer john wishes to enclose. The next N line consists of two real numbers, Xi and Yi, corresponding to the location of the grazing spots in the plane (-1,000,000 <= Xi,Yi <= 1,000,000). The numbers will be in decimal format.

SAMPLE INPUT (file fc.in)

4
4 8
4 12
5 9.3
7 8

OUTPUT FORMAT

The output should consists of one real number, the length of fence required. The output should be accurate to two decimal places.

SAMPLE OUTPUT (file fc.out)

12.00

————————————————————————————————题解

一道凸包求周长的题,卷包裹算法,如果栈中的后两个点组成的向量和新加入的点和最后点的向量是顺时针旋转,那么就不符合凸包的性质,弹出直到符合性质即可

 /*
ID: ivorysi
LANG: C++
TASK: fc
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
struct vec {
double x,y;
vec(double x=0.0,double y=0.0) :x(x),y(y) {}
vec operator - (const vec &b) const{
return vec(x-b.x,y-b.y);
}
double norm() const{
return x*x+y*y;
}
}a[];
int n;
bool dcmp(double a,double b=0.0) {
return fabs(a-b) < esp ? : ;
}
double cross(vec a,vec b) {
return a.x*b.y-a.y*b.x;
}
inline bool cmp(const vec &c,const vec &d) {
vec t1= c-a[],t2=d-a[];
double t=cross(t1,t2);
if(!dcmp(t)) return t>;//逆时针为正,顺时针为负
else return c.norm()<d.norm();
}
double o(double a){
return a*a;
}
struct poly {
vector<vec> poi;
double peri() {
double res=0.0;
siji(i,,poi.size()-) {
res+=sqrt(o(poi[i].x-poi[i-].x)+o(poi[i].y-poi[i-].y));
}
res+=sqrt(o(poi[].x-poi[poi.size()-].x)+o(poi[].y-poi[poi.size()-].y));
return res;
}
void convex() {
int id=;
siji(i,,n) {
if(a[i].x<a[id].x || (a[i].x==a[id].x && a[i].y<a[id].y)) {
id=i;
}
}
if(id!=) swap(a[id],a[]);
sort(a+,a++n,&cmp);
poi.push_back(a[]);
siji(i,,n) {
while(poi.size() >= &&
cross(poi[poi.size()-]-poi[poi.size()-],a[i]-poi[poi.size()-])<=)
poi.pop_back();
poi.push_back(a[i]);
}
}
}tw; void solve() {
scanf("%d",&n);
siji(i,,n) {
scanf("%lf%lf",&a[i].x,&a[i].y);
}
tw.convex();
printf("%.2lf\n",tw.peri());
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("fc.in","r",stdin);
freopen("fc.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,074
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892