2018 Multi-University Training Contest 3 &&HDU6325 Interstellar Travel【凸包】

                               Problem G. Interstellar Travel

                         Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Problem Description

After trying hard for many years, Little Q has finally received an astronaut license. To celebrate the fact, he intends to buy himself a spaceship and make an interstellar travel.
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×yjxj×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.

Input

The first line of the input contains an integer T(1≤T≤10) , denoting the number of test cases.
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 .

Output

For each test case, print a single line containing several distinct integers p1,p2,...,pm(1≤pin) , denoting the route you chosen is p1→p2→...→pm−1→pm . Obviously p1 should be 1 and pm should be n . You should choose the route with minimum total cost. If there are multiple best routes, please choose the one with the smallest lexicographically.
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 .

Sample Input

1
3
0 0
3 0
4 0

Sample Output

1 2 3

【题目链接】 Interstellar Travel

【题意】

给定平面上n个点,起点横坐标最小,终点横坐标最大。每次可以飞到一个横坐标严格更大的点,代价为两个坐标的叉积。 求起点到终点总代价最小的飞行路线,并输出字典序最小的路线。

【思路】

由于从终点走到起点的叉积为0,我们可以人为地把起点和终点连起来,于是我们要求的代价和的值就是走过的部分面积的相反数。

代价和最小等价于走过的面积最大,那么只要排完序在一个上凸壳上走即可,显然显然起点、终点、凸壳的拐点必选。

求出来的结果是一个凸包,我们需要考虑凸包上有点共线的情况,由于要满足字典序最小的要求,我们可能不需要把这些共线的点都选上:两个端点必选,对于中间的点,如果能使字典序变小就选,否则不选。

#include <cstdio>
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef  long long ll;
const int maxn = 200005;
const ll mod = 1e9+7;
const int INF = 1e9;
const double eps = 1e-6;

int n;
int has[maxn],ans[maxn];

struct node
{
    int x,y;
    int id;
}a[maxn],q[maxn];

bool cmp(const node &a,const node &b)
{
    if(a.x==b.x) return a.y==b.y?a.id<b.id:a.y>b.y;
    return a.x<b.x;
}

int main()
{
    rush()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].id=i;
        }
        sort(a+1,a+1+n,cmp);
        int tail=0;
        for(int i=1;i<=n;i++)
        {
            if(i>1&&a[i].x==a[i-1].x) continue;
            while(tail>1&&(ll)(q[tail].y-q[tail-1].y)*(a[i].x-q[tail].x)<(ll)(a[i].y-q[tail].y)*(q[tail].x-q[tail-1].x)) tail--;
            q[++tail]=a[i];
        }
        for(int i=1;i<=tail;i++) has[i]=0;
        has[1]=has[tail]=1;
        for(int i=2;i<tail;i++)
        {
            if((ll)(q[i].y-q[i-1].y)*(q[i+1].x-q[i].x)!=(ll)(q[i+1].y-q[i].y)*(q[i].x-q[i-1].x)) has[i]=1;
        }
        for(int i=tail;i>=1;i--)
        {
            if(has[i]) ans[i]=q[i].id;
            else ans[i]=min(ans[i+1],q[i].id);
        }
        for(int i=1;i<tail;i++)
        {
            if(ans[i]==q[i].id) printf("%d ",ans[i]);
        }
        printf("%d\n",ans[tail]);
    }
}
发布了259 篇原创文章 · 获赞 100 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/my_sunshine26/article/details/81317308