CodeForces 618C
C. Constellation
time limit per test: 2 seconds
memory limit per test: 256 megabytes
inputstandard input
outputstandard output
Cat Noku has obtained a map of the night sky. On this map, he found a constellation with n stars numbered from \(1\) to \(n\). For each \(i\), the \(i\)-th star is located at coordinates (\(x_i\), \(y_i\)). No two stars are located at the same position.
In the evening Noku is going to take a look at the night sky. He would like to find three distinct stars and form a triangle. The triangle must have positive area. In addition, all other stars must lie strictly outside of this triangle. He is having trouble finding the answer and would like your help. Your job is to find the indices of three stars that would form a triangle that satisfies all the conditions.
It is guaranteed that there is no line such that all stars lie on that line. It can be proven that if the previous condition is satisfied, there exists a solution to this problem.
Input
The first line of the input contains a single integer \(n (3 ≤ n ≤ 100 000)\).
Each of the next \(n\) lines contains two integers \(x_i\) and \(y_i (-10^9 ≤ x_i, y_i ≤ 10^9)\).
It is guaranteed that no two stars lie at the same point, and there does not exist a line such that all stars lie on that line.
Output
Print three distinct integers on a single line — the indices of the three points that form a triangle that satisfies the conditions stated in the problem.
If there are multiple possible answers, you may print any of them.
Examples
input 1
3
0 1
1 0
1 1
output 1
1 2 3
input 2
5
0 0
0 2
2 0
2 2
1 1
output 2
1 3 5
Note
In the first sample, we can print the three indices in any order.
In the second sample, we have the following picture.
Note that the triangle formed by starts \(1\), \(4\) and \(3\) doesn't satisfy the conditions stated in the problem, as point \(5\) is not strictly outside of this triangle (it lies on it's border).
我的题解
如果他不说不能有其他星星贴在三角形边上,那还容易一点
但是他说了,那就要加点操作了
我的做法是:首先排个序,按照x和y坐标从大到小排序
这样的话下面的组成三角形和就不会和其他能组成三角形的点的连线了
为什么不会呢?后面我来解释一下
首先,怎么确定三个点能不能组成三角形?我们要确定顶点之间的斜率不相同
那么我们怎么储存斜率呢?我是用的两个整型的比值
这两个整型必须是最简比,且正负确定,否则无法比较
那么在排序之后,我首先确定了一条边:第一个点和第二个点的连线
因为已经排好序了,所以这条连线一定是所有连线之中最靠左靠上的一条,
因此往后面找点的时候,如果一个点不在这条线的延长线上,就一定会被找到
假如说这个点和第一第二个点的连线上有其他点的话
那么这个“其他点”一定会比“这个点”先被找到!就杜绝了连线上有其他点的情况出现。
这就是排序保持单调性的原因。
我的代码
#include <bits/stdc++.h>
#define int long long
int t,n;
std::map<std::pair<int,int>,int> mp;
void solve()
{
std::cin >> n;
std::vector<std::pair<int,int> > a(n);
for(int i = 0 ; i < n ; i ++)
{
std::cin >> a[i].first >> a[i].second;
mp[a[i]] = i+1;
}
std::sort(a.begin(),a.end());
std::pair<int,int> ki;
int kx = a[0].first - a[1].first;
int ky = a[0].second - a[1].second;
if(kx < 0)
{
kx *= (-1);
ky *= (-1);
}
if(kx == 0 || ky == 0)
{
if(kx == 0) ki = {0,1};
else ki = {1,0};
}
else ki = {kx/std::__gcd(kx,std::abs(ky)),ky/std::__gcd(kx,std::abs(ky))};
for(int i = 2 ; i < n ; i ++)
{
int kxq = a[i].first - a[0].first;
int kyq = a[i].second - a[0].second;
if(kxq < 0)
{
kxq *= (-1);
kyq *= (-1);
}
std::pair<int,int> th;
if(kxq == 0) th = {0,1};
else if(kyq == 0) th = {1,0};
else th = {kxq/std::__gcd(kxq,std::abs(kyq)),kyq/std::__gcd(kxq,std::abs(kyq))};
if(th != ki)
{
std::cout << mp[a[0]] << " " << mp[a[1]] << " " << mp[a[i]];
return;
}
}
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
solve();
return 0;
}
标签:std,618C,int,题解,CodeForces,stars,triangle,kx,first
From: https://www.cnblogs.com/jiejiejiang2004/p/18301505