hdu6325 Interstellar Travel 凸包变形
题目大意:
给出n个平面坐标,保证第一个点和第n个点y值为0,其余点的x坐标都在中间,要从 i 点走到 j 点的要求是 i 点的横坐标严格小于 j 的横坐标,并且消耗的能量是(xi * yj - xj * yi),要求消耗的能量最小(能量可以为负),并且字典序要求最小。
思路:
消耗能量的式子就是两个坐标的叉积,叉积的几何意义就是两个向量对应的平行四边形的面积,但是这个面积会有正负,如果向量 j 在向量 i 的顺时针方向,则这个面积是负的,如果我希望能量最小,那么就尽可能使向量是顺时针方向的,由此发现其实就得到了一个凸包,而且是一个上凸包。经过凸包上的点都符合能量的要求,但是由于要求字典序最小,所以如果凸包上的某一条边上有很多点,那么就需要判断一下这些点的id,如果线段中间的点的id比较小,那么这些点选上了之后,字典序肯定会变小,所以在做凸包的时候就要对凸包算法加一点点变形。
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstring>
#include<queue>
#include<stack>
#define CLR(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
typedef long long ll;
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = ;
struct dian {
double x, y;
int id;
dian (){}
dian(double x, double y, int id) :x(x), y(y), id(id){}
}a[maxn],ch[maxn];
bool cmp(dian &a, dian &b)
{
if (a.x - b.x) return a.x < b.x;
if (a.y - b.y) return a.y < b.y;
return a.id < b.id;
}
typedef dian Vector;
Vector operator -(Vector a, Vector b) {
return Vector ( a.x - b.x, a.y - b.y, );
}
bool operator == (Vector a,Vector b){
return ((a.x==b.x) && (a.y==b.y));
}
double cross(Vector a, Vector b)
{
return a.x*b.y - a.y*b.x;
}
int andrew(dian *p, int n, dian *ch)
{
int m = ;
sort(p, p + n, cmp);
for (int i = ; i < n; i++)
{
if(i>&&p[i]==ch[m-])continue;
while (m > && cross(ch[m - ] - ch[m - ], p[i] - ch[m - ] )<= )
{
if(cross(ch[m - ] - ch[m - ], p[i] - ch[m - ] )< )
m--;
else if(ch[m-].id>p[i].id){
m--;
}else break;
}
ch[m++] = p[i];
}
return m;
}
bool vis[maxn];
int main() {
int n,T;
cin >> T;
while (T--)
{
CLR(vis, inf);
scanf("%d", &n);
for (int i = ; i < n; i++)
{
scanf("%lf%lf", &a[i].x, &a[i].y);
a[i].id = i+;
}
int m = andrew(a, n, ch);
for (int i = ; i<=m-; i++)
{
printf("%d", ch[i].id);
if (i < m-)printf(" ");
else printf("\n");
}
} } /* 1
7
0 0
9 0
3 6
1 2
3 6
2 4
10 0 */
Problem G. Interstellar Travel
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2098    Accepted Submission(s): 544
Little Q knows the position of n planets in space, labeled by 1 to n. To his surprise, these planets are all coplanar. So to simplify, Little Q put these n planets on a plane coordinate system, and calculated the coordinate of each planet (xi,yi).
Little Q plans to start his journey at the 1-th planet, and end at the n-th planet. When he is at the i-th planet, he can next fly to the j-th planet only if xi<xj, which will cost his spaceship xi×yj−xj×yi units of energy. Note that this cost can be negative, it means the flight will supply his spaceship.
Please write a program to help Little Q find the best route with minimum total cost.
In each test case, there is an integer n(2≤n≤200000) in the first line, denoting the number of planets.
For the next n lines, each line contains 2 integers xi,yi(0≤xi,yi≤109), denoting the coordinate of the i-th planet. Note that different planets may have the same coordinate because they are too close to each other. It is guaranteed that y1=yn=0,0=x1<x2,x3,...,xn−1<xn.
A sequence of integers a is lexicographically smaller than a sequence of b if there exists such index j that ai=bi for all i<j, but aj<bj.
3
0 0
3 0
4 0
hdu6325 Interstellar Travel 凸包变形的更多相关文章
- HDU 6325 Problem G. Interstellar Travel(凸包)
		
