题目链接
解题思路
我们发现显然第一个和第三个区间的值区间随着长度的增大而增大。
于是我们就可以枚举第一个区间的右端点位置,然后现在问题就转化成了找到一个断点来确定第二,三个区间的长度,由于前文提到的第三个区间的值区间随着长度的增大而增大,于是我们就可以通过二分来找出这个断点。
这里我使用了分块来查询区间最小值,时间复杂度 \(O(n \times \sqrt n \times log_2n)\),常数小的话能过。
如果使用其它数据结构可以有更好的复杂度。
参考代码
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define re register
#define ll long long
#define forl(i,a,b) for(re ll i=a;i<=b;i++)
#define forr(i,a,b) for(re ll i=a;i>=b;i--)
#define forll(i,a,b,c) for(re ll i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(re ll i=a;i>=b;i-=c)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) (x&-x)
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define db long double
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
#define maxqueue priority_queue<ll>
#define minqueue priority_queue<ll,vector<ll>,greater<ll>>
ll t;
ll n;
ll a[200010];
ll sq,b[200010],maxn[200010],minn[200010],L[200010],R[200010];
ll summax[200010];
ll querymax(ll l,ll r)
{
ll ma=-1e18;
if(b[l]==b[r])
{
forl(i,l,r)
ma=max(ma,a[i]);
return ma;
}
forl(i,l,R[b[l]])
ma=max(ma,a[i]);
forl(i,L[b[r]],r)
ma=max(ma,a[i]);
forl(i,b[l]+1,b[r]-1)
ma=max(ma,maxn[i]);
return ma;
}
ll querymin(ll l,ll r)
{
ll mi=1e18;
if(b[l]==b[r])
{
forl(i,l,r)
mi=min(mi,a[i]);
return mi;
}
forl(i,l,R[b[l]])
mi=min(mi,a[i]);
forl(i,L[b[r]],r)
mi=min(mi,a[i]);
forl(i,b[l]+1,b[r]-1)
mi=min(mi,minn[i]);
return mi;
}
void solve()
{
cin>>n;
forl(i,1,n+1)
summax[i]=0;
forl(i,1,n)
cin>>a[i];
forr(i,n,1)
summax[i]=max(summax[i+1],a[i]);
sq=pow(sqrt(n),1);
forl(i,1,n)
{
L[i]=R[i-1]+1;
R[i]=min(n,sq*i);
minn[i]=1e18;
forl(j,L[i],R[i])
b[j]=i,minn[i]=min(minn[i],a[j]),maxn[i]=max(maxn[i],a[j]);
if(R[i]==n)
break;
}
ll ma1=-1e18,ma2=-1e18;
forl(i,1,n-2)
{
ma1=max(ma1,a[i]);
ll L=i+1,R=n-1;
while(L<=R)
{
ll Mid=(L+R)/2;
ll mi1=querymin(i+1,Mid);
ma2=summax[Mid+1];
if(mi1==ma1 && mi1==ma2)
{
cfy;
cout<<i<<' '<<Mid-i<<' '<<n-Mid<<endl;
return ;
}
if(mi1<=ma1 && ma2<=ma1)
R=Mid-1;
else
L=Mid+1;
}
}
cfn;
}
int main()
{
IOS;
t=1;
cin>>t;
while(t--)
solve();
QwQ;
}
标签:CF1454F,ma,forl,ll,Partition,mi,200010,Array,define
From: https://www.cnblogs.com/wangmarui/p/18286592