ural 2067. Friends and Berries
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 2 3 7 6 |
1 1 2 |
3 5 5 2 -4 -4 2 |
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
Tags: geometry
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个点共线才有答案
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 #define INF (1000000001) 5 6 const int N = 200010; 7 int n; 8 typedef long long LL; 9 struct Point 10 { 11 LL x, y; 12 } arr[N]; 13 14 inline void input() 15 { 16 cin >> n; 17 for(int i = 1; i <= n; i++) 18 cin >> arr[i].x >> arr[i].y; 19 } 20 21 inline LL multi(Point o, Point a, Point b) 22 { 23 return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); 24 } 25 26 inline void solve() 27 { 28 for(int i = 3; i <= n; i++) 29 if(multi(arr[1], arr[2], arr[i]) != 0) 30 { 31 cout << 0 << "\n"; 32 return; 33 } 34 35 int x = 1, y = 2; 36 for(int i = 3; i <= n; i++) 37 { 38 if(arr[i].x < arr[x].x || (arr[i].x == arr[x].x && arr[i].y < arr[x].y)) x = i; 39 if(arr[i].x > arr[y].x || (arr[i].x == arr[y].x && arr[i].y > arr[y].y)) y = i; 40 } 41 cout << 1 << "\n"; 42 cout << x << " " << y << "\n"; 43 } 44 45 int main() 46 { 47 ios::sync_with_stdio(0); 48 input(); 49 solve(); 50 return 0; 51 }