HDU 3687 National Day Parade (暴力)

题意:给定 n 个人,在 n 列,问你移动最少的距离,使得他们形成一个n*n的矩阵。

析:这个题本来是要找中位数的,但是有特殊情况,所以改成暴力了,时间也很短,就是从第一个能够放左角的位置开始找,取最大值,挺简单暴力。

我一个同学竟然读对了题,WA了,然后又重新读题,把题意读错了,就是AC了。。。。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 56 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
vector<int> a[maxn];

int main(){
    while(scanf("%d %d", &n, &m) == 2){
        int x, y;
        int cnt = 0;
        if(!m && !n)  break;
        for(int i = 1; i <= n; ++i)  a[i].clear();
        for(int i = 0; i < n*n; ++i){
            scanf("%d %d", &x, &y);
            a[x].push_back(y);
        }
        for(int i = 1; i <= n; ++i)  sort(a[i].begin(), a[i].end());
        int ans = INF;
        for(int i = 1; i <= m-n+1; ++i){
            int cnt = 0;
            for(int j = 1; j <= n; ++j){
                for(int k = 0; k < a[j].size(); ++k){
                    cnt += abs(a[j][k]-i-k);
                }
            }
            ans = min(ans, cnt);
        }
        cout << ans << endl;
    }
    return 0;
}

/*
3 3
1 1
1 1
2 1
1 1
2 1
2 1
3 1
3 1
3 1

*/
原文地址:https://www.cnblogs.com/dwtfukgv/p/5743338.html