题目链接:【51nod】1302 矩形面积交
因为最后可以旋转,所以每一组的矩形一定是短边对短边,长边对长边。
考虑短边最短的一定在
A
组,然后我们将其余的矩形按照长边排序,枚举在
因为此时相当于固定了
A
组的短边(最小只可能是一开始孤立出来的矩形),
#include <bits/stdc++.h>
using namespace std ;
typedef long long LL ;
typedef pair < int , int > pii ;
typedef unsigned long long ULL ;
#define clr( a , x ) memset ( a , x , sizeof a )
const int MAXN = 200005 ;
struct Node {
int x , y ;
bool operator < ( const Node& a ) const {
return y != a.y ? y < a.y : x < a.x ;
}
} ;
Node a[MAXN] ;
int n ;
void solve () {
for ( int i = 1 ; i <= n + n ; ++ i ) {
scanf ( "%d%d" , &a[i].x , &a[i].y ) ;
if ( a[i].x > a[i].y ) swap ( a[i].x , a[i].y ) ;
if ( a[i].x < a[1].x || a[i].x == a[1].x && a[i].y < a[1].y ) swap ( a[i] , a[1] ) ;
}
sort ( a + 2 , a + n + n + 1 ) ;
LL ans = 0 ;
priority_queue < pii , vector < pii > , greater < pii > > q ;
for ( int i = n + n ; i >= 2 ; -- i ) {
q.push ( pii ( a[i].x , a[i].y ) ) ;
if ( q.size () > n ) {
a[1].y = min ( a[1].y , q.top ().second ) ;
q.pop () ;
}
if ( q.size () == n ) {
int tmp = a[1].y ;
if ( i > 2 ) tmp = min ( tmp , a[2].y ) ;
ans = max ( ans , 1LL * a[1].x * tmp + 1LL * a[i].y * q.top ().first ) ;
}
}
printf ( "%lld\n" , ans ) ;
}
int main () {
while ( ~scanf ( "%d" , &n ) ) solve () ;
return 0 ;
}