能推出一些性质。

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



如图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. C语言提高 (7) 第七天 回调函数 预处理函数DEBUG 动态链接库

    链表就是一个结构体 指针指向自身结构体类型 双向链表插入的时候 先改变自身 再改变两边 双向链表删除的时候 先改变两边 再改变自己 逆序一个单向链表 回调函数 指向函数的指针 4链表的遍历回调用法 / ...

  2. Linux学习一:图解CentOS 6.5安装步骤

    1 进入安装界面 2 选择语言 3 选择键盘 4 选择存储类型 5 是否格式化硬盘 6 设置主机名 7 配置网卡 (1)选择网卡并编辑 (2)配置IPv4 (3)查看虚拟网络编辑器 NAT设置 DHC ...

  3. leetCode笔记--binary tree

    993. Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each dept ...

  4. 使用Spring的MailSender发送邮件

    第1步:扫描邮件发送的属性配置 <context:property-placeholder location="/config/mail.properties" ignore ...

  5. linux_ubuntu 连接xftp

    一.修改静态ip 打开网络配置文件 :vim /etc/network/interfaces 1.添加以下配置: 注意:ubuntu 网卡名为 ens33 .多数liunx系统的网卡名为eth0. 可 ...

  6. CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)

    1.http://codeforces.com/problemset/problem/149/D 2.题目大意 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色, ...

  7. BIRT报表Cannot open the connection for the driver:org.eclipse.birt.report.data.oda.jdbc.dbprofile

    现象:报表不能打开: 找了半个小时,随手改了一下tomcat/conf文件夹下的context.xml文件 <Context xmlBlockExternal="false" ...

  8. HDU 4767

    昨晚苦恼了一晚,因为即将大三了,必须要准备实习什么的事了.一般都会去公司实习吧,但是看看自己的简历,实在拿不出手,因为大一大二都在搞ACM(虽然真正搞的只有大二一年),但却没有什么成绩,又不愿意做项目 ...

  9. JBoss vs. Tomcat

    JBoss 支持Servlet.Web Server和其它J2EE Features Servlet引擎使用Tomcat的内核 SSL性能比Tomcat快4倍(据说) 支持.net.PHP.CGI 支 ...

  10. 高校学生学籍系统C++&amp;mysql

    /* C++程序设计实践教学环节任务书 一.题目:高校学籍管理系统 二.目的与要求 1. 目的: 1.掌握C++语言基本知识及其编程方法  2.掌握类和对象的基本概念与用法 3.掌握面向对象中的继承与 ...