E - Friends and Berries
URAL - 2067
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.
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 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 10
5.
input | output |
---|---|
2 2 3 7 6 | 1 1 2 |
3 5 5 2 -4 -4 2 | 0 |
题目链接:https://cn.vjudge.net/contest/160744#problem/E
题目的意思就是说找到两个点,使得dis(u,v)>=dis(u,w)+dis(w,v),仔细想一想其实不可能大于(构成三角形的条件)
这个题是个水题,我们判断所有点是否共线,如果是,就输出线段的两个端点。
代码:
#include <bits/stdc++.h>
#define zero(x) (((x)>0?(x):(-x))<eps)
using namespace std;
const int MAXN=2e5+7;
int n;
const double eps=1e-8;
struct Point
{
double x,y;
int num;
bool operator < (const Point &a)const
{
if(a.x!=x)return x<a.x;
else if(a.y!=y)return y<a.y;
else return num<a.num;
}
}p[MAXN];
double xmult(Point p1,Point p2,Point p)
{
return (p1.x-p.x)*(p2.y-p.y)-(p2.x-p.x)*(p1.y-p.y);
}
int dot_inLine(Point p1,Point p2,Point p3)//判断三点共线
{
return zero(xmult(p1,p2,p3));
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;++i)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
p[i].num=i+1;
}
sort(p,p+n);
int flag=1;
for(int i=2;i<n;++i)
{
if(!dot_inLine(p[0],p[1],p[i]))
{
flag=0;
break;
}
}
if(!flag)puts("0");
else
{
puts("1");
printf("%d %d\n",p[0].num,p[n-1].num);
}
return 0;
}