Problem G

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 5

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

There are Y lines parallel to x axis. And there are exactly n number of point in each lines. Now wanwan want to move these point in lines which is parallel to y axis. And exactly n lines totaly. And for each point (x,y) , only the value of x can be changed.
And no two point can in the same position. Move a point (x, y) to (x', y) cost abs(x' - x) . Now wanwan want to know the minimum of the total cost to finish the job. 

Input

The first line of the input is an integer T, which means there are exactly T cases in the input.

For each test , the first line contain tow integers Y , n(0<Y ,n< 1000), and follow Y lines , every line contain n integers , the jth integer x[ i ][ j ] represent the x_axis of the jth point in the ith line(abs( x[i][j]) < 1000000 )

Output

For each case, output the minimum of the total cost to finish the job. 

Sample Input

1
3 3
1 2 3
1 3 4
0 1 2

Sample Output

5


题目大意:
  给你Y行点   每行有N个
  使1,2,...Y行的这 N个点 都对齐
  求这样操作的最小位移量

结论1:
  在第I列的点 在移动后依旧排在第I列
  证明:
    先考虑向右移
    (1)若I只要移动到I+1点之前
              显然I依旧在第I列
    (2)若I 必须要跨过I+1个点到K点
    可以等价为 I移动到I+1 I+1再移动到K点
    此时I依旧是第I列
    显然向左移动同理可得。

根据这个结论
可以将问题转换为 将所有行第I列的 合并到一行。
又变成那个经典的问题
一条直线上的点 移动到那移动 总位移最小
显然是中位数 证明见前面的文章


代码不贴了 因为测评关闭了 也懒得写了

【贪心+中位数】【新生赛3 1007题】 Problem G (K)的更多相关文章

  1. 新生赛(2) problem 2 丁磊养猪

    Problem B Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  2. SCNU 2015ACM新生赛决赛【F. Oyk闯机关】解题报告

            题目大意:一个$N$$\times$$N$的阵列,每个格子有$X_{ij}$个调和之音,若每次只能选择走右边或下边,从左上角出发走到右下角,问最多能收集到多少个调和之音?       ...

  3. Codeforces 801 A.Vicious Keyboard & Jxnu Group Programming Ladder Tournament 2017江西师大新生赛 L1-2.叶神的字符串

    A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. SCNU ACM 2016新生赛初赛 解题报告

    新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...

  5. 2018 CCPC 桂林站(upc复现赛)补题

    2018 CCPC 桂林站(upc复现赛)补题 G.Greatest Common Divisor(思维) 求相邻数的差值的gcd,对gcd分解素因子,对所有的素因子做一次遍历,找出最小答案. 几个样 ...

  6. Problem G 宝石合成 (内蒙古14年省赛)

    1117: Problem G 宝石合成 时间限制: 1 Sec  内存限制: 128 MB 提交: 18  解决: 4 [提交][状态][讨论版] 题目描写叙述 故事人物:豆豆强 走上致富之路的豆豆 ...

  7. Triangle (第8届山东省赛的某题)

    triangle(第8届山东省赛的某题) 传送门 题意:喵了个呜,这题意真是峰回路转啊.懒死了,不想描述. 做法:我们拿set或线段树维护exp的最小值,每次取出exp值最小的边,删除之.并更新这条边 ...

  8. BZOJ4977 八月月赛 Problem G 跳伞求生 set 贪心

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4977 - 八月月赛 Problem G 题意 小明组建了一支由n名玩家组成的战队,编号依次为1到n ...

  9. 7.27 NOIP模拟测试9 随 (rand)+单(single)+题(problem)

    T1 随 (rand) dp+矩阵优化+原根 看着题解懵了一晚上加一上午,最后还是看了DeepinC的博客才把暴力码出来,正解看得一知半解,循环矩阵也不太明白,先留坑吧.暴力里用二维矩阵快速幂会tle ...

随机推荐

  1. VS2008找不到MFC90d.dll错误解决方法

    问题是在更新嵌入的清单文件时发生的,由于FAT32的原因而未能更新嵌入的清单文件,于是我们有如下两种解决方法: (1)不启用增量链接.在项目的“属性|配置属性|链接器|常规”中的“启用增量链接”选择“ ...

  2. Tomcat 常见问题篇

    Tomcat 常见问题一.Tomcat常见问题 1.Tomcat web容器出现故障时,我们通过Tomcat自带的logs查看原因,以下错误提示都是源于logs 2.Connection refuse ...

  3. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的解决方案

    今天写了一个excel表的导入功能,结果在excel表中的内容导入到页面时报错:SQL  Server 阻止了对组件 'Ad Hoc Distributed Queries' 的  STATEMENT ...

  4. maven:安装m2eclipse插件

    一.安装maven 1.下载maven:http://pan.baidu.com/s/1hqIbx6s 2.解压并配置path路径(直接替换最新的maven包,即可升级) 3.测试 二.安装m2ecl ...

  5. 百度地图API提示"230错误 APP Scode校验失败"

    笔者近2天在 Android Studio上玩了一下百度地图,碰到了常见的"230错误 APP Scode校验失败",下面我来介绍一下具体的解决办法. 1.在andriodstud ...

  6. tomcat7.0的源码下载

    如果想知道servlet的HttpServlet的实现细节,想知道jsp的org.apache.jasper.runtime.HttpJspBase的实现细节,想知道tomcat关于servlet和j ...

  7. Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群

    本文的英文版本链接是 http://xuri.me/2013/11/20/install-mysql-cluster-on-ubuntu-12-04-lts.html MySQL Cluster 是 ...

  8. Wireless Network(POJ 2236)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 20724   Accepted: 871 ...

  9. python操作redis-过期时间

    #!/usr/bin/python #!coding:utf-8 import time import redis if __name__ == "__main__": try: ...

  10. Strtus2工作流程及原理

    从上图可以看出,一个请求在Struts2 框架中的处理大概分为以下几个步骤:1.客户端初始化一个指向Servlet 容器(例如Tomcat)的请求:2.这个请求经过一系列的过滤器(Filter)(这些 ...