zoukankan      html  css  js  c++  java
  • 【贪心+中位数】【新生赛3 1007题】 Problem G (K)

    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列的 合并到一行。
    又变成那个经典的问题
    一条直线上的点 移动到那移动 总位移最小
    显然是中位数 证明见前面的文章


    代码不贴了 因为测评关闭了 也懒得写了
  • 相关阅读:
    三、类的实例方法(阶段三)
    二、类的构造方法(阶段三)
    一、对象与类的区别(阶段三)
    五、数组(阶段二)
    四、(续)流程控制--while for(阶段二)
    四、流程控制--if else和switch(阶段二)
    Java 自带的加密类MessageDigest类(加密MD5和SHA)
    java实现微信扫一扫详解
    springmvc字符编码过滤器CharacterEncodingFilter浅析
    Spring的初始化:org.springframework.web.context.ContextLoaderListener
  • 原文地址:https://www.cnblogs.com/zy691357966/p/5480464.html
Copyright © 2011-2022 走看看