BAPC2014 C&&HUNNU11583:Citadel Construction(几何)

题意:

给出一系列的点,要求寻找最多4个点,使得组成一个面积最大的多边形

思路:

很显然只有两种情况,要么是三角形,要么是四边形

首先不难想到的是,先要把最外面的点都找出来,其实就是找凸包

但是并没有做过凸包,那么怎么办?

我们知道,ab与ac向量相乘得到ab*ac>=0的情况下,可以知道所有符合这个条件的,都固定在顺时针,那么我们可以通过这个,来求得所有外层的点

得到这些点之后,我们就可以来找出答案了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define ls 2*i
#define rs 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define ULL unsigned long long
#define N 1005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define rank rank1
const int mod = 1000000007;

struct point
{
    int x,y;
} a[N],s[N];
int t,n;

int ads(int a)
{
    return a<0?-a:a;
}

int mult(point a,point b,point c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}

int cmp(point a,point b)
{
    if(a.y==b.y)
        return a.x<b.x;
    return a.y<b.y;
}

int set1(point a[],int n,point s[])
{
    int i,j,k;
    int top = 1;
    sort(a,a+n,cmp);
    if(n==0) return 0;
    s[0] = a[0];
    if(n==1) return 1;
    s[1] = a[1];
    if(n==2) return 2;
    s[2] = a[2];
    for(i = 2; i<n; i++)//先找右边的外层
    {
        while(top&&mult(a[i],s[top],s[top-1])>=0)//新进来的在顺时针方向,可以取代已保存在栈内的
            top--;
        s[++top] = a[i];
    }
    int len = top;
    s[++top] = a[n-2];
    for(i = n-3; i>=0; i--)//找左边的外层
    {
        while(top!=len&&mult(a[i],s[top],s[top-1])>=0)
            top--;
        s[++top] = a[i];
    }
    return top;
}

void solve()
{
    int i,j,k,cnt,area;
    for(int i=0; i<n; i++)
        scanf("%d%d",&a[i].x,&a[i].y);
    cnt = set1(a,n,s);
    if(cnt<3)
    {
        printf("0\n");
    }
    else if(cnt==3)
    {
        area = mult(s[2],s[1],s[0]);
        area = ads(area);
        if(area%2) printf("%d.5\n",area/2);
        else printf("%d\n",area/2);
    }
    else
    {
        area=-INF;
        s[cnt] = s[0];
        for(i = 0; i<cnt; i++)
        {
            int l = i,r = i+2;
            for(j = i+2; j<cnt; j++)//以i,j为四边形对角线,两边循环保证两个三角形不相交算出最大面积
            {
                while(abs(mult(s[l+1],s[j],s[i]))>abs(mult(s[l],s[j],s[i]))) l = (l+1)%cnt;
                int s1 = abs(mult(s[l],s[j],s[i]));
                while(abs(mult(s[r+1],s[j],s[i]))>abs(mult(s[r],s[j],s[i]))) r = (r+1)%cnt;
                int s2 = abs(mult(s[r],s[j],s[i]));
                area = max(area,s1+s2);
            }
        }
        if(area%2) printf("%d.5\n",area/2);
        else printf("%d\n",area/2);
    }
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        solve();
    }

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-05 20:48:32

BAPC2014 C&&HUNNU11583:Citadel Construction(几何)的相关文章

ArcEngine开发各种几何错误代码

E_GEOMETRY_AMBIGUOUSPARTTYPE?- Static variable in interface com.esri.arcgis.geometry.esriGeometryError (0x8004024c) The operation would result in the creation of a new part, but the type of part to be created was ambiguous. E_GEOMETRY_AUTHORITY_TOO_L

公用事业的开放级联模型几何数据

一.概述 Overview Open CASCADE中的几何工具(Geometry Utilities)提供如下功能: l 通过插值和逼近创建图形 Creation of shapes by interpolation and approximation l 直接构造图形 Direct construction of shapes l 将曲线曲面转换成B样条曲线和曲面 Conversion of curves and surfaces to bspline curves and surfaces

URAL - 1963(几何)

不知道是不是几何题,反正就是找对称,值得注意的是,对称轴不一定过点,还可能在边上 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; const double mm=1e-7; double x[10],y[10]; bool deng(double a,double

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

如何让你的UWP应用程序无缝调用几何作图

有时候需要编辑一些几何图形,如三角形,圆锥曲线等,在UWP应用中加入这些几何作图功能是件费时间又很难做好的事.其实Windows 10 应用商店中已有一些专业的几何作图工具了,那么能借来一用吗?答案是肯定的. UWP中,微软为Windows.System.Launcher启动器新增了很多的功能,以前只能启动App,打开指定扩展名文件,对uri协议的解析,以及当启动的应用没有安装时则会提示前往商店下载等. 如今,微软丰富了Launcher的功能,通过新增的LaunchUriForResultsAs

抛物线的几何性质(传统几何法推导)

抛物线有很多几何性质,网上也有不少关于这些性质的推导的文章,不过几乎清一色地都是用的解析几何的方法.联立方程,导出根与系数的关系,算算算算算…… 但是,与同样是二次曲线的椭圆和双曲线不同,圆和抛物线的几何性质非常「好」,不用坐标法,也能推出很多结论.不过相比具有完美对称性的圆来说,抛物线还是逊色了许多.圆的切线很容易用几何条件去描述(容易用反证法证出圆的切线垂直于过切点的直径),而抛物线的切线虽然也容易用几何条件描述,但相关结论却难以用纯几何法证出.所以涉及切线问题时,还是需要用坐标法证明一个重

大图标+不规则几何创造不同风格

设计了一套医用图标,运用超大线性图标与不规则几何背景相结合的方式,可以发现也是一种可以尝试的方式.用超大线性图标作为内容的和信息的标识,生动易理解(这里融合卡通风味),与此同时用色彩柔和的不规则几何图形作为背景,加以衬托,提升视觉感,值得尝试,向大家推荐.          我这边设计了两套,一套是彩色的,一套的蓝色的,巧妙运用色彩的搭配.透明度的搭配,可以创作出不同效果. 将图标运用到界面上,主页面直接用大图标: 404页面小动画:

The Doors 最短路 + 简单几何

The Doors 题目抽象:给你一些点,和一些阻碍连边的线段.问原点到终点最短的几何距离. 分析:预处理见图之后,跑最短路. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 const int INF = 0X5FFFFFFF; 7 const int MS = 1005; 8 const i

【几何模板加点小思路】hdu-4998 Rotate

用几何模板敲的,也有直接公式推的,追求短代码的可以点右上角小红了...... 题意就是想想一个物体分别做绕某一点(给出坐标)旋转p度(给出角度)后,其位置等价于绕哪一点旋转多少度,输出该等价点及其等价角度. 其实就是找两个定点,然后看这两个定点旋转后到了哪,分别连接原点与旋转后的点会得到两条线段,两条线段垂直平分线的交点即是等价后绕其旋转的点,再将该交点与任一原点及其旋转后的点连接得到的夹角(咖啡色角)即等价后的旋转角度. 附代码 1 #include <cstdio> 2 #include