Reverse - 2019.11.8 wave

Face questions

【Problem Description】

In the first quadrant, there have from time to time the waves on a beach. With a wave of a digital \ ((x, y) \ ) represents, represents a vertex \ ((0,0) \) , \ ((X, 0) \) , \ ((0, Y) \) , \ ((X, Y) \) rectangle.

Each wave will wash away traces of other waves within its range, and maintain their mark \ ((x, 0) \ to (x, y) \) and \ ((0, y) \ to (x , the y-) \) .

Now people on the coast want to know \ (n \) traces the total length of Bo Houhai shore. Ensure that the input data does not completely cover the other one wave wave.

[Input file]

The first line is the wave number \ (n-\) ( \ (n-\ 50000 Le \) ).

The following is a \ (n-\) rows, each row comprising two numbers \ ((X, Y) \) , ( \ (0 <X, Y \ 10000000 Le \) ) represent each wave.

[Output file]

Single line output the answer.

[Sample input]

3
1 4
4 1
3 3

[Sample Output]

10

Sample icon

solution

Direction is calculated by subject, there aftereffect of subsequent waves. Reverse calculation, each time to process the wave, it obliterated the traces of the waves are determined. Difficult to find, each time adding a new wave, which is partially occluded is his \ ((x, y) \ ) in, \ (X \) in \ (\ {x \} \ ) precursor of and \ (Y \) in \ (\ {y \} \ ) precursor is. However, if a \ (x \) than the previous all \ (x \) are small, the precursor should be zero. 0 elements must be inserted prior to the overall operation.

If std::vectorstorage has added wave, time consuming insertion. It can be used std::setto organize coordinates.

program

#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
 
#define MAXN 50050
int n, x[MAXN], y[MAXN];
set<int> xs, ys;
 
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
     
    xs.insert(0); ys.insert(0);
    int ans = 0;
    for (int i = n - 1; i > -1; i--) {
        set<int>::iterator ix = xs.lower_bound(x[i]);
        set<int>::iterator iy = ys.lower_bound(y[i]);
        ix--; iy--;
        ans += x[i] - (*ix) + y[i] - (*iy);
        xs.insert(x[i]);
        ys.insert(y[i]);
    }
     
    cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/lrw04/p/11826387.html