Claustrophobic Cows

链接:https://ac.nowcoder.com/acm/contest/1081/L
来源:牛客网
 

题目描述

Farmer John has acquired a set of N (2 <= N <= 2,000) touchy cows who are conveniently numbered 1..N. They really hate being too close to other cows. A lot.
FJ has recorded the integer Xi,Yi coordinates of every cow i (1 <= Xi <= 100,000; 1 <= Yi <= 100,000).
Among all those cows, exactly two of them are closest together. FJ would like to spread them out a bit. Determine which two are closest together and print their cow id numbers (i) in numerical order.

By way of example, consider this field of cows (presented on a typewriter grid that has slightly different proportions than you might expect):
                    10 | . . . . . . . 3 . . . . .
                     9 | . 1 . . 2 . . . . . . . .
                     8 | . . . . . . . . . . . . .
                     7 | . . . . . . . . . . 4 . .
                     6 | . . . . . . 9 . . . . . .
                     5 | . 8 . . . . . . . . . . .
                     4 | . . . . . 7 . . . . . . .
                     3 | . . . . . . . . . 5 . . .
                     2 | . . . . . . . . . . . . .
                     1 | . . . . 6 . . . . . . . .
                     0 ---------------------------
                                           1 1 1 1
                       0 1 2 3 4 5 6 7 8 9 0 1 2 3
Quick visual inspection shows that cows 7 and 9 are closest together (the distance separating them is sqrt(1*1+2*2) = sqrt(5), so the output would be '7 9' on a single line (without quotes, of course).

输入描述:

* Line 1: A single integer: N
* Lines 2..N+1: Line i contains the coordinates of cow i expressed as two space-separated integers: Xi and Yi

输出描述:

* Line 1: The two numerical IDs of the closest pair of cows (sorted)

示例1

输入

复制

9
2 9
5 9
8 10
11 7
10 3
5 1
6 4
2 5
7 6

输出

复制

7 9

 

思路:暴力,计算出任意两个奶牛的距离,记录最短距离和对应的奶牛标号

 

代码:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int x[20100], y[20100];

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d %d", &x[i], &y[i]);
    }

    int ans1 = 0, ans2 = 1;
    long long xi, yi, mins = 99999999999;

    for(int i = 0; i < n - 1; i++){
        for(int j = i + 1; j < n; j++){
            xi = (x[i] - x[j]) * (x[i] - x[j]);
            yi = (y[i] - y[j]) * (y[i] - y[j]);
            if(xi + yi < mins){
                ans1 = i;
                ans2 = j;
                mins = xi + yi;
            }
        }
    }
    printf("%d %d\n", ans1 + 1, ans2 + 1);
    return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值