F - F HDU - 1173(二維化一維-思惟)

F - F HDU - 1173ios

一個郵遞員每次只能從郵局拿走一封信送信。在一個二維的直角座標系中,郵遞員只能朝四個方向移動,正北、正東、正南、正西。c++

有n個須要收信的地址,如今須要你幫助找到一個地方建設郵局,使得郵遞員送往n個地址的路程之和最短。spa

Input 多組輸入,每組數據的第一行一個整數
n(0<n<1000000),表示地址有n個。在接下來的n行中,每行有兩個實數x,y,表示其中一個收信地址的座標。n = 0時輸入結束。
(實數範圍原題沒給出,c++ double存儲夠用,運算後自行避免精度問題)code

Output 每組輸入,輸出兩個實數x,y(保留兩位),表明郵局位置。若是座標不惟一輸出其中一個最優解便可。string

Sample Input
4
1.0 1.0
3.0 1.0
3.0 3.0
1.0 3.0
0
Sample Output
2.00 2.00

思路

  • 分析:首先明白咱們選的某個位置(x,y),不管在x軸方向,不管咱們讓x等於什麼? 都不會影響在y軸方向的總的最小和距離,同y座標也不會影響在x軸方向的總水平和距離,剩下的就是單獨討論水平、豎直方向的狀況

代碼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
#define ll long long 
#define INF 1e9
#define db double 
const int Len = 1000005;
db x[Len];
db y[Len];

db Solve(db a[], int n)
{
    sort(a + 1, a + 1 + n);
    if(n % 2)
        return a[n/2 + 1]; 
    return (a[n/2] + a[n/2 + 1])/2; 
}

int main()
{
    /* freopen("A.txt","r",stdin); */
    /* freopen("Ans.txt","w",stdout); */
    int n;
    while(scanf("%d", &n) && n)
    {

        for(int i = 1; i <= n; i ++)
            scanf("%lf %lf", &x[i], &y[i]);
        printf("%.2f %.2f\n", Solve(x, n), Solve(y, n));
    }

    return 0;
}
相關文章
相關標籤/搜索