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


    代码不贴了 因为测评关闭了 也懒得写了
  • 相关阅读:
    对vue-cli各个目录的理解 和 在 vue 中使用json-server
    发论文的一些常见问题
    latex初步入门:springer llncs
    docker tomcat8 mysql8部署常见错误
    docker快速部署本地项目到服务器(tomcat8+mysql8)
    IDEA构建spring项目
    [b0042] python 归纳 (二七)_gui_tkinter_基本使用
    [b0038] python 归纳 (二三)_多进程数据共享和同步_队列Queue
    springboot进入html
    HbaseShell启动
  • 原文地址:https://www.cnblogs.com/zy691357966/p/5480464.html
Copyright © 2011-2022 走看看