能推出一些性质。

矩形肯定是全部躺着或全部立着比较优。



如图x1显然等于x2,y1显然小于y2。

所以我们就让它们都躺下吧。

然后一定有一组的宽为宽最小的矩形的宽。

然后我们枚举另一组的宽最小的矩形。(当然宽在最小的矩形和枚举的矩形之间的矩形都跟宽最小的矩形一组)

之后就只剩下长的影响了。

假设现在还剩下这些长度(排好序):

1 2 3 4 5 6 7 8 9

当然是把一段的矩形分在一组比较优。

那么分在哪一组呢?都试一下不就行了。

用平衡树维护k小。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<ctime>
#include<cstdlib>
using namespace std;
const int N=201000;
struct node{
int x,y;
}c[N];
bool cmp(node a,node b){
return a.x<b.x;
}
int tot,rad[N],size[N],val[N],ch[N][2],root,x,y,z;
int new_node(int x){
int now=++tot;
rad[now]=rand();
size[now]=1;
val[now]=x;
return now;
}
void update(int now){
size[now]=size[ch[now][0]]+size[ch[now][1]]+1;
}
int merge(int x,int y){
if(!x||!y)return x+y;
if(rad[x]>rad[y]){
ch[x][1]=merge(ch[x][1],y);
update(x);
return x;
}
else{
ch[y][0]=merge(x,ch[y][0]);
update(y);
return y;
}
}
void split(int &x,int &y,int now,int k){
if(now==0)x=y=0;
else{
if(val[now]<=k){
x=now;
split(ch[x][1],y,ch[x][1],k);
}
else{
y=now;
split(x,ch[y][0],ch[y][0],k);
}
update(now);
}
}
int kth(int now,int k){
int l=ch[now][0];
if(size[l]>=k)return kth(l,k);
else if(size[l]+1==k)return val[now];
else return kth(ch[now][1],k-size[l]-1);
}
int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
return sum*f;
}
int n;
long long ans;
int main(){
srand(time(NULL));
n=read();
for(int i=1;i<=n*2;i++){
c[i].x=read(),c[i].y=read();
if(c[i].x>c[i].y)swap(c[i].x,c[i].y);
}
sort(c+1,c+1+n*2,cmp);
for(int i=3;i<=n*2;i++){
split(x,y,root,c[i].y);
root=merge(x,merge(new_node(c[i].y),y));
}
for(int i=2;i<=n;i++){
int mn=kth(root,1);
int Mn=kth(root,n-(i-1)+1);
ans=max(ans,1ll*c[1].x*1ll*min(1ll*mn,1ll*c[1].y)+1ll*c[i].x*1ll*min(1ll*Mn,1ll*c[i].y)); Mn=kth(root,n);
ans=max(ans,1ll*c[i].x*1ll*min(1ll*c[i].y,1ll*mn)+1ll*c[1].x*1ll*min(1ll*c[1].y,1ll*Mn)); split(x,z,root,c[i+1].y);
split(x,y,x,c[i+1].y-1);
y=merge(ch[y][0],ch[y][1]);
root=merge(merge(x,y),z);
c[1].y=min(c[1].y,c[i].y);
}
printf("%lld",max(ans,1ll*c[1].x*c[1].y+1ll*c[n+1].x*min(1ll*c[n+1].y,1ll*kth(root,1))));
return 0;
}

51nod 1302(贪心+平衡树)的更多相关文章

  1. 51nod 1163贪心

    用优先队列来贪心,是一个很好地想法.优先队列在很多时候可以维护最值,同时可以考虑到一些其他情况. http://www.51nod.com/onlineJudge/questionCode.html# ...

  2. 51nod 1625 贪心/思维

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 1625 夹克爷发红包 基准时间限制:1 秒 空间限制:13107 ...

  3. 51nod 1099 贪心/思维

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1099 1099 任务执行顺序 基准时间限制:1 秒 空间限制:13107 ...

  4. 51nod 1428 贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活动安排问题 基准时间限制:1 秒 空间限制:13107 ...

  5. SCUT - 131 - 小P玩游戏II - 贪心 - 平衡树

    https://scut.online/p/131 首先假如钦定了一群人去打怪兽,那么可以把主要的任务都丢给b最大的人去打,这样不会更差.然后考虑枚举这个b最大的人,其他人陪练.一开始就是ai+k*b ...

  6. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) E - Aquarium decoration 贪心 + 平衡树

    E - Aquarium decoration 枚举两个人都喜欢的个数,就能得到单个喜欢的个数,然后用平衡树维护前k大的和. #include<bits/stdc++.h> #define ...

  7. 51nod 1672 贪心/队列

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 1672 区间交 基准时间限制:1 秒 空间限制:131072 K ...

  8. 51nod 1449 贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1449 1449 砝码称重 题目来源: CodeForces 基准时间限制 ...

  9. 51nod 1255 贪心/构造

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1255 1255 字典序最小的子序列 题目来源: 天津大学OJ 基准时间限 ...

随机推荐

  1. 用div布局,页面copyright部分始终居于

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...

  2. luogu 4240 毒瘤之神的考验 (莫比乌斯反演)

    题目大意:略 题面传送门 果然是一道神duliu题= = 出题人的题解传送门 出题人的题解还是讲得很明白的 1.关于$\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m ...

  3. linux双网卡配置

    一.VM虚拟机添加一个网络适配器. 选择自己需要的模式类型 二.启动虚拟机,配置网卡 按原先配置网卡的方式配置完(ip地址及默认网关还有网卡名不能跟原先的一样) 重启所有网卡(service netw ...

  4. iptables防火墙和selinux

    iptables 存在以下两种方式: 一.service方式 查看防火墙状态: [root@centos6 ~]# service iptables status iptables:未运行防火墙 开启 ...

  5. TensorFlow实现LeNet5模型

    # -*- coding: utf-8 -*-import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_ ...

  6. OA项目知识总结2

    BaseAction的抽取 项目中的每个实体类都对应一个action  每个action都都要继承ActionSupport类 已以及实现ModelDriver接口  并且需要注入service 虽然 ...

  7. nodejs-路由(待补充)

    path Router 1 2 3 4 5 var express = require('express'); var Router = express.Router(); Router.get('/ ...

  8. 浅析PHP中cookie与session技术

    1.cookie是什么? cookie指某些站点为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密). 通俗来理解就是,你去一个专卖店或者超市买东西,然后店里给你办一张 ...

  9. java 正则表达式(一)

    1匹配验证-验证Email是否正确 Java | 复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static void main(String[] args)  ...

  10. eclips 手机 offline

    [退出eclipse] [进入eclipse的工作区间] [删除 .metadata文件夹,确保 .metadata文件夹得隐藏文件也要删除] [重启eclipse,并重新导入你的工程]