Primary Mathematics - Noise

Face questions

【Problem Description】

Robin To rent an apartment to a cell, he found a cell (the n- the n-) matrix (with the n- the n-squares). District where there are two mower, mowing every weekend will produce a lot of noise. Mower mowing each to come to m (m <= n * n ) th place, the coordinates of the i-th place (X [i] (OK), Y [i] (column)), manufactured q [ i] point noise level, noise cell is very unique sound will spread sideways or vertically. Each spread a grid, the noise level will be increased by one. (Number of grid taken minimum propagation), the coordinates of the house is to be selected in the Robin matrix. But the noise made him a big headache. He wanted to find the smallest noise value place.

[Input Format]

The first input line n, m.

Next m lines of three positive integers x [i], y [i] and q [i].

[Output format]

The first line of a minimum integer value is how much noise.

The next line, and two positive integers zb1 ZB2, noise represents the minimum value of the coordinates of the place (if there are multiple answers, with minimal output abscissa, ordinate smallest).

Sample 1 [O]

3 3
1 1 1
1 2 1
1 3 1
5
1 2

【data range】

10% of the data, n <= 10 (a gift from a subject).

30% of the data, n <= 1000.

100%的数据,0<n<=100000,0<m<=100000,0<q[i]<=100.

Sample 1 [explain]

(1,1) initial noise value is 1, (2) an initial value of the noise, (3) an initial noise value is 1, (1,1) and from (1,2) to 1 (abs (1-1) + abs (1-2)), the propagation process is a noise increase. (2) from the (1,2) is 0, the noise propagation is 0, (1,3) and from (1,2) is 1, the communication process is increased noise 1. The total noise value of 1 + 1 + 1 + 1 + 0 + 1 = 5.

solution

When set up home in the answer made Robin \ ((x, y) \ ) at. The noise value
\ [\ sum_ = {I}. 1 mq_i ^ + | X-x_i | + | Y-y_i | \] . We can be isolated. \ (\ sum q \) is a constant, and if the \ (\ sum | x-x_i | \) and \ (\ sum | y-y_i | \) are taken, then the global minimum, the noise can be taken to a minimum value. Thus, as long as the \ (X = X _ {\ lfloor \ FRAC {m +. 1} {2} \ rfloor} \) ,
\ (Y = Y _ {\ lfloor \ FRAC {m +. 1} {2} \ rfloor} \) (so that \ (x_1 \ Le x_2 \ Le \ cdots x_m \) , \ (Y_1 \ Le y_2 \ Le \ cdots Y_M \) ).

When determined particular, we do not need all the \ (x_i) _ {I}. 1 ^ m = \) , \ ((y_i) _ {I}. 1 ^ m = \) are sorted, we can use the std::nth_element()linear time completed under complexity.

program

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
 
#define MAXN 100020
int n, m, xs[MAXN], ys[MAXN];
int xo[MAXN], yo[MAXN], qs[MAXN];
 
int main() {
    ios::sync_with_stdio(false);
     
    cin >> n >> m;
    for (int i = 0; i < m; i++)
        cin >> xo[i] >> yo[i] >> qs[i];
    for (int i = 0; i < m; i++) {
        xs[i] = xo[i];
        ys[i] = yo[i];
    }
    int mid = m / 2 - (m % 2 ? 0 : 1);
    nth_element(xs, xs + mid, xs + m);
    nth_element(ys, ys + mid, ys + m);
    long long ans = 0;
    int ansx = xs[mid], ansy = ys[mid];
    for (int i = 0; i < m; i++)
        ans += qs[i] + abs(ansx - xo[i]) + abs(ansy - yo[i]);
    cout << ans << endl << ansx << " " << ansy << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/lrw04/p/12114576.html
Recommended