列表

详情


NC52174. Symmetrical Painting

描述

Initially, the entire coordinate plane is colored white. N rectangles were painted black. The i-th rectangle has bottom-left corner (i-1, L_i)and top-right corner (i,R_i) for . You want to paint some of the black area white so that the remaining black part has a horizontal axis of symmetry. Find the maximum possible area of the remaining black part.

输入描述

The first line of input contains a single integer N (1 <= N <= 300000), denoting the number of rectangles.

The next N lines of input contain two space-separated integers each, denoting Li, Ri (1 <= Li < Ri <= 109).

输出描述

Output a single integer, the answer to the problem.

示例1

输入:

3
1 5
4 7
2 3

输出:

4

说明:

It is optimal to paint second rectangle entirely white and a rectangle with bottom-left corner (0, 4) and top-right corner (1, 5) white. Then, the remaining black figure has axis of symmetry at y = 2.5. The remaining black area is 4.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 181ms, 内存消耗: 7656K, 提交时间: 2019-08-19 11:05:19

#include<bits/stdc++.h>
using namespace std;
const int N=300010;
typedef long long LL;
LL a[N*3],n,curans,ans,lst,num;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	for(LL l,r,i=0;i<n;i++){
		cin>>l>>r;
		a[i*3]=l<<2;
		a[i*3+1]=r<<2;
		a[i*3+2]=(l+r)*2+1;
	}
	sort(a,a+3*n);
	for(int i=0;i<n*3;i++){
		curans+=(a[i]/2-lst)*num;
		ans=max(ans,curans);
		lst=a[i]/2;
		if(a[i]&1)num-=2;
		else num++;
	}cout<<ans<<endl;
	return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 176ms, 内存消耗: 7528K, 提交时间: 2019-08-15 13:43:16

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll b[900005],l,r,n,ma,xl,nu,la;
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>l>>r;
		b[i*3]=l*4;
		b[i*3+1]=r*4;
		b[i*3+2]=(l+r)*2+1;
	}
	sort(b,b+n*3);
	ma=0;xl=0;nu=0;la=0;
	for(int i=0;i<n*3;i++){
		nu+=((b[i]/2)-la)*xl;
		ma=max(ma,nu);
		la=b[i]/2;
		if(b[i]&1){
			xl-=2;
		}else{
			xl++;
		}
	}
	cout<<ma;
	
}

上一题