BZOJ 1069 Luogu P4166 最大土地面积 (凸包)

news/2025/3/14 16:39:59/

题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=1069

(luogu)https://www.luogu.org/problemnew/show/P4166

题解: 水题,凸包极角排序之后枚举凸四边形对角线\(i,j\)然后找面积最大的点\(k\)\(k\)随着\(i,j\)是单调的

但是有个易错点,就是双指针那个\(k\)前移的条件必须是前移后大于等于原来,如果写成大于就只有50(详见代码)

查了半天发现原因居然是: 数据里有重点! 一旦有重点就会出现\(k\)一直在重点处进不动的情况。呜呜呜好坑啊

代码

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<algorithm>
#include<cmath>
using namespace std;const double EPS = 1e-8;
int dcmp(double x) {return x<-EPS ? -1 : (x>EPS ? 1 : 0);}
struct Point
{double x,y;Point() {}Point(double _x,double _y) {x = _x,y = _y;}
};
bool cmpy(Point x,Point y) {return dcmp(x.y-y.y)<0 || (dcmp(x.y-y.y)==0 && dcmp(x.x-y.x)<0);}
bool cmpx(Point x,Point y) {return dcmp(x.x-y.x)<0 || (dcmp(x.x-y.x)==0 && dcmp(x.y-y.y)<0);}
typedef Point Vector;
Point operator +(Point x,Point y) {return Point(x.x+y.x,x.y+y.y);}
Point operator -(Point x,Point y) {return Point(x.x-y.x,x.y-y.y);}
Point operator *(Point x,double y) {return Point(x.x*y,x.y*y);}
Point operator /(Point x,double y) {return Point(x.x/y,x.y/y);}
double Dot(Vector x,Vector y) {return x.x*y.x+x.y*y.y;}
double Cross(Vector x,Vector y) {return x.x*y.y-x.y*y.x;}
double EuclidDist(Point x,Point y) {return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));}
Vector rotate(Vector x,double ang) {return Vector(x.x*cos(ang)-x.y*sin(ang),x.x*sin(ang)+x.y*cos(ang));}
const int N = 2000;
Point a[N+3];
Point ch[(N<<1)+3];
int stk[N+3];
double area[N+3][N+3];
int n,tp;bool cmp(Point x,Point y) {return dcmp(Cross(x-a[1],y-a[1]))>0;}void Convex_Hull()
{for(int i=2; i<=n; i++){if(cmpy(a[i],a[1])==true) {swap(a[i],a[1]);}}sort(a+2,a+n+1,cmp);stk[1] = 1; stk[2] = 2; tp = 2;for(int i=3; i<=n; i++){while(dcmp(Cross(a[i]-a[stk[tp-1]],a[stk[tp]]-a[stk[tp-1]]))>0) {tp--;}tp++; stk[tp] = i;}for(int i=1; i<=tp; i++) ch[i] = a[stk[i]];for(int i=1; i<=tp; i++) ch[i+tp] = ch[i];
//  printf("chsize=%d\n",tp);
//  for(int i=1; i<=tp; i++) printf("(%lf %lf)\n",ch[i].x,ch[i].y);
}int main()
{scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%lf%lf",&a[i].x,&a[i].y);Convex_Hull();if(tp==3){double ans = fabs(Cross(ch[2]-ch[1],ch[3]-ch[1]))/2.0,ans2 = 1e11;for(int i=1; i<=n; i++){if(i==stk[1]||i==stk[2]||i==stk[3]) continue;double cur = min(min(fabs(Cross(a[i]-ch[1],ch[2]-ch[1])),fabs(Cross(a[i]-ch[2],ch[3]-ch[2]))),fabs(Cross(a[i]-ch[3],ch[1]-ch[3])))/2.0;ans2 = min(ans2,cur);}printf("%lf\n",ans-ans2);return 0;}double ans = 0.0;for(int i=1; i<=tp; i++){int k = i+1;for(int j=i+2; j<=i+tp-2; j++){while(k+1<j && dcmp(Cross(ch[k+1]-ch[i],ch[j]-ch[i])-Cross(ch[k]-ch[i],ch[j]-ch[i]))>=0) {k++;}area[i][(j-1)%tp+1] = Cross(ch[k]-ch[i],ch[j]-ch[i])/2.0;
//          printf("i%d j%d k%d %lf\n",i,j,k,area[j]);}}for(int i=1; i<=tp; i++){for(int j=i+2; j<=tp; j++){double tmp = area[i][j]+area[j][i];
//          printf("area[%d][%d]=%lf\n",i,j,area[i][j]);
//          printf("area[%d][%d]=%lf\n",j,i,area[j][i]);ans = max(ans,tmp);}}printf("%.3lf\n",ans);return 0;
}

