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

*/
时间: 2024-08-02 13:59:35

HDU 3687 National Day Parade (暴力)的相关文章

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&

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

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

HDU 4930 Fighting the Landlords(暴力枚举+模拟)

HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型,如果先手能一步走完,或者一步让后手无法管上,就赢 思路:先枚举出两个人所有可能的牌型的最大值,然后再去判断即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Player { int rank[15]; } p1, p2; int t, h

hdu 1399 Starship Hakodate-maru (暴力搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1399 题目大意:找到满足i*i*i+j*(j+1)*(j+2)/6形式且小于等于n的最大值. 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 while(scanf("%d",&n),n) 10 { 11 int j,

HDU 5280 Senior&#39;s Array (暴力,水)

题意:给一个数列,再给一个数字p,要求p一定要替换掉数列中的一个元素,然后求最大连续子序列之和. 思路:1000*1000的复杂度,O(n*n) .就是每个都试,然后求和. 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define pii pair<int,int> 4 #define INF 0x7f7f7f7f 5 using namespace std; 6 const int N=2000; 7 int a[N]

hdu 4932 Miaomiao&#39;s Geometry(暴力)

题目链接:hdu 4932 Miaomiao's Geometry 题目大意:在x坐标上又若干个点,现在要用若干条相等长度的线段覆盖这些点,若一个点被一条线段覆盖,则必须在这条线的左端点或者是右端点,并且各个线段放的位置不能又重叠,求最大长度. 解题思路:这题有坑点,比赛的时候o(n)的算法去寻找两点之间最短距离.但起始这样是不行的,比如-1 0 10 12 18 20,这样维护过去的话,最短应该是12~18,长度为6,这段线段可以覆盖12和18的点,然后-1和20又在两端.于是只有0和10两点

hdu 4876 ZCC loves cards(暴力)

题目链接:hdu 4876 ZCC loves cards 题目大意:给出n,k,l,表示有n张牌,每张牌有值.选取其中k张排列成圈,然后在该圈上进行游戏,每次选取m(1≤m≤k)张连续的牌,取牌上值的亦或和.要求找到一个圈,使得L~R之间的数都可以得到,输出R.如果R < L输出0. 解题思路:暴力,首先预处理出来每种选取的亦或值,然后在该基础上从可以组成L的状态中挑选一个,L+1的状态中挑取一个,知道说总的挑取出所有状态中选中的牌的个数大于K为值,然后用全排序去查找最大的R. #includ

HDU 3360 National Treasures 奇偶匹配最小点覆盖

题目来源:HDU 3360 National Treasures 题意:如果a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 如果对应位是1 说明在那里放一个守卫可以看住a[i][j]位置上的这个东西 思路:明显死最小点覆盖 奇偶匹配建图 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 55; in