Cattle off more school training camp ninth field J - Symmetrical Painting (sorting)

J - Symmetrical Painting

The meaning of problems

You \ (n-\) rectangles, bottom left \ ((. 1-I, \ L_i) \) , the top right \ ((I, \ R_i) \) , to find a line \ (L \) parallel to \ ( x \) axis, so that these rectangular according to \ (l \) symmetric, asymmetric section deleted, ask the biggest symmetrical pattern area is how much?

Thinking

The main axis of symmetry it is to enumerate, from small to large enumeration \ (L_i, (L_i R_i +) / 2, R_i \) , the position of the three axis of symmetry.
It's time to enumerate how to maintain the rectangular area.

  • First, can \ (L_i * 2, * 2 R_i \) , so that an error does not occur taking the middle.
  • Then three positions labeled as \ (1: L_i, \ \ \ 2: (L_i + R_i) / 2, \ \ \ 3: R_i \)
  • According to the position of small to large order
  • With \ (A and B \) tag to add or subtract, \ (ANS \) : the current symmetry area
    1, if the hit flag \ (1, A ++ \)
    2, if the hit flag \ (2, a-- , B ++ \)
    . 3, if the hit flag \ (. 3, B - \)
    \ (ANS = + ANS (W_i -. 1-W_ {I}) * (ab &) * 2 \) , below only for a rectangular analysis
    • \ (L_i \ Le W_i \ Le (L_i R_i +) / 2 \) : Symmetric larger area, \ (ANS = + ANS (W_i -. 1-W_ {I}) * 2 \)
    • \ ((L_i + R_i) / 2 \ Le W_i \ Le R_i \) : Symmetric area becomes small, \ (ANS = ANS - (W_i - W_ {I-. 1}) * 2 \) , since prior to the addition over \ ( (L_i R_i +) / 2 \) ~ \ (L_i \) area, the area is now needed (W_i \) \ ~ \ (R_i \) area, then subtracting the foregoing \ ((w_i - w_ { i-1}) * 2 \ )
    • \ (R_i \ Le W_i \) L: the area equal to \ (0 \) , then \ (b - \) on it.
  • The final answer to remember \ (Max / 2 \)
    game when I wanted to enumerate the axis of symmetry of the class, but always wanted to do not understand how to maintain, and later saw some of the chiefs of the code, simple and ingenious. . . Anyway, I have no idea. . keep working hard.

    Code

#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  lowbit(x)  x & (-x)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi         first
#define  se         second
#define  pb         push_back
#define  pii        pair<int, int>

typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 3e5 + 10;
const int    maxm = 1e5 + 10;
const ll     mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;

int n, m;
int cas, tol, T;
struct Node{
    ll w;
    int id;
    bool operator < (Node const & a){
        return w < a.w;
    }
}node[maxn*4];

int main() {
    scanf("%d", &n);
    ll l, r;
    int cnt = 0;
    for(int i = 1; i <= n; i++){
        scanf("%lld%lld", &l, &r);
        l *= 2;
        r *= 2;
        node[++cnt] = {l, 1};
        node[++cnt] = {(l+r)/2, 2};
        node[++cnt] = {r, 3};
    }
    sort(node+1, node+cnt+1);
    ll a = 0, b = 0, ans = 0, Max = 0;
    for(int i = 1; i <= cnt; i++){
        ans += (node[i].w - node[i-1].w)*(a-b)*2;       \\也可以这边不乘,最后就不用除了
        if(node[i].id == 1)
            a++;
        if(node[i].id == 2){
            a--;b++;
        }
        if(node[i].id == 3)
            b--;
        Max = max(Max, ans);
    }
    printf("%lld\n", Max/2);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zhuyou/p/11365730.html