zoukankan      html  css  js  c++  java
  • <Codeforces Round #127 (Div. 1)> B. Guess That Car!(难题)

    B. Guess That Car!
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A widely known among some people Belarusian sport programmer Yura possesses lots of information about cars. That is why he has been invited to participate in a game show called "Guess That Car!".

    The game show takes place on a giant parking lot, which is 4n meters long from north to south and 4mmeters wide from west to east. The lot has n + 1 dividing lines drawn from west to east and m + 1dividing lines drawn from north to south, which divide the parking lot into n·m 4 by 4 meter squares. There is a car parked strictly inside each square. The dividing lines are numbered from 0 to n from north to south and from 0 to m from west to east. Each square has coordinates (i, j) so that the square in the north-west corner has coordinates (1, 1) and the square in the south-east corner has coordinates(n, m). See the picture in the notes for clarifications.

    Before the game show the organizers offer Yura to occupy any of the (n + 1)·(m + 1) intersection points of the dividing lines. After that he can start guessing the cars. After Yura chooses a point, he will be prohibited to move along the parking lot before the end of the game show. As Yura is a car expert, he will always guess all cars he is offered, it's just a matter of time. Yura knows that to guess each car he needs to spend time equal to the square of the euclidean distance between his point and the center of the square with this car, multiplied by some coefficient characterizing the machine's "rarity" (the rarer the car is, the harder it is to guess it). More formally, guessing a car with "rarity" c placed in a square whose center is at distance d from Yura takes c·d2 seconds. The time Yura spends on turning his head can be neglected.

    It just so happened that Yura knows the "rarity" of each car on the parking lot in advance. Help him choose his point so that the total time of guessing all cars is the smallest possible.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the sizes of the parking lot. Each of the next n lines contains m integers: the j-th number in the i-th line describes the "rarity" cij (0 ≤ cij ≤ 100000) of the car that is located in the square with coordinates (i, j).

    Output

    In the first line print the minimum total time Yura needs to guess all offered cars. In the second line print two numbers li and lj (0 ≤ li ≤ n, 0 ≤ lj ≤ m) — the numbers of dividing lines that form a junction that Yura should choose to stand on at the beginning of the game show. If there are multiple optimal starting points, print the point with smaller li. If there are still multiple such points, print the point with smaller lj.

    Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use thecincout streams or the %I64d specifier.

    Sample test(s)
    input
    2 3
    3 4 5
    3 9 1
    
    output
    392
    1 1
    
    input
    3 4
    1 0 0 0
    0 0 3 0
    0 0 5 5
    
    output
    240
    2 3
    
    Note

    In the first test case the total time of guessing all cars is equal to 3·8 + 3·8 + 4·8 + 9·8 + 5·40 + 1·40 = 392.

    The coordinate system of the field:

    官方提供的解题报告:http://codeforces.com/blog/entry/4808

    Problem В(div 1)/D(div 2) — Guess That Car!

    We need to find such x and y that the value of  is minimum possible. This expression can be rewritten as . Note that the first part doesn’t depend on y and the second part doesn’t depend on x, so we can minimize these parts separately. Here is how to minimize , the second part is minimized similarly. As the expression in the brackets doesn’t depend on j, this part can be rewritten as , where . Now it’s enough to calculate the required value for all possible values of x and choose x for which this value is the smallest. The optimal value of y can be found similarly.

    The overall complexity of this solution is O(n·m + n2 + m2).

    As the objective function is convex, other approaches to this problem are possible, for example, ternary search, gradient descent or analytical approach (calculation of derivatives).

    AC CODE:

    //Memory: 5400 KB		Time: 390 MS
    //Language: GNU C++ 4.6		Result: Accepted
    
    #include <iostream>
    #include <cstdio>
    #define MAXN 1001
    using namespace std;
    
    int car[MAXN][MAXN];
    long long si[MAXN] = {0}, sj[MAXN] = {0}; //要设为long long才能过
    
    int main()
    {
        int n, m, li, lj;
        long long sumx, sumy;
        scanf("%d %d", &n, &m);
        //注意,(i,j)对应car[j][i]
        //行号:y轴
        for(int j = 1; j <= n; j++)
        {
            //列号:x轴
            for(int i = 1; i <= m; i++)
            {
                scanf("%d", &car[j][i]);
                si[i] += car[j][i];
                sj[j] += car[j][i];//cout<<1;
            }
        }
        //for(int i = 1; i <= m; i++) cout<<si[i]<<" ";
        //for(int i = 1; i <= n; i++) cout<<sj[i]<<" ";
        sumx = sumy = 9223372036854775807;
        //处理x
        //第一重for loop控制x,第二层控制xi
        for(int x = 0; x <= 4*m; x += 4)
        {
            long long tmp = 0;
            for(int i = 1; i <= m; i++)
            {
                tmp = tmp + si[i]*(x - 4*i + 2)*(x - 4*i + 2);
            }
            if(tmp < sumx)
            {
                sumx = tmp;
                lj = x/4;
            }
        }
        //处理y
        //第一重for loop控制y,第二层控制yj
        for(int y = 0; y <= 4*n; y += 4)
        {
            long long tmp = 0;
            for(int j = 1; j <= n; j++)
            {
                tmp = tmp + sj[j]*(y - 4*j + 2)*(y - 4*j + 2);
                //cout<<""
            }
            if(tmp < sumy)
            {
                sumy = tmp;
                li = y/4;
            }
        }
        //cout<<sumx<<" "<<sumy<<endl;
        printf("%I64d\n%d %d\n", sumx + sumy, li, lj);
        return 0;
    }


  • 相关阅读:
    Spring Boot 自定义属性 以及 乱码问题
    IDEA 修改文件编码
    Gojs简单例子
    无法转换json问题 Error: Model.nodeDataArray value is not an instance of Array or NodeList or HTMLCollection
    java json转换
    git设置HTTP代理
    thymeleaf中的日期格式化
    thymeleaf:字符串Strings常见的使用方法
    thymeleaf+bootstrap,onclick传参实现模态框中遇到的错误
    Thymeleaf教程 (十二) 标签内,js中使用表达式
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910569.html
Copyright © 2011-2022 走看看