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 基准时间限 ...
随机推荐
- CentOS 笔记(二) 端口占用,进程查看
①查看当前端口情况 netstat -nultp ②查看当前进程情况 ps -ef ps -ef|grep dotnet ③强制kill 进程 kill -9 [PIN]
- SpringBoot2整合activiti6环境搭建
SpringBoot2整合activiti6环境搭建 依赖 <dependencies> <dependency> <groupId>org.springframe ...
- PHP学习总结(5)——PHP入门篇之PHP字符串
字符串 一个字符串是用双引号括起来的一个词或一个句字,比如:"Hi,imooc!".你可以用PHP语言输出把这个字符串输出,像这样: <?php echo "Hi, ...
- Ajax接收json响应
json? Json和xml比较 Ajax如何使用JSON Ajax接收json响应案例 什么是json?JSON (JavaScript Object Notation) 是一种轻量级的 ...
- java的classLoader原理理解和分析
java的classLoader原理理解和分析 学习了:http://blog.csdn.net/tangkund3218/article/details/50088249 ClassNotFound ...
- 杭电(hdu)ACM 4548 美素数
美素数 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submis ...
- codecombat之KithGard地牢19-37关代码分享
codecombat中国游戏网址:http://www.codecombat.cn/ 全部代码为javascript代码分享 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 19 ...
- 2015.05.11,外语,读书笔记-《Word Power Made Easy》 15 “如何谈论事情进展” SESSION 44
1. not the real McCoy simulate(['simjuleit] v. 假装,冒充,模仿,模拟)来自拉丁simulo,copy的意思.simulo本身派生自拉丁形容词simili ...
- mysqli一些常用方法及详解
mysqli一些常用方法及详解 1.die()函数:表示向用户输出引号中的内容后,程序终止运行,提示定制的出错信息 ex: $conn = mysqli_connect("localhost ...
- python matplot 绘图
import numpy as np import matplotlib.pyplot as plt plt.figure(1) # 创建图表1 plt.figure(2) # 创建图表2 ax1 = ...