记录编号 346995 评测结果 AAAAAAAAAAAAAAA
题目名称 [USACO Feb15]负载平衡(白金组) 最终得分 100
用户昵称 GravatarFoolMike 是否通过 通过
代码语言 C++ 运行时间 4.989 s
提交时间 2016-11-12 18:29:51 内存使用 15.63 MiB
显示代码纯文本
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1000010,SIZE=1e6;
int n,ans,q[N],cnt,sl,sr;
pair<int,int> p[N];
struct bit{
	int a[N];
	int sum(int r){
		int ans=0;
		for (;r;r-=(r&-r)) ans+=a[r];
		return ans;
	}
	void add(int p,int d){
		for (;p<=SIZE;p+=(p&-p)) a[p]+=d;
	}
}TL,TR;
int f1(int x){return sl-2*TL.sum(x);}
int f2(int x){return sr-2*TR.sum(x);}
int f3(int x){return sl-TL.sum(x)-TR.sum(x);}
int f4(int x){return sr-TL.sum(x)-TR.sum(x);}
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b,int c,int d){return max(max(a,b),max(c,d));}
int min(int a,int b,int c,int d){return min(min(a,b),min(c,d));}
int F(int x){
	if (x<0||x>1e6) return max(sl,sr);
	int nl=TL.sum(x),nr=TR.sum(x);
	return max(nl,sl-nl,nr,sr-nr);
}
int erfen(int(*f)(int)){//求该函数零点(近似) 
	int l=0,r=1e6;
	while (l!=r){
		int mid=(l+r)>>1;
		if (f(mid)>0) l=mid+1;else r=mid;
	}
	int x=l,ans=min(F(x-1),F(x-2));
	return min(ans,F(x),F(x+1),F(x+2));
}
void solve(){
	int x=min(erfen(f1),erfen(f2),erfen(f3),erfen(f4));
	ans=min(ans,x);
}
int main()
{
	freopen("Load_Balancing.in","r",stdin);
	freopen("Load_Balancing.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d%d",&p[i].first,&p[i].second);
		sr++;TR.add(p[i].second,1);
	}
	sort(p+1,p+n+1);ans=n;
	solve();
	for (int pos=1;pos<=n;){
		int flag=p[pos].first;
		for (;p[pos].first==flag;pos++){
			sl++;TL.add(p[pos].second,1);
			sr--;TR.add(p[pos].second,-1);
		}
		solve();
	}
	printf("%d\n",ans);
	return 0;
}