2020-2021 ACM-ICPC, Asia Seoul Regional Contest L. Two Buildings (决策单调性 分治)

linkkkkkk
题意:
给定长度为n nn的数组c cc,求m a x ( ( j − i ) ∗ ( c i + c j ) ) max((j-i)*(c_i+c_j))max((ji)(ci+cj))
思路:

  • 将式子转化为m a x ( ( j − i ) ∗ ( c j − ( − c i ) ) ) max((j-i)*(c_j-(-c_i)))max((ji)(cj(ci)))
    ( j , c j ) , ( i , − c i ) (j,c_j),(i,-c_i)(j,cj),(i,ci)分别看做是矩形的左下角和右上角的点,问题就转化成了求矩形的最大面积。
    假设a = ( j , c j ) , b = ( i , − c i ) a=(j,c_j),b=(i,-c_i)a=(j,cj),b=(i,ci)
  • 可以看出b bb是有决策单调性的,固定a aa的话,肯定b bb越往右上越优;所以对于b bb来说,如果b [ u ] x . < b [ v ] . x , b [ u ] . y < = b [ v ] . y b[u]x.<b[v].x,b[u].y<=b[v].yb[u]x.<b[v].x,b[u].y<=b[v].y,那么v vv是比u uu优的,可以直接忽略掉u uu
    a aa也同理。
  • 如果计算答案呢,假设a aa数组的长度为l a lalab bb数组的长度为l b lblb,此时的两个数组都先按x xx从小到大再按y yy从小到大排序了,即从左下到右上。用s o l v e ( l 1 , r 1 , l 2 , r 2 ) solve(l1,r1,l2,r2)solve(l1,r1,l2,r2)表示a [ l 1 : r 1 ] a[l1:r1]a[l1:r1]b [ l 2 : r 2 ] b[l2:r2]b[l2:r2]的最大值,考虑怎么实现比较优秀的递归。
  • m i d = ( l 1 + r 1 ) / 2 mid=(l1+r1)/2mid=(l1+r1)/2,遍历b [ l 2 , r 2 ] b[l2,r2]b[l2,r2]找到p o s pospos使得a [ m i d ] − b [ p o s ] a[mid]-b[pos]a[mid]b[pos]的面积最大,即p o s pospos是相对于m i d midmid的最优解。那么接下来应该递归s o l v e ( l 1 , m i d − 1 , l 2 , p o s ) solve(l1,mid-1,l2,pos)solve(l1,mid1,l2,pos)s o l v e ( m i d + 1 , r 1 , p o s , r 2 ) solve(mid+1,r1,pos,r2)solve(mid+1,r1,pos,r2)
    在这里插入图片描述图片来自

所以对于a aam i d midmid左下的点来说,最优解只会在p o s pospos以及p o s pospos左边。
时间复杂度O ( n l o g n ) O(nlogn)O(nlogn)

代码:

// Problem: L. Two Buildings
// Contest: Codeforces - 2020-2021 ACM-ICPC, Asia Seoul Regional Contest
// URL: https://codeforces.ml/gym/102920/problem/L
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
//#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
typedef pair<string,string>PSS;
#define I_int ll
inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}
 
inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
 
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
#define x first
#define y second
ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
 
const int maxn=5e6+100,inf=0x3f3f3f3f;

ll ans,n,c[maxn];

struct node{
	ll x,y;
}a[maxn],b[maxn];
 
bool cmp(node a,node b){
	if(a.x==b.x) return a.y<b.y;
	return a.x<b.x;
}
ll solve(int l1,int r1,int l2,int r2){
	if(l1>r1||l2>r2) return 0;
	int mid=(l1+r1)/2;
	ll maxx=-1e18,pos=l2;
	for(int i=l2;i<=r2;i++){
		if(a[mid].x>b[i].x&&a[mid].y>b[i].y) continue;
		ll tmp=(b[i].x-a[mid].x)*(b[i].y-a[mid].y);
		if(maxx<tmp) maxx=tmp,pos=i;
	}
	maxx=max(maxx,solve(l1,mid-1,l2,pos));
	maxx=max(maxx,solve(mid+1,r1,pos,r2));
	ans=max(ans,maxx);
	return maxx;
}
 
int main() {
    n=read;
    rep(i,1,n){
    	c[i]=read;
    	a[i]={i,-c[i]};b[i]={i,c[i]};
    }
    sort(a+1,a+1+n,cmp);
    sort(b+1,b+1+n,cmp);
    
    int pos=1,idx=0;
    unordered_map<int,int>mp;
    for(int i=2;i<=n;i++)
    	if(a[i].y>=a[pos].y) mp[i]=1;
    	else pos=i;
    for(int i=1;i<=n;i++)
    	if(!mp[i]) a[++idx]=a[i];
    int tot=0;mp.clear();
    pos=n;
    for(int i=n-1;i;i--)
    	if(b[i].y<=b[pos].y) mp[i]=1;
    	else pos=i;
    for(int i=1;i<=n;i++)
    	if(!mp[i]) b[++tot]=b[i];
    solve(1,idx,1,tot);
    
    cout<<ans<<endl;
    
    return 0;
}

参考1
参考2


版权声明:本文为weixin_45675097原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。