hdu3687 National Day Parade 枚举贪心

http://acm.hdu.edu.cn/showproblem.php?pid=3687

National Day Parade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1828    Accepted Submission(s): 787

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<=Xi<=n,1<= Yi<=m indicating that the coordinate of the ith student is (Xi , Yi ). 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*M的矩阵和N*N个点,所有点只能左右移动,问将所有点排列成矩形的最小步数。

开始题看错了,最后才看见只能左右移,那就很简单了,按x,y排序,枚举矩阵的左上点,每次取的n个肯定只计算y坐标差。。。。

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int INF=0x3f3f3f3f;
const LL inf=(((LL)1)<<61)+5;
struct point{
    int x,y;
}a[60*60];
int n,m;
bool cmp(point u,point v)
{
    if(u.x==v.x) return u.y<v.y;
    return u.x<v.x;
}
int gao(int x)
{
    int ans=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            ans+=abs(a[i*n+j].y-(x+j));
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        for(int i=0;i<n*n;i++)
            scanf("%d%d",&a[i].x,&a[i].y);
        sort(a,a+n*n,cmp);
        int ans=INF;
        for(int i=1;i<=m-n+1;i++)
            ans=min(ans,gao(i));
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-09-27 23:59:44

hdu3687 National Day Parade 枚举贪心的相关文章

枚举+贪心 HDOJ 4932 Miaomiao&#39;s Geometry

题目传送门 1 /* 2 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 3 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则往右边放, 4 判断一下,取最大值.这题二分的内容少 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <cmath> 10 using n

hdu 3687 10 杭州 现场 H - National Day Parade 暴力水题

H - National Day Parade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3687 Appoint description:  System Crawler  (2014-11-08) Description There are n×n students preparing for the National Day

2015 计蒜之道 初赛(4)爱奇艺的自制节目(枚举 贪心)

爱奇艺的自制节目 爱奇艺作为一家视频网站巨头,要为上亿的用户每天都提供“悦享品质”的服务.除了引进一些优秀的影视作品外,爱奇艺还做了一些诸如奇葩说.晓松奇谈的自制节目.爱奇艺最近又准备制作四档新的节目,它们分别是 W, X, Y, Z:但是现在能用来录这些节目的演播室只有两个,分别是演播室 A 和演播室 B. W 节目的现场搭建比较复杂,每一期都要在演播室 A 来录制,X 节目的摄影机位调整会影响节目质量,每一期都固定在演播室 B 来录制.而节目 Y 和节目 Z 的搭建都比较容易移动,摄影也没有

NYOJ 30 &amp;&amp; POJ 1042 Gone Fishing(枚举+贪心)

[题目链接]:Click here~~ [题目大意]: 一个人去钓鱼,在一条单向路上的旁边有n个湖,并且从湖i到湖i+1需要ti的时间,每个湖里面有fi条鱼,每钓一次鱼,鱼会减少di条.在给定时间T内,问如何才能使钓的鱼最多,并记录在各个湖上停留的时间. [解题思路] 此题细节处理好多麻烦,一定要认真看清题意,POJ WA了无数遍,纠结一天.参考了别人的题解,思路如下: 首先须注意的一点是,John只能向前走,返回的话只会增加John在路上的时间,因而减少他钓鱼的时间.因此此题解题步骤如下: 1

[BZOJ 1028] [JSOI2007] 麻将 【枚举+贪心判断】

题目链接:BZOJ - 1028 题目分析 枚举听的是哪种牌,再枚举成对的是哪种牌,再贪心判断: 从1到n枚举每一种牌,如果这种牌的个数小于0,就返回不合法. 将这种牌的张数 % 3, 剩下的只能和 i + 1, i + 2 这两种牌构成顺,所以 Num[i + 1] -= Num[i]; Num[i + 2] -= Num[i]; 牌的种类要枚举到 n + 2,因为第 n - 1 和第 n 种牌可能会减掉 n + 1,n + 2 的牌. 代码 #include <iostream> #inc

Hdu 3687 National Day Parade(暴力)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3687 思路:暴力,枚举每列(矩形开始的列),将同一行的人按列从小到大排序,则每个人的移动距离为abs(g[j][k]-i-k)(第j行的第k个人移动到合适位置距离),枚举累加取最最小值即可. #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include&

Codeforces 761C Dasha and Password(枚举+贪心)

题目链接 Dasha and Password 题目保证一定有解. 考虑到最多只有两行的指针需要移动,那么直接预处理出该行移动到字母数字或特殊符号的最小花费. 然后O(N^3)枚举求最小值即可. 时间复杂度O(N*M+N^3) 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define rep(i,a,b) for(int i(

hihocoder1051(枚举贪心)

题目链接:点击打开链接 解题思路: 明显要消除连续的m才能使收益最大,我们直接暴力的枚举好了,每个区间的a[i + m ] - a[ i - 1] - 1的最大值即所求.这里在左右边界分别添上0和100 完整代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <climits> using namespace std; i

ZOJ 3715 Kindergarten Election(枚举+贪心)

Kindergarten Election Time Limit: 2 Seconds      Memory Limit: 65536 KB At the beginning of the semester in kindergarten, the n little kids (indexed from 1 to n, for convenience) in class need to elect their new leader. The ith kid will vote for his