Q:
给N(1≤N≤1e5)个任意长度的三角形,长度为a1,a2,…aN(1≤ai≤1e9),Q(1≤Q≤1e5)组询问,每组询问给出l和r,在第l个到第r个的长度中选三个组成三角形使其周长最大,不存在输出-1
A:
首先这三个长度按大小排序时的标号显然必定是连续的:
比方说设三个长度a>b>c, 如果a是这个区间中最大的数,b,c肯定是次大的数和次次大的数,因为这个时候b和c已经尽可能大了,假如加起来还是不能大于a,那么对于取a为最大数的情况下,数组里就莫得满足条件的b和c了
所以我们也就是按这个顺序找(a=最大,b=次大,c=次次大),(a=次大,b=次次大,c=次次次大),……以此类推直到c为区间中最小数中是否有满足b+c>a的,有的话break,一直都莫得就为-1
那么要怎么找才不会TLE呢?这个时候我们需要推一个规律。
我们在最坏情况下需要找多少次才能找到一个满足条件的三元组(a,b,c)呢?
找不到说明b+c<=a,最坏的可能就是取等于,一直找到1e9,显然是一个斐波那契数列,F(1)=1,F(2)=1,F(3)=F(2)+F(1),F(4)=F(3)+F(2),……,F(45)>1e9,所以最坏情况下找45组(假如这个区间有这么多数的话),一定会找到一个满足条件的三元组。
所以对于一个查询,我们找这个区间的最大,次大,……45大就好啦(是指从大往小找)
有挺多方法找的:
主席树:主席树可以找到第k大,套个板子,它是一个一个找(最大,次大,…),看看符不符合就好了
注意!Kuangbin板子中指的第k大的数是指从小到大的第k个数,我们要从大到小找,稍加修改即可
另:一般来说由于主席树带一个很大的常数,跑起来时间是线段树的5倍左右。但是这道题的数据应该是随机数据(随机数据主席树不用找45个(这个毕竟是特殊情况)就能找到符合条件的了,线段树却需要一次性处理45个),所以反而卡线段树(可做优化以ac)不卡主席树
线段树:线段树找前45大,它是一次性找出45个来,需优化
RMQ:找前45大,同线段树
主席树ac代码:(套的kuangbin的板子,据说某些数组在这道题里并不需要开?我也不叽道我只是只菜鸡.jpg等我彻底弄明白线段树再看。。)
Time:1294ms
Memory:74200kB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn=100010;
const ll M=maxn*30;
ll n,q,m,tot;
ll a[maxn],t[maxn];
ll T[maxn],lson[M],rson[M],c[M];
void Init_hash()
{
for(ll i=1;i<=n;i++)
t[i]=a[i];
sort(t+1,t+1+n);
m=unique(t+1,t+1+n)-t-1;
}
ll build(ll l,ll r)
{
ll root=tot++;
c[root]=0;
if(l!=r)
{
ll mid=(l+r)>>1;
lson[root]=build(l,mid);
rson[root]=build(mid+1,r);
}
return root;
}
ll hashh(ll x)
{
return lower_bound(t+1,t+1+m,x)-t;
}
ll update(ll root,ll pos,ll val)
{
ll newroot=tot++,tmp=newroot;
c[newroot]=c[root]+val;
ll l=1,r=m;
while(l<r)
{
ll mid=(l+r)>>1;
if(pos<=mid)
{
lson[newroot]=tot++;
rson[newroot]=rson[root];
newroot=lson[newroot];root=lson[root];
r=mid;
}
else
{
rson[newroot]=tot++;
lson[newroot]=lson[root];
newroot=rson[newroot];root=rson[root];
l=mid+1;
}
c[newroot]=c[root]+val;
}
return tmp;
}
ll query(ll left_root,ll right_root,ll k)
{
ll l=1,r=m;
while(l<r)
{
ll mid=(l+r)>>1;
if(c[lson[left_root]]-c[lson[right_root]]>=k)
{
r=mid;
left_root=lson[left_root];
right_root=lson[right_root];
}
else
{
l=mid+1;
k-=c[lson[left_root]]-c[lson[right_root]];
left_root=rson[left_root];
right_root=rson[right_root];
}
}
return l;
}
int main()
{
while(scanf("%lld%lld",&n,&q)!=EOF)
{
memset(a,0,sizeof(a));
memset(T,0,sizeof(T));
memset(lson,0,sizeof(lson));
memset(rson,0,sizeof(rson));
memset(t,0,sizeof(t));
memset(c,0,sizeof(c));
tot=0;
for(ll i=1;i<=n;i++)
scanf("%lld",&a[i]);
Init_hash();
T[n+1]=build(1,m);
for(ll i=n;i;i--)
{
ll pos=hashh(a[i]);
T[i]=update(T[i+1],pos,1);
}
while(q--)
{
ll l,r;
scanf("%lld%lld",&l,&r);
if(r-l+1<3)
{
printf("-1\n");
continue;
}
else
{
ll a=t[query(T[l],T[r+1],r-l+1)];
ll b=t[query(T[l],T[r+1],r-l)];
ll c=t[query(T[l],T[r+1],r-l-1)];
//cout<<a<<' '<<b<<' '<<c<<endl;
if(b+c>a)
{
printf("%lld\n",(a+b+c));
}
else
{
//cout<<"?"<<endl;
bool flag=false;
ll step=1;
for(ll k=r-l-2;k>=1;k--)
{
a=b;
b=c;
c=t[query(T[l],T[r+1],k)];
//cout<<a<<' '<<b<<' '<<c<<endl;
if(b+c>a)
{
flag=true;
printf("%lld\n",(a+b+c));
break;
}
if(flag) break;
step++;
if(step>47) break;
}
if(!flag)
printf("-1\n");
}
}
}
}
return 0;
}
线段树:(之后再补)
RMQ:(之后再补)