HDU 3687 National Day Parade (枚举)

49 篇文章 0 订阅

National Day Parade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2323    Accepted Submission(s): 1009



Problem Description
There are n×n students preparing for the National Day parade on the playground. The playground can be considered as a n×m grid. The coordinate of the west north corner is (1,1) , and the coordinate of the east south corner is (n,m).

When training, every students must stand on a line intersection and all students must form a n×n square. The figure above shows a 3×8 playground with 9 students training on it. The thick black dots stand for the students. You can see that 9 students form a 3×3 square.

After training, the students will get a time to relax and move away as they like. To make it easy for their masters to control the training, the students are only allowed to move in the east-west direction. When the next training begins, the master would gather them to form a n×n square again, and the position of the square doesn’t matter. Of course, no student is allowed to stand outside the playground.

You are given the coordinates of each student when they are having a rest. Your task is to figure out the minimum sum of distance that all students should move to form a n×n square.
 

Input
There are at most 100 test cases.
For each test case:
The first line of one test case contain two integers n,m. (n<=56,m<=200)
Then there are n×n lines. Each line contains two integers, 1<=X i<=n,1<= Y i<=m indicating that the coordinate of the i th student is (X i , Y i ). It is possible for more than one student to stand at the same grid point.

The input is ended with 0 0.
 

Output
You should output one line for each test case. The line contains one integer indicating the minimum sum of distance that all students should move to form a n×n square.
 

Sample Input
  
  
2 168 2 101 1 127 1 105 2 90 0 0
 

Sample Output
  
  
41
 

Source
2010 Asia Hangzhou Regional Contest

大体题意:
有n*n 个学生,一开始他们排成了正方形,休息时,每一行的学生只能在每一行走动,告诉你他们休息时的坐标,求出最少多少步,他们能够在度排成正方形。
思路:
卡了足足三个小时才A了这道题目,自己思路还是差远了。
可以枚举正方的左边,因为列数最多是200,先给学生按照列数排序,然后枚举左边,从左边依次往右加学生,求出个最小值即可!
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<cctype>
#include<cmath>
#include<cstdlib>
using namespace std;
const int maxn = 4000 + 10;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
typedef long long ll;
typedef unsigned long long llu;
int flag;
struct Node{
    int x,y;
    bool operator < (const Node & rhs) const {
        return abs(flag - y) < abs(flag - rhs.y);
    }
}p[maxn];
int cntx[60][210];
bool cmp(const Node & lhs,const Node & rhs){
    return lhs.y < rhs.y;
}
const int con = 60;
int main(){

    int n,m;
    while(scanf("%d %d",&n,&m) == 2 && (n || m)){
//        int sum = 0;
        memset(cntx,0,sizeof cntx);
//        memset(visx,0,sizeof visx);
        for (int i = 0; i < n*n; ++i){

            scanf("%d%d",&p[i].x,&p[i].y);
        }
            sort(p,p+n*n,cmp);
        int ans = inf;
        for (int i = 1; i <= m; ++i){
            memset(cntx,0,sizeof cntx);
            flag = i;

            int sum = 0;
            for (int j = 0; j < n*n; ++j){
                for (int x = 0; x <= n; ++x){
                    if (cntx[p[j].x][x] == 0){
                        cntx[p[j].x][x] = 1;
                        sum += abs(i + x - p[j].y);
                        break;
                    }

                }
            }
            ans = min(ans,sum);
            if (ans == 0)break;
        }
        printf("%d\n",ans);

    }

    return 0;
}
/*
2 166
1 4 1 4 2 5 2 5
*/


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值