[Gdgzezoi] Problem A: Small ball staining

Description

N-bags, each bag filled with two white ball. i number of bag above two white balls were written xi, yi two numbers. Now stained to the ball, two balls in each bag to a dyed red and one blue. After staining the ball out of 2n, define:

  • Rmax is the maximum number of red balls
  • Rmin is the minimum number of balls on red
  • Bmax is the largest digital blue balls
  • The minimum number of balls on the blue Bmin

Seeking (Rmax-Rmin) (Bmax- Bmin) minimum.
Input
The first row is an integer n (1≤n≤2 × 105), followed by n lines of two numbers xi, yi (1≤xi, yi≤109) , it represents the two bags of the i the numbers on the balls.
Output
output line an integer representative of the minimum value (Rmax-Rmin) (Bmax- Bmin) of.
The Input the Sample
Sample INPUT. 1:
. 3
. 1 2
. 3. 4
. 5. 6

sample input 2:
3
1010 10
1000 1
20 1020

sample input 3:
2
1 1
1000000000 1000000000
Sample Output
sample output 1:
15

sample output 2:
380

. 3 Output Sample:
999999998000000001
the HINT
Sample 1 is the optimal solution x1, x2, y3 red y2, y2, x3 blue stained

Thinking

Consider the overall maximum and minimum values, while as a color or the maximum / minimum values, or are the maximum / minimum of two different colors. Which is relatively easy to handle, obviously should all point the larger of the value of the dyed the same color, smaller dyed another color.

If the global minimum and maximum values ​​are the same color more trouble. You may wish to set these two values ​​are dyed red. At this time, all other values ​​can be dyed red, not out of range. Therefore, the remaining problem into a point value for each tap dyed blue, so that maximum minus minimum blue minimized.

Code

#include<bits/stdc++.h>
#define N 220077
#define ll long long
using namespace std;
struct arr
{
    ll x,y;
}p[N];
ll n,Rmax,Rmin,Lmax,Lmin,r,l,Max,Min;
bool operator < (arr a,arr b)
{
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
ll solve1()
{
    Max=p[1].y,Min=p[1].x;r=1;l=1;
    for(int i=2;i<=n;i++)
    {
        if(p[i].x<Min)Min=p[i].x,l=i;
        if(p[i].y>Max)Max=p[i].y,r=i;
    }
    Rmax=Max;Lmin=Min;
    Rmin=p[l].y;Lmax=p[r].x;
    for(int i=1;i<=n;i++)
    {
        Rmin=min(Rmin,p[i].y);
        Lmax=max(Lmax,p[i].x);
    }
    return (ll)(Rmax-Rmin)*(Lmax-Lmin);
}
ll solve2(){
    Lmax=Max;
    Lmin=Min;
    Rmax=max(p[n].x,p[1].y);
    ll k=p[1].y;
    Rmin=min(p[1].y,p[2].x);
    ll ans=Rmax-Rmin;
    for(int i=2;i<=n-1;i++){
        k=min(k,p[i].y);
        Rmax=max(p[i].y,Rmax);
        Rmin=min(k,p[i+1].x);
        if(Rmax-Rmin<ans)ans=Rmax-Rmin;
    }
    return (ll)ans*(Lmax-Lmin);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&p[i].x,&p[i].y);
        if(p[i].x>p[i].y)swap(p[i].x,p[i].y);
    }
    sort(p+1,p+n+1);
    ll ans=solve1();
    if(r!=l)ans=min(ans,solve2());
    printf("%lld",ans);
    return 0;
}

Published 703 original articles · won praise 392 · Views 140,000 +

Guess you like

Origin blog.csdn.net/Eric1561759334/article/details/100808376