题意: 给你n个点,第一个点一定是(0,0),最后一个点纵坐标yn一定是0,中间的点的横坐标一定都是在(0,xn)之间的 然后从第一个点开始飞行,每次飞到下一个点j,你花费的价值就是xi*yj-xj* ...
 - 2018HDU多校训练-3-Problem G.     Interstellar Travel
		
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6325 Interstellar Tra ...
 - POJ 1696 Space Ant(凸包变形)
		
Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scie ...
 - 2015 ACM/ICPC Asia Regional Changchun Online Pro 1005 Travel (Krsukal变形)
		
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
 - POJ 1696 - Space Ant 凸包的变形
		
Technorati Tags: POJ,计算几何,凸包 初学计算几何,引入polygon后的第一个挑战--凸包 此题可用凸包算法做,只要把压入凸包的点从原集合中排除即可,最终形成图形为螺旋线. 关于 ...
 - 2018 HDU多校第三场赛后补题
		
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
 - 2018 Multi-University Training Contest 3 Solution
		
A - Problem A. Ascending Rating 题意:给出n个数,给出区间长度m.对于每个区间,初始值的max为0,cnt为0.遇到一个a[i] > ans, 更新ans并且cn ...
 - hdu多校第三场
		
Problem D. Euler Function 思路:打表找找规律. #include<bits/stdc++.h> #define LL long long #define fi f ...
 - 2018 Multi-University Training Contest 3 - HDU Contest
		
题解: solution Code: A. Ascending Rating #include<cstdio> const int N=10000010; int T,n,m,k,P,Q, ...
 
随机推荐
- oracle使用PLSQL免安装客户端
			
2. 下载Oracle Instant Client (32-bit) 只需要下载instantclient-basic-nt-11.2.0.3.0.zip就可以了,其它的都是一些根据不同需要扩展的包 ...
 - 如何获取.properties配置文件
			
如何获取.properties配置文件 分析思路: 先使用流和文件关联,即读取文件 再读取文件内容,一行一行读取 字符分割“=” 键值对 然后把键值对放到集合中去 但是Properties类里面有方 ...
 - location.reload() 和 location.replace()的区别和应用
			
首先介绍两个方法的语法: reload 方法,该方法强迫浏览器刷新当前页面.语法: location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 fals ...
 - Linux基础-工作中经常使用到的linux 命令
			
linux 常用命令 (1)命令ls——列出文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当前目录下以字母a开头的所有文件 ls -l *.d ...
 - cmake的一些词的解释
			
cmake中一些预定义变量 PROJECT_SOURCE_DIR 工程的根目录 PROJECT_BINARY_DIR 运行cmake命令的目录,通常是${PROJECT_SOURCE_DIR} ...
 - 模板方法(Template Method)模式
			
/* * 抽象模版(AbstractClass)角色有如下的责任: 定义了一个或多个抽象操作,以便让子类实现.这些抽象操作叫做基本操作,它们是一个顶级逻辑的组成步骤. 定义并实现了一个模版方法.这个模 ...
 - .net Stream篇(四)
			
FileStream 目录: 如何去理解FileStream? FileStream的重要性 FileStream常用构造函数(重要) 非托管参数SafeFileHandle简单介绍 FileStre ...
 - 我用Django搭网站(1)-新浪微博登录
			
新浪微博第三方登录使用的是OAuth2.0,开发前提已经注册开发者帐号,是开发者. OAuth简介 OAuth: OAuth(开放授权)是一个开放标准,允许用户授权第三方网站访问他们存储在另外的服务提 ...
 - Java集合类总结 (四)
			
PriorityQueue类 优先队列不管你按照什么顺序插入元素,出队列的时候元素都是按顺序输出的.也就是每次调用remove的时候,都返回当前队列中最小的元素.然后队列中的元素不是维持排序状态的,如 ...
 - Vue watch用法
			
Vue.js 提供了一个方法 watch,它用于观察Vue实例上的数据变动.对应一个对象,键是观察表达式,值是对应回调.值也可以是方法名,或者是对象,包含选项. 例如,同一个组件切换时,不会触发生命周 ...