51nod 1302(贪心+平衡树)
能推出一些性质。
矩形肯定是全部躺着或全部立着比较优。

如图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(贪心+平衡树)的更多相关文章
- 51nod 1163贪心
用优先队列来贪心,是一个很好地想法.优先队列在很多时候可以维护最值,同时可以考虑到一些其他情况. http://www.51nod.com/onlineJudge/questionCode.html# ...
- 51nod 1625 贪心/思维
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 1625 夹克爷发红包 基准时间限制:1 秒 空间限制:13107 ...
- 51nod 1099 贪心/思维
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1099 1099 任务执行顺序 基准时间限制:1 秒 空间限制:13107 ...
- 51nod 1428 贪心
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活动安排问题 基准时间限制:1 秒 空间限制:13107 ...
- SCUT - 131 - 小P玩游戏II - 贪心 - 平衡树
https://scut.online/p/131 首先假如钦定了一群人去打怪兽,那么可以把主要的任务都丢给b最大的人去打,这样不会更差.然后考虑枚举这个b最大的人,其他人陪练.一开始就是ai+k*b ...
- Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) E - Aquarium decoration 贪心 + 平衡树
E - Aquarium decoration 枚举两个人都喜欢的个数,就能得到单个喜欢的个数,然后用平衡树维护前k大的和. #include<bits/stdc++.h> #define ...
- 51nod 1672 贪心/队列
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672 1672 区间交 基准时间限制:1 秒 空间限制:131072 K ...
- 51nod 1449 贪心
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1449 1449 砝码称重 题目来源: CodeForces 基准时间限制 ...
- 51nod 1255 贪心/构造
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1255 1255 字典序最小的子序列 题目来源: 天津大学OJ 基准时间限 ...
随机推荐
- LOJ——#2256. 「SNOI2017」英雄联盟
https://loj.ac/problem/2256 题目描述 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不了网友们的嘲讽,决定变强了,他变强 ...
- spring boot系列--spring security (基于数据库)登录和权限控制
先说一下AuthConfig.java Spring Security的主要配置文件之一 AuthConfig 1 @Configuration 2 @EnableWebSecurity 3 publ ...
- 漫说好管理vs.坏管理
天地会珠海分舵注:本文英文版来自Medium今日热点头条.漫画简单明了,全文差点儿没有多余的语言去装饰.两天内获得两千三百多个推荐,且读者的反馈也相当的热烈.中文版由天地会珠海分舵编译后分享给大家. ...
- Java 嵌套类和内部类演示样例<三>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-s ...
- Python标准库:内置函数callable(object)
假设对象object參数是能够调用的对象,就返回True.否则返回False.只是要注意的是,当一个对象是能够调用的.并不表示调用该对象时运行一定成功,但不可调用的对象去调用时一定不会成功.假设类对象 ...
- Android菜鸟笔记- 获取未安装的APK图标、版本号、包名、名称、是否安装、安装、打开
周末闲来无事,把Android的基础知识拿出来复习复习,今天主题是<获取未安装的APK图标.版本号.包名.名称.是否安装.跳转安装.打开> 一.获取APK图标 通常读取APK的图标能够用, ...
- 石子合并(区间dp)
石子合并(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程仅仅能每次将相邻 ...
- insmod: error inserting 'hello.ko': -1 Invalid module format
在学习编写linux驱动程序的时候,一般都是从写一个helloworld的模块開始. 可是在编译完毕后,进行模块载入的时候,有时会出现例如以下错误: insmod: error inserting ' ...
- elasticsearch源码分析之search模块(client端)
elasticsearch源码分析之search模块(client端) 注意,我这里所说的都是通过rest api来做的搜索,所以对于接收到请求的节点,我姑且将之称之为client端,其主要的功能我们 ...
- spark Bisecting k-means(二分K均值算法)
Bisecting k-means(二分K均值算法) 二分k均值(bisecting k-means)是一种层次聚类方法,算法的主要思想是:首先将所有点作为一个簇,然后将该簇一分为二.之后选择能最大程 ...