F - Friends and Berries


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  vu as:  pvu) =  sqrt((  s  v −  s u2 + (  r  v −  r  u2)
The friendliness between three children  vuw is the half the sum of pairwise friendlinesses:  pvuw) = (  pvu) +  pvw) +  puw)) / 2
The best friends are that pair of children  vu for which  v ≠  u and  pvu) ≥  p( vuw) 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 ir  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 10  5.
Example
input output
2
2 3
7 6
1
1 2
3
5 5
2 -4
-4 2
0

思路:

求所有点在一条直线上。

因为p(v,u)>(  p(v,u)+p(v,w)+p(u,w)  )/2 —> p(v,u)>p(v,w)+p(u,w)

把他们看成三个点,w只有在vw线段上才能满足条件。而w必须取任意值都行才可以,所以所有点在一条线段上。

代码:

#include<iostream>
#include<algorithm>
using namespace std;

struct node
{
    int x;
    int y;
    int id;
}s[200010];

int cmp(node a,node b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>s[i].x>>s[i].y;
        s[i].id=i+1;
    }
    sort(s,s+n,cmp);
    int flag=0;
    for(int i=1;i<=n-2;i++)
    {
        if((s[i].x-s[0].x)*(s[n-1].y-s[i].y)!=(s[i].y-s[0].y)*(s[n-1].x-s[i].x))
        {
            flag=1;
            break;
        }
    }
    if(flag==0)
    {
        cout<<"1"<<endl;
        cout<<s[0].id<<" "<<s[n-1].id<<endl;
    }
    else
        cout<<"0"<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值