首页 技术 正文
技术 2022年11月18日
0 收藏 619 点赞 3,659 浏览 1453 个字

http://acm.nyist.net/JudgeOnline/problem.php?pid=78

圈水池

时间限制:3000 ms  |  内存限制:65535 KB难度:4 

描述
有一个牧场,牧场上有很多个供水装置,现在牧场的主人想要用篱笆把这些供水装置圈起来,以防止不是自己的牲畜来喝水,各个水池都标有各自的坐标,现在要你写一个程序利用最短的篱笆将这些供水装置圈起来!(篱笆足够多,并且长度可变)

 

输入
第一行输入的是N,代表用N组测试数据(1<=N<=10)
第二行输入的是m,代表本组测试数据共有m个供水装置(3<=m<=100)
接下来m行代表的是各个供水装置的横纵坐标
输出
输出各个篱笆经过各个供水装置的坐标点,并且按照x轴坐标值从小到大输出,如果x轴坐标值相同,再安照y轴坐标值从小到大输出
样例输入
1
4
0 0
1 1
2 3
3 0
样例输出
0 0
2 3
3 0

分析:

凸包。

AC代码:

 // NYOJ 78 -- 圈水池 Andrew 凸包算法 O(nlogn)
// 将凸包的顶点按 从小到大 先x后y的 顺序输出
//
/*test data
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
== 1628 */ #include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; const int MAXP = ;
const double Pzero = 0.00001; struct POINT{
int x,y;
}p[MAXP],CHp[MAXP*]; double XJ(POINT a, POINT b, POINT c){
// vector a->b (b.x-a.x, b.y-a.y)
// vector a->c (c.x-a.x, c.y-a.y)
return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
} bool comp(POINT a,POINT b){
return (a.x < b.x) || ((a.x==b.x) && (a.y<b.y));
} int AndrewConvexHull(POINT *CHp, int n){
// 计算凸包 CHp[] 逆时针存点
sort(p, p + n, comp);
int nCHp = ;
for (int i = ; i < n ; CHp[nCHp++] = p[i++])
while(nCHp >= && XJ(CHp[nCHp-], CHp[nCHp-], p[i]) <= )
nCHp--;
for (int i = n - ,m = nCHp + ; i >= ; CHp[nCHp++] = p[i--])
while(nCHp >= m && XJ(CHp[nCHp-], CHp[nCHp-], p[i]) <= )
nCHp--;
return --nCHp;
} void init(int n){
// 输入
for (int i = ; i < n; i++)
scanf("%d%d", &p[i].x, &p[i].y);
} void Output(int nCHp){
sort(CHp, CHp + nCHp, comp);
for (int i = ; i < nCHp ; i++){
printf("%d %d\n", CHp[i].x, CHp[i].y);
}
} int main()
{
// freopen("in.txt","r",stdin);
int n;scanf("%d",&n);
while(~scanf("%d",&n) ){ init(n);
int nCHp = AndrewConvexHull(CHp, n);
Output(nCHp);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900