暴力枚举
首先,读清题,题意:给你两个数n,m,n表示接下来会有n*n对数据,m表示图的面积为n*m,如果n和m都为0,则输入结束,在接下来的n*n对数据表示n*n个人的坐标(x,y),现在问你怎么用最小的移动距离,使得这n*n个人组成一个n*n的方阵。一开始我就读到这,直到后来发现:“the students are only allowed to move in the east-west direction”,就是说,每行都必有n个人,,,
代码:
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <bitset>
#define PI acos(-1)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
using std::bitset;
typedef long long LL;
typedef double db;
const int maxn = 211;
vector<int>maps[maxn];
int cnt = 0;
int main(void)
{
int n, m;
while(cin >> n >> m && (n + m))
{
int xx, yy, minn = INT_MAX;
for(int i = 1; i <= n; i++)
{
maps[i].clear();
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> xx >> yy;
maps[xx].push_back(yy);
}
}
for(int i = 1; i <= n; i++)
{
sort(maps[i].begin(), maps[i].end());
}
for(int i = 1; i <= m; i++)
{
int sum = 0;
for(int j = 1; j <= n; j++)
{
for(int k = 0; k < n; k++)
{
sum += abs(maps[j][k] - (i + k));
}
}
minn = min(minn, sum);
}
cout << minn << endl;
}
return 0;
}
1033

被折叠的 条评论
为什么被折叠?



