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

简介: 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 ) )

思路:

将式子转化为m a x ( ( j − i ) ∗ ( c j − ( − c i ) ) )

将( j , c j ) , ( i , − c i )分别看做是矩形的左下角和右上角的点,问题就转化成了求矩形的最大面积。

假设a = ( j , c j ) , b = ( i , − c i )。

可以看出b是有决策单调性的,固定a的话,肯定b越往右上越优;所以对于b来说,如果b [ u ] x . < b [ v ] . x , b [ u ] . y < = b [ v ] . y那么v是比u优的,可以直接忽略掉u。a也同理。

如果计算答案呢,假设a数组的长度为l a,b数组的长度为l b,此时的两个数组都先按x从小到大再按y从小到大排序了,即从左下到右上。用s o l v e ( l 1 , r 1 , l 2 , r 2 )表示a [ l 1 : r 1 ]和b [ l 2 : r 2 ]的最大值,考虑怎么实现比较优秀的递归。

设m i d = ( l 1 + r 1 ) / 2,遍历b [ l 2 , r 2 ]找到p o s使得a [ m i d ] − b [ p o s ]的面积最大,即p o s是相对于m i d的最优解。那么接下来应该递归s o l v e ( l 1 , m i d − 1 , l 2 , p o s )和s o l v e ( m i d + 1 , r 1 , p o s , r 2 )。

20200401134307494.png

图片来自

所以对于a中m i d左下的点来说,最优解只会在p o s以及p o s左边。

时间复杂度O ( n l o g n )


代码:

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


目录
相关文章
|
7月前
|
机器学习/深度学习 人工智能 BI
2019ICPCshenyang网络赛(C. Dawn-K's water)
2019ICPCshenyang网络赛(C. Dawn-K's water)
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
The Preliminary Contest for ICPC China Nanchang National Invitational M题 Subsequence
80 0
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR
The Preliminary Contest for ICPC China Nanchang National Invitational K题 MORE XOR
78 0
|
机器学习/深度学习
The Preliminary Contest for ICPC China Nanchang National Invitational J题 Distance on the tree
The Preliminary Contest for ICPC China Nanchang National Invitational J题 Distance on the tree
96 0
The Preliminary Contest for ICPC China Nanchang National Invitational H题 Coloring Game
The Preliminary Contest for ICPC China Nanchang National Invitational H题 Coloring Game
96 0
|
机器学习/深度学习 人工智能
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
The Preliminary Contest for ICPC China Nanchang National Invitational I题 Max answer
97 0
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
The Preliminary Contest for ICPC China Nanchang National Invitational A题 PERFECT NUMBER PROBLEM
72 0
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
95 0
upc2021秋组队训练赛第一场 ICPC North Central NA Contest 2020
2020 ICPC Asia Taipei-Hsinchu Site Programming Contest H. Optimization for UltraNet (二分+最小生成树+算贡献)
2020 ICPC Asia Taipei-Hsinchu Site Programming Contest H. Optimization for UltraNet (二分+最小生成树+算贡献)
129 0