武大OJ 706.Farm
Farmer John has a farm. Betsy, a famous cow, loves running in farmer John's land. The noise she made makes John mad. So he wants to restrict the area Betsy can run. There are nn (4<n\le 20000004<n≤2000000) points of interest (or POI) on the farm, the i-th one located at (x_i,y_i)(xi,yi), 0\le |x_i |,|y_i |\le 10^60≤∣xi∣,∣yi∣≤106. So farmer John said: You can select four points, and the area you can run is the convex hull of the four points you select. Betsy is clever, but she doesn't like computing, so she asked you for help to calculate the max area she can run.
Important note
There are at most 20 test cases in your input. 18 of them satisify n\le 2000n≤2000. And the ramaining two testcases are generated in random. The max size of input file is about 60MB. Please, use fast input methods (for example, please use BufferedReader
instead of Scanner
for Java, and scanf
instead of cin
for C++).
Input
Input contains multiple cases, please process to the end of input. The first line of each test cases contains an integer nn, the number of POI. The following nn lines, contains two integers, x_i,y_ixi,yi, the location of ii-th POI.
Output
For each test case, print one line with your answer, your answer should keep exactly one decimal place.
Sample Input
4
0 0
1 0
1 1
0 1
4
1 1
2 2
3 3
4 4
Sample Output
1.0
0.0
原题是BZOJ1069
首先求个凸包,然后在凸包上面枚举两点,找到它两边能组成的最大三角形即可,这个三角形可用2-
pointer的方式,就是找凸包上关于某一条线最远的点,复杂度是n^2
n的范围虽然很大,但是n个点都是随机生成的,求凸包后会丢弃大量的点
求凸包用的是基于水平序的Graham_Scan算法
需要注意的是求叉积和面积的时候可能会超出int类型,需要转double
#include<algorithm>
#include<iostream>
#include<fstream>
#include<math.h>
#include<stdio.h>
using namespace std; int n;
typedef struct{
int x,y;
}Point; Point a[];
Point b[];
int q[],top;
int ans[]; bool cmp(Point a,Point b){
return a.y<b.y||(a.y==b.y&&a.x<b.x);
} //int转double的技巧
double cross(int i,int j,int k){
Point a1=a[i];
Point b=a[j];
Point c=a[k];
return 1.0*(c.x-b.x)*(a1.y-b.y)-1.0*(c.y-b.y)*(a1.x-b.x);
} void scan(){
q[]=;q[]=;top=;
for(int i=;i<=n;++i)
{
while(top>&&cross(q[top-],q[top],i)<=) top--;
q[++top]=i;
} for(int i=;i<=top;++i)
ans[i]=q[i];
ans[]=top; q[]=n;q[]=n-;top=;
for(int i=n-;i>=;--i)
{
while(top>&&cross(q[top-],q[top],i)<=) top--;
q[++top]=i;
} for(int i=;i<top;++i)
{
ans[]++;
ans[ans[]]=q[i];
}
} //int转double的技巧
double area(int i,int j,int k){
Point a1=a[ans[i]];
Point b=a[ans[j]];
Point c=a[ans[k]];
return fabs((1.0*(c.x-b.x)*(a1.y-b.y)-1.0*(c.y-b.y)*(a1.x-b.x))/); } double max_area=;
double left_area=;
double right_area=;
int main(){
while(~scanf("%d",&n)){
max_area=;
for(int i=;i<=n;++i)
scanf("%d %d",&b[i].x,&b[i].y); sort(b+,b+n+,cmp); //去除重复点
int len=;
a[]=b[];
int i=;
while(i<=n)
{
while(i<=n&&b[i].x==b[i-].x&&b[i].y==b[i-].y) i++;
if(i<=n)
a[++len]=b[i];
i++;
}
n=len; //水平序扫描构造凸包
scan(); if(ans[]<=) {printf("0.0\n");continue;}
if(ans[]==) {printf("%.1f\n",area(,,));continue;} int r,l,d;
for(int i=;i<=ans[];++i)
{
r=i+;
l=i+;
if(i+>ans[]) continue;//第4个点不应该超过最后一个凸包节点,不然就重复计算了
for(int j=i+;j<=ans[];++j)
{
if(i==&&j==ans[]) continue; while(r+<j&&area(i,j,r)<=area(i,j,+r)) r++;
right_area=area(i,j,r); while(l+<=ans[]&&area(i,j,l)<=area(i,j,l+)) l++;
left_area=area(i,j,l); if(right_area+left_area>max_area) max_area=right_area+left_area;
}
}
printf("%.1f\n",max_area); } }
武大OJ 706.Farm的更多相关文章
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- 武大OJ 574. K-th smallest
Description Give you a number S of length n,you can choose a position and remove the number on it.Af ...
- 武大OJ 622. Symmetrical
Description Cyy likes something symmetrical, and Han Move likes something circular. Han Mov ...
- 武大OJ 613. Count in Sama’s triangle
Description Today, the math teacher taught Alice Hui Yang’s triangle. However, the teacher came up w ...
- 武大OJ 612. Catch the sheep
Description Old Sama is a great and powerful magician in the word. One day, a little girl, Anny, tou ...
- BZOJ 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场
题目 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 491 S ...
- Online Judge(OJ)搭建(第一版)
搭建 OJ 需要的知识(重要性排序): Java SE(Basic Knowledge, String, FileWriter, JavaCompiler, URLClassLoader, Secur ...
- SharePoint 2013: A feature with ID has already been installed in this farm
使用Visual Studio 2013创建一个可视web 部件,当右击项目选择"部署"时报错: "Error occurred in deployment step ' ...
- 1Z0-053 争议题目解析706
1Z0-053 争议题目解析706 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 706.You execute the following command to set the ...
随机推荐
- STL---vector的内存分配策略
2级策略,过程如下: 第一级 __malloc_alloc_template内存分配器 该分配器是对malloc.realloc以及free的封装: 第二级 __default_alloc_temp ...
- js实现点击上下按钮,图片向上向下循环滚动切换
//popup.js //jquery.1.4.2-min.js (function(p,j){function u(){if(!c.isReady){try{v.documentElement.do ...
- CF814B An express train to reveries
思路: 模拟,枚举. 实现: #include <iostream> using namespace std; ; int a[N], b[N], cnt[N], n, x, y; int ...
- WordPress百度熊掌号页面改造(纯代码实现)
一.粉丝关注改造 1.添加熊掌号ID声明 ID声明 <script src="//msite.baidu.com/sdk/c.js?appid=你的熊掌ID">< ...
- URI URL URN的区别
一:什么是URI,URL,URN ? URI:Uniform Resource Identifier,统一资源标识符,是一个用于表示互联网上资源名称的字符串 格式:http://www.xxx.com ...
- canvas一周一练 -- canvas绘制中国银行标志(4)
运行效果: <!DOCTYPE html> <html> <head> </head> <body> <canvas id=" ...
- (转)Hibernate框架基础——Java对象持久化概述
http://blog.csdn.net/yerenyuan_pku/article/details/52732990 Java对象持久化概述 应用程序的分层体系结构 基于B/S的典型三层架构 说明 ...
- MySQLWorkBench怎么设置主键自增长
参考 https://blog.csdn.net/qq_40472613/article/details/87858099 勾选AI选项,相当于执行了这个语句: AUTO_INCREMENT表示自增 ...
- SpringMVC接收多参数的处理方法
问题:依赖SpringMVC自带的机制解析多对象参数往往出现解析不了的问题,使用较为复杂. 解决思路:前端 JS 先把传递到后台的对象转换为 JSON 字符串,后台直接使用字符串类型接收,再使用 st ...
- 实现加载页Loading Page 的几种方法
网页也可以像原生应用那样加入进度条或者其他的loading效果带来更好的等待体验,这里归纳几种我收集的实现loading page的方法,这几种方法在交互上都有利有弊,适用于不同应用.(PS:以下方法 ...