URAL 2067 Friends and Berries
2017-04-28 20:26
309 查看
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 s i and his attachment to raspberry as
r i.
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((
s v − s u) 2 + (
r v − r
u) 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 · 10 5).
In the next n lines there are two integers in each line —
s i and r
i (−10 8 ≤
s i, r i ≤ 10 8).
It is guaranteed that for every two children their tastes differ. In other words, if
v ≠ u then s v ≠
s u or r
v ≠ r u.
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.
Example
Hint
题目中那两个公式的意思就是如果两个点与其他任意一个点都共线并在同一边上,就存在最好的朋友,否则输出0先排序,最远的两个点即为所求。
8d26
i-th child rates his attachment to strawberry as s i and his attachment to raspberry as
r i.
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((
s v − s u) 2 + (
r v − r
u) 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 · 10 5).
In the next n lines there are two integers in each line —
s i and r
i (−10 8 ≤
s i, r i ≤ 10 8).
It is guaranteed that for every two children their tastes differ. In other words, if
v ≠ u then s v ≠
s u or r
v ≠ r u.
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.
Example
input | output |
---|---|
2 2 3 7 6 | 1 1 2 |
3 5 5 2 -4 -4 2 | 0 |
题目中那两个公式的意思就是如果两个点与其他任意一个点都共线并在同一边上,就存在最好的朋友,否则输出0先排序,最远的两个点即为所求。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; struct node { int x,y; int num; }a[300005]; bool cmp(node a,node b) { if(a.x==b.x) return a.y<b.y; return a.x<b.x; } int main() { int n; scanf("%d",&n); int i; for(i=0;i<=n-1;i++) { scanf("%d%d",&a[i].x,&a[i].y); a[i].num=i+1; } sort(a,a+n,cmp); int flag=0; for(i=0;i<=n-1;i++) { if(abs(a[i].x-a[0].x)*abs(a[i].y-a[n-1].y)!=abs(a[i].x-a[n-1].x)*abs(a[i].y-a[0].y)) { flag=1; break; } } if(!flag) { printf("1\n"); printf("%d %d\n",a[0].num,a[n-1].num); } else printf("0\n"); return 0; }
8d26
相关文章推荐
- URAL 2067 Friends and Berries (推理,数学)
- E - Friends and Berries URAL - 2067(三点共线)
- Ural 2067 Friends and Berries 【思维】
- E - Friends and Berries URAL - 2067 ----三点共线
- URAL 2001. Mathematicians and Berries
- HDU 5874 Friends and Enemies(构造)
- A. Dima and Friends
- Codeforces Round #395 (Div. 1) E. Timofey and our friends animals(lct动态维护mst)
- ural 2063. Black and White
- URAL 2040 Palindromes and Super Abilities 2
- Ural 1750. Pakhom and the Gully
- Codeforces Round #275 (Div. 2) B. Friends and Presents 二分+数学
- URAL 1026 — Questions and Answers
- 一部欢快的儿童小车类动画片,恰克大冒险The Adventures of Chuck andFriends 26集
- URAL 2026 C - Dean and Schedule
- 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
- 【bitset+string+关闭流同步优化】Oleg and Little Ponies URAL - 2108
- codeforces #305 E Mike and friends
- Codeforces D. Friends and Subsequences [RMQ + erfen]
- URAL 题目1553. Caves and Tunnels(Link Cut Tree 修改点权,求两点之间最大)