武大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 ...
随机推荐
- HDU 1879(最小生成树)
#include "iostream" #include "algorithm" #include "cstdio" using names ...
- Redis基础---5个基本数据结构(比较性记忆)
“ Redis是一个内存数据库,只用硬盘来进行持久化. Mongodb是半内存数据库 Mysql是硬盘数据库 ” 1. Redis启动 安装好了之后.运行redis-3.2.8/src/下的redis ...
- JS获取到时间转换成字符串类型
JS获取到时间格式为:/Date(1479279579047)/ 提供一个函数可将其转换为“yyyy-MM-dd”和“yyyy-MM-dd hh:mm:ss” //时间日期转换成string func ...
- document.write清除原有内容情况
原博客: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...
- JSON字符串的生成
public class Corporation { public string remark { get; set; } public string version { get; set; } pu ...
- 01JavaScript使用
JavaScript使用 1.内容写入 HTML <P onmouseover="alert('欢迎您学习JavaScript!')">鼠标移过来</P> ...
- 利用freemarker+SAX解析xml的方式对excel文件字段校验
利用freemarker对参数进行校验这篇文章主要用到的技术点: 自定义注解的使用反射机制SAX解析xmlFreemarker的运用我们在工作中经常需要上传excel文件,然后在对文件中的字段进行校验 ...
- wpf 自定义单选按钮 RadioButton
新建RadioButtonEx.cs public class RadioButtonEx : RadioButton { public Geometry SelectIcon { get { ret ...
- HDU - 6264 - Super-palindrome(思维)
题意: 给出一个字符串,使得所有的奇数连续子串为回文串,输出最小变化次数 思路: 分析过后,只存在两种情况,1全部为一种字母,2形如abab交替类型 对于奇数位和偶数位单独计数,只需计算出奇数位或者偶 ...
- C++ 迭代器运算符 箭头运算符->
所有标准库容器都支持迭代器,只有少数几种才支持下标运算 迭代器运算符 运算符 作用 *iter 返回迭代器iter所指元素的引用 iter -> mem 解引用iter,并获取元素名为mem的成 ...