BZOJ 2687: 交与并

答案存在于

1.两个互相包含的区间

2.两个互不包含的区间

决策单调性显然

但是这代码很精妙啊,并不知道这个为什么能这样写

#include<cstdio>
#include<algorithm>
using namespace std;
long long ans;
int stack[1000005],f[1000005];
struct node{
	int l,r;
}a[1000005],A[1000005];
bool cmp(node a,node b){
	return a.l<b.l || a.l==b.l && a.r<b.r;
}
long long calc(int x,int y){
	if (A[x].r<A[y].l) return 0;
	return 1ll*(A[y].r-A[x].l)*(A[x].r-A[y].l);
}
int main(){
	int n;
	scanf("%d",&n);
	for (int i=1; i<=n; i++) scanf("%d%d",&a[i].l,&a[i].r);
	sort(a+1,a+n+1,cmp);
	int Last=0,ID=0;
	for (int i=1; i<=n; i++)
		if (a[i].r<=Last) {
			f[i]=1;
			ans=max(ans,1ll*(a[i].r-a[i].l)*(a[ID].r-a[ID].l));
		}
		else Last=a[i].r,ID=i;
	int N=0;
	for (int i=1; i<=n; i++) if (!f[i]) A[++N]=a[i];
	int top=2;
	for (int i=1; i<N; i++){
		if (top==i) top++;
		while (top<N && calc(i,top)<calc(i,top+1)) top++;
		ans=max(ans,calc(i,top));
	}
	printf("%lld\n",ans);
	return 0;
}

  

 

posted @ 2018-10-27 17:36  ~Silent  阅读(202)  评论(0编辑  收藏  举报
Live2D