A ninth field correction cattle off more computational geometry J Symmetrical Painting / scan line

Meaning of the questions:

There are several on a plane the same width of the rectangular area is blackened, so you find a number of rectangular horizontal cross section, the portion of the black portions such that they pull down this horizontal axis of symmetry, can pull down seek The maximum area.

answer:

As in the process of shifting the axis of symmetry, there must be an increase in the effective area of ​​the rectangular portion, a part in reducing the effective area, the effective area of ​​the same part.

When a single rectangular state changes, only when the axis of symmetry of the touch end, the midpoint, the upper end.

Thus preprocessing information and the rectangular discretization all three point mutations, then traverse from the bottom up, recording each time point three rectangular total number of states, this recursion area.

The optimal solution at a certain point mutation occurs, it can record.

To avoid floating point arithmetic, the height multiplied by two stores.

#include<iostream>
#include<algorithm>
#define LL long long 
using namespace std;
struct Node{
    int stat;
    LL height;
    friend bool operator <(const Node &a,const Node &b){
        return a.height<b.height;
    }
    Node(){}
    Node(int x,LL y){
        stat=x;height=y;
    }
}node[900005];
int main(){
    int n;
    scanf("%d",&n);
    int cnt=0;
    node[0]=Node(0,0);
    for(int i=1;i<=n;i++){
        LL l,r;
        scanf("%lld %lld",&l,&r);
        node[++cnt]=Node(-1,l*2);
        node[++cnt]=Node(0,l+r);
        node[++cnt]=Node(1,r*2);
    }
    sort(node+1,node+1+cnt);
    int up=0,down=0;
    LL now=0,maxx=0;
    for(int i=1;i<=cnt;i++){
        now+=(node[i].height-node[i-1].height)*(up-down);
        maxx=max(now,maxx);
        if(node[i].stat==-1)up++;
        if(node[i].stat==0)up--,down++;
        if(node[i].stat==1)down--;
    }
    printf("%lld\n",maxx);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/isakovsky/p/11366676.html