转载于:https://www.cnblogs.com/suncongbo/p/11113912.html


http://www.ppmy.cn/news/243412.html

相关文章

Android中的Intent(显示隐式)

Android中的Intent(显示&隐式) 显示Intent 显示Intent是明确目标Activity的类名 通过Intent(Context packageContext, Class<?> cls)构造方法Intent intent new Intent(this, SecondActivity.class) startActivity(intent);通过Intent的setComponent()方法Componen…

NLP的idea,看了就能水一篇论文

1.问题 在中文情感分析任务中,已有方法仅从单极、单尺度来考虑情感特征&#xff0c;无法充分挖掘和利用情感特征信息&#xff0c;模型性能不理想。 单级单尺度&#xff1a;只从一个方面学习文本的特征 多级多尺度&#xff1a;应该是分别从不同方面学习文本的特征&#xff0c…

一键提升分辨率,呈现更清晰、更细腻的视觉盛宴

牛学长视频修复工具是一款使用AI技术对你的视频分辨率就行调整放大清晰的软件&#xff0c;最高支持8K超高清。 现如今&#xff0c;视频成为人们记录生活、表达创意的重要方式之一。然而&#xff0c;我们常常遇到一个问题&#xff1a;旧有的视频素材分辨率低&#xff0c;画质模…

priority_queue的模拟实现和仿函数

priority_queue模拟 首先查看源代码&#xff0c;源代码就在queue剩下的部分中 push_heap是STL库中的堆算法&#xff0c;STL库中包装有支持堆的算法&#xff0c;在algorithm.h中&#xff1a; 只要不断用堆的形式插入数据&#xff0c;就会形成堆。 priority_queue模拟——初版 pr…

网络协议——STP协议是什么?是如何实现的?

作者&#xff1a;Insist-- 个人主页&#xff1a;insist--个人主页 作者会持续更新网络知识和python基础知识&#xff0c;期待你的关注 目录 一、STP协议是什么 二、为什么需要STP协议 三、STP的实现过程 ​编辑 1、选举跟桥 2、给非跟桥交换机选举跟端口 3、给每个网段选…

正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)失败:未知的名称或服务。wget无法解析主机地址

网络问题&#xff1a;做个记录 我这边是更改了链接方式没有问题了就因为之前安装时使用的是nat方式 后来未关机的情况下改成了桥连接出现的问题。ping不通www.baidu.com

CP2102和CP2104的区别

主要区别&#xff1a; 1. CP2104更便宜。 2. CP2104体积更小&#xff0c;占地面积不同。CP2104是QFN24&#xff08;4x4mm&#xff09;; CP2102是QFN28&#xff08;5x5mm&#xff09;。 3. CP2104具有 I/O 电源引脚&#xff0c;可通过外部电阻承受VDD至5V的电压。 4. CP2104支持…

413_J1

<template><div><div class"selectDiv"><div style"margin: 0 10px">违规数量类型:</div><el-select v-model"value1" placeholder"请选择" size"mini" change"selectChange"&g…