0
点赞
收藏
分享

微信扫一扫

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


​​linkkkkkk​​​ 题意:
给定长度为的数组,求
思路:

  • 将式子转化为
    分别看做是矩形的左下角和右上角的点,问题就转化成了求矩形的最大面积。
    假设
  • 可以看出是有决策单调性的,固定的话,肯定越往右上越优;所以对于来说,如果,那么是比优的,可以直接忽略掉
    也同理。
  • 如果计算答案呢,假设数组的长度为数组的长度为,此时的两个数组都先按从小到大再按从小到大排序了,即从左下到右上。用表示的最大值,考虑怎么实现比较优秀的递归。
  • ,遍历找到使得的面积最大,即是相对于的最优解。那么接下来应该递归
  • 2020-2021 ACM-ICPC, Asia Seoul Regional Contest L. Two Buildings (决策单调性 分治)_分治算法_33

所以对于左下的点来说,最优解只会在以及左边。
时间复杂度

代码:

// 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​​


举报

相关推荐

0 条评论