http://poj.org/problem?id=2079 (题目链接)

题意

  求凸包内最大三角形面积

Solution

  旋转卡壳。

  只会n²的做法,但是竟然过了。就是枚举每一个点,然后旋转卡壳另外两个点。先固定i,j这2个邻接的顶点。然后找出使三角形面积最大的那个k点。然后再固定i,枚举j点,由于k点是随着j点的变化在变化,所以k点不必从开头重新枚举。

  之后去网上看了下O(n)的做法,当时就感觉有点鬼,打了一遍交上去Wa了,鬼使神差拍出一组数据好像可以把网上O(n)的做法全部卡掉,但是我也还搞不清为什么这样做是错的。

数据: 

-7 0 
-5 1 
-1 5 
-2 8 

0 7 
1 5 
5 1 
8 2 
4 8 

0 -7 
4 -8 
8 -2 
5 -1 
1 -5 
-1

  这3个数据都是同一个凸包,面积都是15.00。  

O(n)代码

  1. // poj2079
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cstdlib>
  6. #include<cstdio>
  7. #include<cmath>
  8. #include<map>
  9. #define esp 1e-8
  10. #define inf 2147483640
  11. #define LL long long
  12. #define Pi acos(-1.0)
  13. #define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
  14. using namespace std;
  15. inline LL getint() {
  16. LL x=0,f=1;char ch=getchar();
  17. while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
  18. while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
  19. return x*f;
  20. }
  21.  
  22. const int maxn=50010;
  23. struct point {int x,y;}p[maxn],p0;
  24.  
  25. int cross(point p0,point p1,point p2) {
  26. return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
  27. }
  28. double dis(point a,point b) {
  29. return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  30. }
  31. bool cmp(point a,point b) {
  32. int t=cross(p0,a,b);
  33. if (t>0) return 1;
  34. if (t<0) return 0;
  35. return dis(p0,a)<dis(p0,b);
  36. }
  37. int Graham(int n) {
  38. if (n==1) return 1;
  39. int k=1,top=2;
  40. for (int i=1;i<=n;i++)
  41. if (p[i].y==p[k].y ? p[i].x<p[k].x : p[i].y<p[k].y) k=i;
  42. p0=p[k];p[k]=p[1];p[1]=p0;
  43. sort(p+2,p+1+n,cmp);
  44. for (int i=3;i<=n;i++) {
  45. while (top>1 && cross(p[top-1],p[top],p[i])<=0) top--;
  46. p[++top]=p[i];
  47. }
  48. return top;
  49. }
  50. double RC(int n) {
  51. int ans=0;
  52. p[n+1]=p[1];
  53. int i=1,j=2,k=3,t;
  54. while (k!=1) {
  55. int ii=i,jj=j,kk=k;
  56. while ((t=abs(cross(p[i],p[k],p[j])))<abs(cross(p[i],p[k+1],p[j]))) k=k%n+1;
  57. ans=max(ans,t);
  58. while ((t=abs(cross(p[i],p[k],p[j])))<abs(cross(p[i],p[k],p[j+1]))) j=j%n+1;
  59. ans=max(ans,t);
  60. while ((t=abs(cross(p[i],p[k],p[j])))<abs(cross(p[i+1],p[k],p[j]))) i=i%n+1;
  61. ans=max(ans,t);
  62. if (ii==i && jj==j && kk==k) k=k%n+1;
  63. }
  64. return (double)ans/2.0;
  65. }
  66. int main() {
  67. int n;
  68. while (scanf("%d",&n)!=EOF && n>0) {
  69. for (int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
  70. n=Graham(n);
  71. printf("%.2f\n",RC(n));
  72. }
  73. return 0;
  74. }

O(n²)代码

  1. // poj2079
  2. #include<algorithm>
  3. #include<iostream>
  4. #include<cstring>
  5. #include<cstdlib>
  6. #include<cstdio>
  7. #include<cmath>
  8. #include<map>
  9. #define esp 1e-8
  10. #define inf 2147483640
  11. #define LL long long
  12. #define Pi acos(-1.0)
  13. #define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
  14. using namespace std;
  15. inline LL getint() {
  16. LL x=0,f=1;char ch=getchar();
  17. while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
  18. while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
  19. return x*f;
  20. }
  21.  
  22. const int maxn=50010;
  23. struct point {int x,y;}p[maxn],p0;
  24.  
  25. int cross(point p0,point p1,point p2) {
  26. return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
  27. }
  28. double dis(point a,point b) {
  29. return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  30. }
  31. bool cmp(point a,point b) {
  32. int t=cross(p0,a,b);
  33. if (t>0) return 1;
  34. if (t<0) return 0;
  35. return dis(p0,a)<dis(p0,b);
  36. }
  37. int Graham(int n) {
  38. if (n==1) return 1;
  39. int k=1,top=2;
  40. for (int i=1;i<=n;i++)
  41. if (p[i].y==p[k].y ? p[i].x<p[k].x : p[i].y<p[k].y) k=i;
  42. p0=p[k];p[k]=p[1];p[1]=p0;
  43. sort(p+2,p+1+n,cmp);
  44. for (int i=3;i<=n;i++) {
  45. while (top>1 && cross(p[top-1],p[top],p[i])<=0) top--;
  46. p[++top]=p[i];
  47. }
  48. return top;
  49. }
  50. double RC(int n) {
  51. int ans=0;
  52. p[n+1]=p[1];
  53. for (int i=1;i<=n;i++) {
  54. int j=i%n+1,k=(i+1)%n+1;
  55. while (abs(cross(p[i],p[j],p[k]))<abs(cross(p[i],p[j],p[k+1]))) k=k%n+1;
  56. while (i!=j && i!=k) {
  57. ans=max(ans,abs(cross(p[i],p[j],p[k])));
  58. while (abs(cross(p[i],p[j],p[k]))<abs(cross(p[i],p[j],p[k+1]))) k=k%n+1;
  59. j=j%n+1;
  60. }
  61. }
  62. return (double)ans/2.0;
  63. }
  64. int main() {
  65. int n;
  66. while (scanf("%d",&n)!=EOF && n>0) {
  67. for (int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
  68. n=Graham(n);
  69. printf("%.2f\n",RC(n));
  70. }
  71. return 0;
  72. }

  

【poj2079】 Triangle的更多相关文章

  1. 【LeetCode】Triangle 解决报告

    [称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

  2. 【leetcode】Triangle (#120)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. 【leetcode】triangle(easy)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. 【Leetcode】Triangle

    给定一个由数字组成的三角形,从顶至底找出路径最小和. Given a triangle, find the minimum path sum from top to bottom. Each step ...

  5. 【数组】Triangle

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  6. 【Leetcode】【Medium】Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. 【poj1085】 Triangle War

    http://poj.org/problem?id=1085 (题目链接) 题意 A,B两人玩游戏,在一个大三角形上放火柴,若A放上一根火柴后成功组成一个三角形,那么这个三角形就归属于A,并且A被奖励 ...

  8. 【计数】【UVA11401】 Triangle Counting

    传送门 Description 把1……n这n个数中任取3个数,求能组成一个三角形的方案个数 Input 多组数据,对于每组数据,包括: 一行一个数i,代表前i个数. 输入结束标识为i<3. O ...

  9. 【HDOJ6300】Triangle Partition(极角排序)

    题意:给定3n个点,保证没有三点共线,要求找到一组点的分组方案使得它们组成的三角形之间互不相交. n<=1e3 思路:以y为第一关键字,x为第二关键字,按x递减,y递增排序 #include&l ...

随机推荐

  1. Spring 一二事(1)

    简单介绍一下spring,一方面带新手入入门,一方面自己也重温一下第一个小工厂先暂时不用maven,下一个会用maven来来配置 jar包只需要一个,spring版本为2.5(暂时为2.5,后续更新, ...

  2. uGUI VS NGUI

    前言 这篇日志的比较是根据自己掌握知识所写的,请各路大神多多指教. 引擎版本: Unity 4.6 beta 两者区别 1.uGUI的Canvas 有世界坐标和屏幕坐标 2.uGUI的Button属性 ...

  3. [汇编] 002基础知识-CPU和寄存器

    CPU是什么 当然这里的内存不仅仅指电脑上的内存,例如:我的金士顿8G内存,七彩虹1G独显,在这里来说,显卡也是有内存的(寄存器) CPU如何控制其它部件的? 问题:CPU是如何和电脑主机中其它芯片有 ...

  4. python playfair

    #########################Playfair密码######################### #约定1:若明文字母数量为奇数,在明文末尾添加一个'Z' #约定2:'I'作为 ...

  5. Spring AOP 针对注解的AOP

    我也忘记是从哪里扒来的代码,不过有了这个思路,以后可以自己针对 Controller 还有 Service层的任意 方法进行代理了 package pw.jonwinters.aop; import ...

  6. Linq中查询List组合相同值数量大于1

     List< select g.Key).ToList();

  7. 序列化在Netty中的使用

    Java序列化的缺点 1.无法跨语言 对于Java序列化后的字节数组,别的语言无法进行反序列化 2.序列化后的码流过大 3.序列化性能低 使用JDK自带的序列化进行对象的传输 被传输的,实现了序列化接 ...

  8. 支持MVC的代码生成运行效果 C# ASP.NET

    做技术的,你若还不懂MVC的话,你好像是外星球来的一样,或者还生活在远古社会里一样,这几天正好没什么事情干,可以静心学习学习MVC技术,顺便把原先的代码生成器修改了一下,只要数据库里设计好了数据结构, ...

  9. CHAP认证原理

    整个过程就是PPP协商过程,分三步:LCP.认证.NCP. 一 协议概述 PPP包含以下两个层次的协议: ·链路控制协议(LCP):负责建立.配置和测试数据链路连接 ·网络控制协议(NCP):负责建立 ...

  10. WorldWind源码剖析系列:BMNG类构造函数深入分析

    BMNG构造函数深入分析 一.主要类图 二.主要功能: 1)        BMNG类 BMNG类将包含以“Blue Marble”为主题的所有可渲染影像的根节点添加到当前星球的可渲染对象列表中,包括 ...