Hyperspace Travel

https://www.hackerrank.com/contests/infinitum16-firsttimer/challenges/hyperspace-travel

给出n个点,是一个m维坐标,要求找一个点,使得这n个点到这个点的哈曼顿距离之和最小。输出字母序最小的一组解

考虑一维的时候,是很简单的,先把x排序,然后取中点就行,偶数的话取左边那个即可。

然而这是n维,但是是一样的,因为x和y是分开算的,什么意思呢?就是是固定的,对于一个点(x0,y0)去到(x1,y1),固定的是要走x1-x0步(x方向)  y是固定走y1-y0步的,

所以x和y不互相影响,把m维拆开来算即可

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 100 + 20;
vector<int>pp[maxn];
void work() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            int x;
            cin >> x;
            pp[j].push_back(x);
        }
    }
    for (int i = 1; i <= m; ++i) {
        sort(pp[i].begin(), pp[i].end());
    }
    int to = 104;
    if (n & 1) {
        for (int i = 1; i <= m; ++i) {
//            cout << pp[i][m / 2] << endl;
            pp[to].push_back(pp[i][n / 2]);
        }
    } else {
        for (int i = 1; i <= m; ++i) {
            pp[to].push_back(pp[i][n / 2 - 1]);
        }
    }
    for (int i = 0; i < m - 1; ++i) {
        cout << pp[to][i] << " ";
    }
    cout << pp[to][m - 1] << endl;
}

int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    work();
    return 0;
}
View Code

 

posted on 2016-09-14 22:08  stupid_one  阅读(230)  评论(0编辑  收藏  举报

导航