负载平衡Load Balancing_Silver

题目描述
Farmer John’s NN cows are each standing at distinct locations (x_1, y_1) \ldots (x_n, y_n)(x
1
​ ,y
1
​ )…(x
n
​ ,y
n
​ ) on his two-dimensional farm (1 \leq N \leq 10001≤N≤1000, and the x_ix
i
​ 's and y_iy
i
​ 's are positive odd integers of size at most 1,000,0001,000,000). FJ wants to partition his field by building a long (effectively infinite-length) north-south fence with equation x=ax=a (aa will be an even integer, thus ensuring that he does not build the fence through the position of any cow). He also wants to build a long (effectively infinite-length) east-west fence with equation y=by=b, where bb is an even integer. These two fences cross at the point (a,b)(a,b), and together they partition his field into four regions.

FJ wants to choose aa and bb so that the cows appearing in the four resulting regions are reasonably “balanced”, with no region containing too many cows. Letting MM be the maximum number of cows appearing in one of the four regions, FJ wants to make MM as small as possible. Please help him determine this

smallest possible value for MM.

给你一个矩阵,里面有些点,让你横向切一刀,纵向切一刀,使得得到的四个区域内的最大的点数最少。

输入格式
The first line of the input contains a single integer, NN. The next NN lines

each contain the location of a single cow, specifying its xx and yy

coordinates.

输出格式
You should output the smallest possible value of MM that FJ can achieve by

positioning his fences optimally.

输入输出样例
输入 #1 复制
7
7 3
5 5
7 13
3 1
11 7
5 3
9 1
输出 #1 复制
2

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

void chmin(int &x,int y){
    if(x>y)x=y;
}

const int N=40000005;
int n,i,u;
int px[N],py[N];
int id[N];

bool hhh(int x,int y){
   return py[x]<py[y];
}

bool kkk(int x,int y){
   return px[x]<px[y];
}

int lisan_py(){
    for(i=1;i<=n;++i){
		id[i]=i;
	}
    sort(id+1,id+n+1,hhh);
    int now=0,top=0;
    for(i=1;i<=n;++i){
		int x=id[i];
		if(py[x]!=now){
			now=py[x];
			++top;
		}
		py[x]=top;
    }
    return top;
}

#define cl (i<<1)
#define cr (cl+1)

int al[N*4],ar[N*4],d;

void init(int *a){
    for(i=1;i<=n;++i){
		++a[d+py[i]];
	}
    for(i=u+d>>1;i;--i){
		a[i]=a[cl]+a[cr];
	}
}

void add(int *a,int i,int w){
    for(i+=d;i;i>>=1){
		a[i]+=w;
	}
}

int erfen(){
    i=1;
    int all=0,alr=0,arl=0,arr=0;
    while(i<=d){
        int mxl=max(all+al[cl],arl+ar[cl]),mxr=max(alr+al[cr],arr+ar[cr]);
        if(mxl<=mxr){
			all+=al[cl];
			arl+=ar[cl];
			i=cr;
		}
        else{
			alr+=al[cr];
			arr+=ar[cr];
			i=cl;
		}
    }
    int mxl=max(max(all+al[i],arl+ar[i]),max(all,arr)),
    mxr=max(max(alr+al[i],arr+al[i]),max(all,arl));
    return min(mxl,mxr); 
}


int main(){
    int i;
    scanf("%d",&n);
    for(i=1;i<=n;++i){
		scanf("%d%d",px+i,py+i);
	}
    u=lisan_py();
    for(d=1;d<u;d<<=1);d-=1;
    init(ar);
    sort(id+1,id+n+1,kkk);
    int ans=n;
    for(i=1;i<=n;++i){
        int x=id[i];
        add(ar,py[x],-1);
        add(al,py[x],1);
        chmin(ans,erfen());
    }
    printf("%d\n",ans);
}

基于python实现的粒子群的VRP(车辆配送路径规划)问题建模求解+源码+项目文档+算法解析,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 算法设计的关键在于如何向表现较好的个体学习,标准粒子群算法引入惯性因子w、自我认知因子c1、社会认知因子c2分别作为自身、当代最优解和历史最优解的权重,指导粒子速度和位置的更新,这在求解函数极值问题时比较容易实现,而在VRP问题上,速度位置的更新则难以直接采用加权的方式进行,一个常见的方法是采用基于遗传算法交叉算子的混合型粒子群算法进行求解,这里采用顺序交叉算子,对惯性因子w、自我认知因子c1、社会认知因子c2则以w/(w+c1+c2),c1/(w+c1+c2),c2/(w+c1+c2)的概率接受粒子本身、当前最优解、全局最优解交叉的父代之一(即按概率选择其中一个作为父代,不加权)。 算法设计的关键在于如何向表现较好的个体学习,标准粒子群算法引入惯性因子w、自我认知因子c1、社会认知因子c2分别作为自身、当代最优解和历史最优解的权重,指导粒子速度和位置的更新,这在求解函数极值问题时比较容易实现,而在VRP问题上,速度位置的更新则难以直接采用加权的方式进行,一个常见的方法是采用基于遗传算法交叉算子的混合型粒子群算法进行求解,这里采用顺序交叉算子,对惯性因子w、自我认知因子c1、社会认知因子c2则以w/(w+c1+c2),c1/(w+c1+c2),c2/(w+c1+c2)的概率接受粒子本身、当前最优解、全局最优解交叉的父代之一(即按概率选择其中一个作为父代,不加权)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值