【51nod】1302 矩形面积交【优先队列贪心】

题目链接:【51nod】1302 矩形面积交

因为最后可以旋转,所以每一组的矩形一定是短边对短边,长边对长边。
考虑短边最短的一定在 A 组,然后我们将其余的矩形按照长边排序,枚举在B组的最短长边,逐个将矩形插入优先队列,然后优先队列中个数大于 N 时,取出短边最短的加入A,然后同时维护两组矩形的面积即可。
因为此时相当于固定了 A 组的短边(最小只可能是一开始孤立出来的矩形),A组的长边以及 B <script type="math/tex" id="MathJax-Element-37">B</script>组的长边。

#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 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值