首页 > 其他分享 >题解:CodeForces 618C Constellation[贪心/模拟]

题解:CodeForces 618C Constellation[贪心/模拟]

时间:2024-07-14 14:18:28浏览次数:16  
标签:std 618C int 题解 CodeForces stars triangle kx first

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.

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

相关文章

  • 题解:CodeForces 835 D Palindromic characteristics[区间dp/马拉车Manacher]
    CodeForces835DD.Palindromiccharacteristicstimelimitpertest:3secondsmemorylimitpertest:256megabytes*inputstandardinputoutputstandardoutputPalindromiccharacteristicsofstring\(s\)withlength\(|s|\)isasequenceof\(|s|\)in......
  • 题解:CodeForces 1019 A Elections[贪心/三分]
    CodeForces1019AA.Electionstimelimitpertest:2secondsmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputAsyouknow,majorityofstudentsandteachersofSummerInformaticsSchoolliveinBerlandforthemostparto......
  • [CF1941E] Rudolf and k Bridges 的题解
    题目大意在第\((i,j)\)个格子修建一个桥墩需要\(a_{i,j}+1\)的花费而且要求\((i,0)\)与\((i,m)\)必须修建桥墩并且桥墩之间的距离不得大于\(d\)。现在需要求见\(k\)个连续的桥,求最小代价。其中\(1\lek\len\le100,3\lem\le2\cdot10,1\led\lem\)。思路因为......
  • [ABC206E] Divide Both 的题解
    题目大意求出从\(l\)至\(r\)中满足以下条件的\((x,y)\)的个数。\(\gcd(x,y)\ne1\)且\(\gcd(x,y)\nex\)且\(\gcd(x,y)\ney\)。其中\(1\lel\ler\le10^6\)。思路正难则反,所以可以求出所有互质或者是相互倍数的\((x,y)\)的对数,在将其减去所有的方案数就是......
  • [CF1178D] Prime Graph 的题解
    题目大意构造一个简单无向图,是所有的有度的点的度都是质数而且总共的边的数量的个数是质数。思路因为需要让所有的入度都为质数,所以我们可以找到两个相邻的质数\(2,3\),因为这样即使增加了一条边那么这个节点的度也是质数。先将这个图构成一个巨大的环,接着如果所有的边数并不......
  • [CF1538F] Interesting Function 的题解
    题目大意给定两个正整数\(l,r\),将\(l\)不断加\(1\)直到\(l=r\),求出这一过程中\(l\)发生变化的位数总数。\(1\lel<r\le10^9\)。思路假设从\(l\)处理到\(r\)变化的次数为\(f(l,r)\)。因为直接求解出\(f(l,r)\)十分困难,所以可以通过求出\(f(0,l)\)......
  • 题解:CodeForces 1511 C Yet Another Card Deck[暴力/模拟]
    CodeForces1511CC.YetAnotherCardDeckDescriptionYouhaveacarddeckof\(n\)cards,numberedfromtoptobottom,i. e.thetopcardhasindex\(1\)andbottomcard —index\(n\).Eachcardhasitscolor:the\(i\)-thcardhascolor\(a_i\......
  • 「ABC217F」Make Pair 的题解
    题目大意一共\(2N\)个学生站成一排,其中有\(M\)对朋友关系。老师每次从队列中挑出两个相邻的学生作为同桌。为了关系和睦,每次选出的两个学生必须是朋友关系。选出的两个学生离开队列,空出来的位置左右合拢。请问老师有多少种方式选完所有学生?对于两种选人的方案,即使同桌关系相......
  • [COCI2015-2016#6] PAROVI 的题解
    题意选择一些\(n\)一下互质的二元组\(\{a,b\}\),求对于任意\(x\in\big[2,n\big]\)都不满足\(a,b<x\)和\(a,b\gex\)的个数。简化题意因为无解的情况只发生在所有的\(\{a,b\}\)之间没有多余的位置用于放置\(x\),所以题意可以抽象成这样:选择一些区间互质的区间\([a......
  • [COCI2006-2007#4] ZBRKA 的题解
    题目大意在一个长度为\(n\)的排列中找出逆序对数量恰好为\(c\)的排列总数,其中\(1\len\le10^3,1\lec\le10^4\)。思路考虑将\(1\)到\(n\)这些数从小到大一次填进去,因为每一次填入的数多是最大的,所以逆序对增加的数量只与其所在的位置相关,所以设计\(f_{i,j}\)表......