鱼C论坛

 找回密码
 立即注册
查看: 1138|回复: 0

[技术交流] 1.1Dance Party

[复制链接]
发表于 2023-8-16 11:57:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Seawolf 于 2023-8-17 08:49 编辑
Dance Party
Emma and the rest of her friends totaling N (2 <= N <= 5,000) have gone for a party. For the people-only part of the dance, two people are chosen as the "Star of the Party". DJ records the X,Y coordinates (0 <= X_i <= 10,000; 0 <= Y_i <= 10,000) of everyone in the dance floor and asks you to determine the indices of the two persons who are farthest apart(which are guaranteed to be unique). The standard cartesian distance is calculated as the square root of the sum of the squares of the row and column coordinate differences.

In a dance with seven people:

8 | . . . . . . . . . .
7 | C . . . . . . . . .
6 | . . . . . . C . . .
5 | . . C . . . . . . .
4 | . . . . . . . . . .
3 | . . C . . . . . . .
2 | . . . . C C . . C .
1 | . . . . . . . . . .
0 +--------------------
   0 1 2 3 4 5 6 7 8 9 10
DJ hopes you would choose the people at 1,7 and 9,2 as farthest apart.

INPUT FORMAT

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains the integer coordinate location of person i: X_i and Y_i

OUTPUT FORMAT

* Line 1: Two sorted integers that are the indices of the two people that are located farthest apart.

SAMPLE INPUT

7
3 5
1 7
5 2
3 3
6 2
9 2
7 6
SAMPLE OUTPUT

2 6
Person #2 and Person #6 are the person numbers of the people from the example in the text.



O(n) Solution!
#include <cstdio>
#include <iostream>
using namespace std;

const int M = 5005;
int pos[M][2];

int distance(int i, int j)
{
    return (pos[i][0] - pos[j][0]) * (pos[i][0] - pos[j][0]) + (pos[i][1] - pos[j][1]) * (pos[i][1] - pos[j][1]); // using dis square instead of dis to avoid the precision issue.
}

int main()
{
    int N;
    cin >> N;
    
    for(int i = 1; i <= N; i++)
    {
        cin >> pos[i][0] >> pos[i][1];
    }
    
    int result = 0;
    int first = 0, second = 0;
    
    for(int i = 1; i <= N; i++)
    {
        for(int j = i + 1; j <= N; j++)
        {
            int dis = distance(i, j);
            if(dis > result)
            {
                result = dis;
                first = i;
                second = j;
            }
        }
    }
    
    cout << first << " " << second << endl; 
}

本帖被以下淘专辑推荐:

  • · USACO|主题: 2, 订阅: 0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-12-24 02:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表