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

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

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

代码如下:

  1. #include <cstdio>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <iostream>
  6. #include <cstring>
  7. #include <set>
  8. #include <queue>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <map>
  12. #include <cctype>
  13. using namespace std ;
  14. typedef long long LL;
  15. typedef pair<int, int> P;
  16. const int INF = 0x3f3f3f3f;
  17. const double inf = 0x3f3f3f3f3f3f3f;
  18. const double eps = 1e-8;
  19. const int maxn = 56 + 5;
  20. const int dr[] = {0, 0, -1, 1};
  21. const int dc[] = {-1, 1, 0, 0};
  22. int n, m;
  23. inline bool is_in(int r, int c){
  24. return r >= 0 && r < n && c >= 0 && c < m;
  25. }
  26. vector<int> a[maxn];
  27.  
  28. int main(){
  29. while(scanf("%d %d", &n, &m) == 2){
  30. int x, y;
  31. int cnt = 0;
  32. if(!m && !n) break;
  33. for(int i = 1; i <= n; ++i) a[i].clear();
  34. for(int i = 0; i < n*n; ++i){
  35. scanf("%d %d", &x, &y);
  36. a[x].push_back(y);
  37. }
  38. for(int i = 1; i <= n; ++i) sort(a[i].begin(), a[i].end());
  39. int ans = INF;
  40. for(int i = 1; i <= m-n+1; ++i){
  41. int cnt = 0;
  42. for(int j = 1; j <= n; ++j){
  43. for(int k = 0; k < a[j].size(); ++k){
  44. cnt += abs(a[j][k]-i-k);
  45. }
  46. }
  47. ans = min(ans, cnt);
  48. }
  49. cout << ans << endl;
  50. }
  51. return 0;
  52. }
  53.  
  54. /*
  55. 3 3
  56. 1 1
  57. 1 1
  58. 2 1
  59. 1 1
  60. 2 1
  61. 2 1
  62. 3 1
  63. 3 1
  64. 3 1
  65.  
  66. */

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

  1. hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0

    H - National Day Parade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  2. HDU 2920 分块底数优化 暴力

    其实和昨天写的那道水题是一样的,注意爆LL $1<=n,k<=1e9$,$\sum\limits_{i=1}^{n}(k \mod i) = nk - \sum\limits_{i=1}^ ...

  3. HDU 3360 National Treasures 奇偶匹配的最低点覆盖

    标题来源:pid=3360">HDU 3360 National Treasures 意甲冠军:假设a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 ...

  4. hdu 5277 YJC counts stars 暴力

    YJC counts stars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  5. HDU 5762 Teacher Bo (暴力)

    Teacher Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...

  6. hdu 4712 Hamming Distance(随机函数暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  7. hdu 4876 ZCC loves cards(暴力)

    题目链接:hdu 4876 ZCC loves cards 题目大意:给出n,k,l,表示有n张牌,每张牌有值.选取当中k张排列成圈,然后在该圈上进行游戏,每次选取m(1≤m≤k)张连续的牌,取牌上值 ...

  8. HDU 5442 Favorite Donut(暴力 or 后缀数组 or 最大表示法)

    http://acm.hdu.edu.cn/showproblem.php?pid=5442 题意:给出一串字符串,它是循环的,现在要选定一个起点,使得该字符串字典序最大(顺时针和逆时针均可),如果有 ...

  9. HDU 5273 Dylans loves sequence 暴力递推

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5273 bc:http://bestcoder.hdu.edu.cn/contests/con ...

随机推荐

  1. 使用ssh公钥密钥自动登陆linux服务器

    转自:http://7056824.blog.51cto.com/69854/403669 作为一名 linux 管理员,在多台 Linux 服务器上登陆进行远程操作是每天工作的一部分.但随着服务器的 ...

  2. JAVA中获取项目文件路径

    在java中获得文件的路径在我们做上传文件操作时是不可避免的. web 上运行 1:this.getClass().getClassLoader().getResource("/" ...

  3. mybatis源码分析(1)——SqlSessionFactory实例的产生过程

    在使用mybatis框架时,第一步就需要产生SqlSessionFactory类的实例(相当于是产生连接池),通过调用SqlSessionFactoryBuilder类的实例的build方法来完成.下 ...

  4. bzoj2561: 最小生成树

    如果出现在最小生成树上,那么此时比该边权值小的边无法连通uv.据此跑最小割(最大流)即可. #include<cstdio> #include<cstring> #includ ...

  5. codeforces 340C Tourist Problem(简单数学题)

    题意:固定起点是0,给出一个序列表示n个点,所有点都在一条直线上,其中每个元素代表了从起点到这个点所走的距离.已知路过某个点不算到达这个点,则从起点出发,到达所有点的方案有许多种.求所有方案走的总路程 ...

  6. poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)

    多校第七场考了一道lca,那么就挑一道水题学习一下吧= = 最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u, ...

  7. django - django 承接nginx请求

    # -*- coding: utf-8 -*- import os import sys import tornado.ioloop import tornado.web import tornado ...

  8. VS2010下编译安装DarwinStreamingServer5.5.5

    源码下载链接:http://dss.macosforge.org/源码版本: 5.5.5版本电脑环境:visual studio2010,window 7 x64系统.用VS2010打开WinNTSu ...

  9. linux笔试

    在对linux基本知识的归纳总结之后,这里是一份linux的测试题.希望能帮助大家复习和熟悉linux知识. 一.选择题 1.cron 后台常驻程序 (daemon) 用于:  A. 负责文件在网络中 ...

  10. Mybaits+SpringMVC项目(含代码生成工具源码)

       大家下载下来修改数据库配置应该就能运行起来,里面有一个SM的简单案例了,还有说明文件. 运行效果    工具类可以生成Springmvc+mybatis的相关类和配置文件,并具有增删查改的功能, ...