A - Area 51 Gym - 101334A 计算几何 极坐标 poj 1696 极坐标排序

2024-02-13 16:58

本文主要是介绍A - Area 51 Gym - 101334A 计算几何 极坐标 poj 1696 极坐标排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题意


给定n个位于第一和第二象限的点,每一个点都有一个符号表示

给出一个序列,问从x轴的哪些区间从做往右看刚好符合这个序列


题解:

输入后进行极坐标排序,以负无穷为源点,按角的大小降序排列,若有相同角的就按照近的在前

再计算区间的分割点

然后枚举区间,看是否符合题意


这里不需要在枚举区间的时候都进行极坐标排序,只需要经过一个区间分割点的时候将两个字母换一个位置即可



#include<vector>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;vector<pair<double,double> >ans;#define MAXN 110
#define inf 0x3f3f3f3f
const double eps=1e-8;
char str[MAXN];
int now[MAXN*MAXN];
struct Point
{char ch[5];double x,y;Point(){}Point(double _X, double _Y){x = _X; y = _Y;}
};
Point P[MAXN];
struct Line
{double x;int t1,t2;Line(){}Line(double _x,int _t1,int _t2){x=_x,t1=_t1,t2=_t2;}
};
Line line[MAXN*MAXN];double Cross(Point p1,Point p2,Point p3){return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
double Dis(Point A, Point B){return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}
Point operator - (Point A,Point B){return Point(A.x-B.x, A.y-B.y);
}
Point operator + (Point A, Point B){return Point(A.x+B.x, A.y+B.y);
}
Point operator * (Point A, double p){return Point(A.x*p, A.y*p);
}
bool operator == (Point A, Point B){return (A.x-B.x) == 0 && (A.y-B.y) == 0;
}
int sgn(double x)
{if(fabs(x)<eps)return 0;if(x<0)return -1;return 1;
}bool cmp(Point a, Point b)///按极角降序排序,若角度相等距离小的在前面
{return a.y!=b.y?a.y>b.y:a.x<b.x;
}bool cmpt(Line a,Line b)
{if(fabs(a.x-b.x)>eps) return a.x<b.x;if(a.t1!=b.t1)        return a.t1<b.t1;return a.t2<b.t2;
}double deal(int i,int j)
{double x1=P[i].x,y1=P[i].y;double x2=P[j].x,y2=P[j].y;if(x1==x2)return x1;return x1-y1/(y2-y1)*(x2-x1);
}bool check(int n)
{for(int i=0;i<n;i++)if(str[now[i]]!=P[i].ch[0])return 0;return 1;
}int main()
{int n;//freopen("in.txt","r",stdin);freopen("area.in","r",stdin);freopen("area.out","w",stdout);while(scanf("%d",&n)!=EOF){scanf("%s",str);for(int i=0;i<n;i++)scanf("%s%lf%lf",P[i].ch,&P[i].x,&P[i].y);sort(P,P+n,cmp);int cnt=0;for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){if(P[i].y!=P[j].y){line[cnt].x=deal(i,j);line[cnt].t1=i;line[cnt++].t2=j;}}}line[cnt++]=Line(inf,0,0);sort(line,line+cnt,cmpt);for(int i=0;i<n;i++)now[i]=i;ans.clear();double last=-inf;for(int i=0;i<cnt;i++){if(last<line[i].x-eps&&check(n))ans.push_back(make_pair(last,line[i].x));last=line[i].x;swap(now[line[i].t1],now[line[i].t2]);}printf("%d\n",ans.size());for(int i=0;i<ans.size();i++){double x=ans[i].first,y=ans[i].second;x==-inf?printf("* "):printf("%0.8lf ",x);y==inf?printf("*"):printf("%0.8lf",y);printf("%c",i==ans.size()-1?'\n':' ');}}return 0;
}

Space Ant
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4511 Accepted: 2841

Description

The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y1999 and called it M11. It has only one eye on the left side of its head and just three feet all on the right side of its body and suffers from three walking limitations:
  1. It can not turn right due to its special body structure.
  2. It leaves a red path while walking.
  3. It hates to pass over a previously red colored path, and never does that.

The pictures transmitted by the Discovery space ship depicts that plants in the Y1999 grow in special points on the planet. Analysis of several thousands of the pictures have resulted in discovering a magic coordinate system governing the grow points of the plants. In this coordinate system with x and y axes, no two plants share the same x or y.
An M11 needs to eat exactly one plant in each day to stay alive. When it eats one plant, it remains there for the rest of the day with no move. Next day, it looks for another plant to go there and eat it. If it can not reach any other plant it dies by the end of the day. Notice that it can reach a plant in any distance.
The problem is to find a path for an M11 to let it live longest.
Input is a set of (x, y) coordinates of plants. Suppose A with the coordinates (xA, yA) is the plant with the least y-coordinate. M11 starts from point (0,yA) heading towards plant A. Notice that the solution path should not cross itself and all of the turns should be counter-clockwise. Also note that the solution may visit more than two plants located on a same straight line.

Input

The first line of the input is M, the number of test cases to be solved (1 <= M <= 10). For each test case, the first line is N, the number of plants in that test case (1 <= N <= 50), followed by N lines for each plant data. Each plant data consists of three integers: the first number is the unique plant index (1..N), followed by two positive integers x and y representing the coordinates of the plant. Plants are sorted by the increasing order on their indices in the input file. Suppose that the values of coordinates are at most 100.

Output

Output should have one separate line for the solution of each test case. A solution is the number of plants on the solution path, followed by the indices of visiting plants in the path in the order of their visits.

Sample Input

2
10
1 4 5
2 9 8
3 5 9
4 1 7
5 3 2
6 6 3
7 10 10
8 8 1
9 2 4
10 7 6
14
1 6 11
2 11 9
3 8 7
4 12 8
5 9 20
6 3 2
7 1 6
8 2 13
9 15 1
10 14 17
11 13 19
12 5 18
13 7 3
14 10 16

Sample Output

10 8 7 3 4 9 5 6 2 1 10
14 9 10 11 5 12 8 7 6 13 4 14 1 3 2



给出一些点,然后要你卷包裹一样卷起了,看图就能懂了

然后就是注意这里有一些点在同一条直线上


附下列代码



#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;const double eps = 1e-8;
int sgn(int x)
{if(abs(x) < eps)return 0;if(x < 0)return -1;else return 1;
}struct point
{int pos;int x,y;point(){}point(int _x,int _y){x=_x,y=_y;}
};
point p[100];double Dis(point p1,point p2){return sqrt((p1.x-p2.x)*(p1.x-p2.x)*1.0+1.0*(p1.y-p2.y)*(p1.y-p2.y));
}
double Cross(point p1,point p2,point p3){return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
point operator - (point A,point B){return point(A.x-B.x, A.y-B.y);
}
point operator + (point A, point B){return point(A.x+B.x, A.y+B.y);
}
point operator * (point A, double p){return point(A.x*p, A.y*p);
}
bool operator == (point A, point B){return (A.x-B.x) == 0 && (A.y-B.y) == 0;
}int pos;
bool cmp(point a,point b)
{double tmp =Cross(a,b,p[pos]);if(sgn(tmp) == 0)return Dis(p[pos],a) < Dis(p[pos],b);else if(sgn(tmp) < 0)return false;else return true;
}int main()
{int n,T;//freopen("in.txt","r",stdin);scanf("%d",&T);while(T--){scanf("%d",&n);scanf("%d%d%d",&p[0].pos,&p[0].x,&p[0].y);for(int i=1;i<n;i++){scanf("%d%d%d",&p[i].pos,&p[i].x,&p[i].y);if(p[i].y<p[0].y||p[i].y==p[0].y&&p[i].x<p[0].x)swap(p[0],p[i]);}pos=0;for(int i=1;i<n;i++){sort(p+i,p+n,cmp);pos++;}printf("%d",n);for(int i=0;i<n;i++)printf(" %d",p[i].pos);puts("");}return 0;
}


这篇关于A - Area 51 Gym - 101334A 计算几何 极坐标 poj 1696 极坐标排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/706093

相关文章

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

Python文本相似度计算的方法大全

《Python文本相似度计算的方法大全》文本相似度是指两个文本在内容、结构或语义上的相近程度,通常用0到1之间的数值表示,0表示完全不同,1表示完全相同,本文将深入解析多种文本相似度计算方法,帮助您选... 目录前言什么是文本相似度?1. Levenshtein 距离(编辑距离)核心公式实现示例2. Jac

Python中经纬度距离计算的实现方式

《Python中经纬度距离计算的实现方式》文章介绍Python中计算经纬度距离的方法及中国加密坐标系转换工具,主要方法包括geopy(Vincenty/Karney)、Haversine、pyproj... 目录一、基本方法1. 使用geopy库(推荐)2. 手动实现 Haversine 公式3. 使用py

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Java List排序实例代码详解

《JavaList排序实例代码详解》:本文主要介绍JavaList排序的相关资料,Java排序方法包括自然排序、自定义排序、Lambda简化及多条件排序,实现灵活且代码简洁,文中通过代码介绍的... 目录一、自然排序二、自定义排序规则三、使用 Lambda 表达式简化 Comparator四、多条件排序五、

JAVA数组中五种常见排序方法整理汇总

《JAVA数组中五种常见排序方法整理汇总》本文给大家分享五种常用的Java数组排序方法整理,每种方法结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录前言:法一:Arrays.sort()法二:冒泡排序法三:选择排序法四:反转排序法五:直接插入排序前言:几种常用的Java数组排序

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti