BZOJ4411 - [Usaco2016 Feb]Load balancing

2024-03-19 17:58

本文主要是介绍BZOJ4411 - [Usaco2016 Feb]Load balancing,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Portal

Description

给出平面上的\(n(n\leq10^5)\)个整点。画两条直线\(x=x_0\)\(y=y_0\)将这些点划分成\(s_1,s_2,s_3,s_4\)个点,最小化\(max\{s_1,s_2,s_3,s_4\}\)

Solution

二分答案+线段树。
首先进行离散化,记录\(sumY[i]\)表示\(y\leq i\)的点的个数。
检查\(m\)是否合法时,枚举\(x_0\)并查找是否存在合法的\(y_0\)。将↖称为\(s_1\),↙称为\(s_2\),↗称为\(s_3\),↘称为\(s_4\)。那么我们找到使得\(s_1,s_2\leq m\)的最大的\(y_0\),然后检查\(s_3,s_4\)是否不超过\(m\)。我们将\(x\leq x_0\)的点加入线段树,在线段树上二分来找到最大的\(y_0\)使得线段树上\([1,y_0]\)中的点数和(即\(s_1\))不超过\(m\)\(s_2=sumY[y_0]-s_1\)也不超过\(m\)。找到\(y_0\)后检查\(s_3=sum[rt]-s_1,s_4=n-s_1-s_2-s_3\)是否均不超过\(m\),若均合法则说明\(ans\leq m\),否则继续枚举\(x_0\)。若对于所有\(x_0\)均不存在合法的\(y_0\),则说明\(ans>m\)

时间复杂度\(O(nlog^2n)\)

Code

//[Usaco2016 Feb]Load balancing
#include <algorithm>
#include <cstdio>
using namespace std;
inline char gc()
{static char now[1<<16],*s,*t;if(s==t) {t=(s=now)+fread(now,1,1<<16,stdin); if(s==t) return EOF;}return *s++;
}
inline int read()
{int x=0; char ch=gc();while(ch<'0'||'9'<ch) ch=gc();while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();return x;
}
const int N=1e5+10;
int n,nX,nY; int sumY[N];
struct point{int x,y;} pt[N];
bool cmpX(point x,point y) {return x.x<y.x;}
int mapX[N],mapY[N];
void discrete()
{for(int i=1;i<=n;i++) mapX[i]=pt[i].x,mapY[i]=pt[i].y;sort(mapX+1,mapX+n+1); nX=unique(mapX+1,mapX+n+1)-mapX-1;for(int i=1;i<=n;i++) pt[i].x=lower_bound(mapX+1,mapX+nX+1,pt[i].x)-mapX;sort(mapY+1,mapY+n+1); nY=unique(mapY+1,mapY+n+1)-mapY-1;for(int i=1;i<=n;i++) pt[i].y=lower_bound(mapY+1,mapY+nY+1,pt[i].y)-mapY;sort(pt+1,pt+n+1,cmpX);
}
const int Ns=4e5+10;
#define Ls (p<<1)
#define Rs (p<<1|1)
int rt,sum[Ns]; int clr[Ns];
int optL,optR;
void update(int p) {sum[p]=sum[Ls]+sum[Rs];}
void pushdw(int p) {if(clr[p]) sum[Ls]=sum[Rs]=0,clr[Ls]=clr[Rs]=true,clr[p]=false;}
void ins(int p,int L0,int R0,int x,int v)
{if(L0==R0) {sum[p]+=v; return;}pushdw(p);int mid=L0+R0>>1;if(x<=mid) ins(Ls,L0,mid,x,v);else ins(Rs,mid+1,R0,x,v);update(p);
}
int query1(int p,int L0,int R0,int s1,int x)
{if(L0==R0) return L0;pushdw(p);int mid=L0+R0>>1; int t1=s1+sum[Ls],t2=sumY[mid]-t1;if(x<t1||x<t2) return query1(Ls,L0,mid,s1,x);else return query1(Rs,mid+1,R0,t1,x);
}
int query2(int p,int L0,int R0)
{if(optL<=L0&&R0<=optR) return sum[p];pushdw(p);int mid=L0+R0>>1; int res=0;if(optL<=mid) res+=query2(Ls,L0,mid);if(mid<optR) res+=query2(Rs,mid+1,R0);return res;
}
bool check(int m)
{rt=1; sum[rt]=0,clr[rt]=true;for(int i=1,t=1;i<=nX;i++){while(pt[t].x==i) ins(rt,1,nY,pt[t].y,1),t++;if(sum[rt]>m+m) break;else if(n-sum[rt]>m+m) continue;int j=query1(rt,1,nY,0,m)-1;optL=1,optR=j; int s1=query2(rt,1,nY);int s2=sumY[j]-s1,s3=sum[rt]-s1,s4=n-s1-s2-s3;if(s1>m||s2>m) printf("query1 GG\n");if(s3<=m&&s4<=m) return true; }return false;
}
int main()
{n=read();for(int i=1;i<=n;i++) pt[i].x=read(),pt[i].y=read();discrete();for(int i=1;i<=n;i++) sumY[pt[i].y]++;for(int i=1;i<=nY;i++) sumY[i]+=sumY[i-1];int L=n/4-1,R=n;while(L<=R){int mid=L+R>>1;if(check(mid)) R=mid-1;else L=mid+1;}printf("%d\n",L);return 0;
}

P.S.

sro Icefox的\(O(nlogn)\)做法 orz,比我强多啦

转载于:https://www.cnblogs.com/VisJiao/p/BZOJ4411.html

这篇关于BZOJ4411 - [Usaco2016 Feb]Load balancing的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/826813

相关文章

类的load方法和initialize方法对比

1. load方法在main()之前被调用,而initialize方法在main()之后调用 load方法实际是在load_images过程中被调用的。load_images会将当前应用依赖的所有镜像(动态库)加载到内存,在在加载中首先是对镜像进行扫描,将所有包含 load 方法的类加入列表 loadable_classes ,然后从这个列表中逐一调用其所包含的 load 方法。 +[XXCl

Unity Adressables 使用说明(六)加载(Load) Addressable Assets

【概述】Load Addressable Assets Addressables类提供了加载 Addressable assets 的方法。你可以一次加载一个资源或批量加载资源。为了识别要加载的资源,你需要向加载方法传递一个键或键列表。键可以是以下对象之一: Address:包含你分配给资源的地址的字符串。Label:包含分配给一个或多个资源的标签的字符串。AssetReference Obj

How can I load the openai api configuration through js in html?

题意:怎样在HTML中通过JavaScript加载OpenAI API配置 问题背景: I am trying to send a request through js in my html so that openai analyzes it and sends a response, but if in the js I put the following: 我正在尝试通过HTM

JavaBug系列- Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class load

JavaBug系列之Mysql驱动问题 Java医生一、关于错误信息二、如何解决问题 Java医生 本系列记录常见Bug,以及诊断过程和原因 Java/一对一零基础辅导/企业项目一对一辅导/日常Bug解决/代码讲解/毕业设计等 V:study_51ctofx 一、关于错误信息 APPLICATION FAILED TO START Description: Fai

【Python百日进阶-Web开发-音频】Day705 - 音频加载 librosa.load / librosa.stream

文章目录 一、音频加载1.1 librosa.load1.1.1 语法与参数1.1.2 例子1.1.2.1 下载并加载文件1.1.2.2 加载并重采样1.1.2.3 加载文件,从第15秒开始,加载5秒- 1.2 librosa.stream1.2.1 语法与参数1.2.2 例子1.2.2.1 一次对 256 帧的块应用短期傅里叶变换。1.2.2.2 使用较短的帧和不重叠的窗口计算流上的 m

【异常】java.sql.SQLException: Unable to load authentication plugin ‘caching_sha2_password‘.

异常现象 执行mysql数据库操作的时候,出现以下异常信息: java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:868) ~[mysql-connector-

$(document).ready()与$(window).load()的区别

1.执行时间不同: 从字面的意思上理解,$(document).ready()就是文档准备好了。也就是浏览器已经解析完整个html文档,dom树已经建立起来了,这时就可以通过class属性或者id属性等等对dom进行操作等。而$(window).load()就是整个页面已经加载完毕。与前者的区别是dom树虽然已经建立起来了,但页面不一定加载完毕,如一些大的图片等,加载完成就需要一定的时间;但是页

解决Can‘t load tokenizer for ‘bert-base-chinese‘.问题

报错提示: OSError: Can't load tokenizer for 'bert-base-chinese'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwi

POL(Point-of-Load)负载点电源

负载点(POL)电源在靠近负载处单独放置电源调节器(线性稳压器或DC-DC),解决了高性能半导体器件,例如:微控制器、ASIC等,所面临的高峰值电流、低噪声裕量等设计挑战。 一般我们会把负载点电源尽量靠近负载放置, 这么做可以最大限度地确保供电效率和准确性。 图 1 常见POL电源的拓扑结构 Typical设计POL设计

七. 部署YOLOv8检测器-load-save-tensor

目录 前言0. 简述1. 案例运行2. 补充说明3. 代码分析3.1 main.cpp3.2 create_data.py 结语下载链接参考 前言 自动驾驶之心推出的 《CUDA与TensorRT部署实战课程》,链接。记录下个人学习笔记,仅供自己参考 本次课程我们来学习课程第六章—部署分类器,一起来学习利用 cnpy 库加载和保存 tensor 课程大纲可以看下面的思维导图