2067. Friends and Berries
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
There is a group of n children. According to a proverb, every man to his own taste. So the children value strawberries and raspberries differently. Let’s say that i-th child rates his attachment to strawberry as si and his attachment to raspberry as ri.
According to another proverb, opposites attract. Surprisingly, those children become friends whose tastes differ.
Let’s define friendliness between two children v, u as: p(v, u) = sqrt((sv − su)2 + (rv − ru)2)
The friendliness between three children v, u, w is the half the sum of pairwise friendlinesses:p(v,u,w) = (p(v,u) + p(v,w) + p(u,w)) / 2
The best friends are that pair of children v, u for which v ≠ u and p(v, u) ≥ p(v,u,w) for every child w. Your goal is to find all pairs of the best friends.
Input
In the first line there is one integer n — the amount of children (2 ≤ n ≤ 2 · 105).
In the next n lines there are two integers in each line — si and ri (−108 ≤ si, ri ≤ 108).
It is guaranteed that for every two children their tastes differ. In other words, if v ≠ u then sv≠ su or rv ≠ ru.
Output
Output the number of pairs of best friends in the first line.
Then output those pairs. Each pair should be printed on a separate line. One pair is two numbers — the indices of children in this pair. Children are numbered in the order of input starting from 1. You can output pairs in any order. You can output indices of the pair in any order.
It is guaranteed that the amount of pairs doesn’t exceed 105.
Samples
input | output |
---|---|
2 |
1 |
3 |
0 |
Problem Author: Alexey Danilyuk (prepared by Alexey Danilyuk, Alexander Borzunov)
Problem Source: Ural Regional School Programming Contest 2015
Problem Source: Ural Regional School Programming Contest 2015
Difficulty: 245
题意:给n个点,
p(v, u) = sqrt((sv − su)^2 + (rv − ru)^2)
p(v,u,w) = (p(v,u) + p(v,w) + p(u,w)) / 2
要求找出p(v, u) ≥ p(v,u,w) 的对数
分析:
变形一下,根据三角形两边之和大于第三边的理论。。。
显然当且仅当n个点共线才有答案
#include <iostream>
#include <cstdio>
using namespace std;
#define INF (1000000001) const int N = ;
int n;
typedef long long LL;
struct Point
{
LL x, y;
} arr[N]; inline void input()
{
cin >> n;
for(int i = ; i <= n; i++)
cin >> arr[i].x >> arr[i].y;
} inline LL multi(Point o, Point a, Point b)
{
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
} inline void solve()
{
for(int i = ; i <= n; i++)
if(multi(arr[], arr[], arr[i]) != )
{
cout << << "\n";
return;
} int x = , y = ;
for(int i = ; i <= n; i++)
{
if(arr[i].x < arr[x].x || (arr[i].x == arr[x].x && arr[i].y < arr[x].y)) x = i;
if(arr[i].x > arr[y].x || (arr[i].x == arr[y].x && arr[i].y > arr[y].y)) y = i;
}
cout << << "\n";
cout << x << " " << y << "\n";
} int main()
{
ios::sync_with_stdio();
input();
solve();
return ;
}