长大校赛C(st表+二分)

这个题有点意思 。。可能窝的想法和题解不一样。。不过比较容易想到。。

首先从最暴力想起,就是枚举区间端点,再找最次值酱紫。。当然次大值这个还是相当好搞,维护一个st表就可以解决辣。。

如果枚举子区间的话明显是超时的。。然后固定一端另一端也没办法很好地去怎么计数。。所以这个思路可能得放弃了。。

然后貌似是齐齐说过,像这种枚举所有子区间并排序的肯定是要考虑每个元素的贡献?所以尝试求出a[i]的贡献。。

怎么求。。可以先往右延伸,那么可以找到第一个比a[i]大的数a[y]和第二个比a[i]大的数a[_y],怎么找?就是二分嘛。。还好前几天刚做233333

二分可以找到y,然后以x后边的区间进行二分可以找到 _y,然后可行的区间就是包含x而不包含_y的。。即有_y-y个区间

同理向左延伸可以找出_x和x(_x<x)

然后就是同时延伸的情况了。。

其中x和y是一定要包含一个的。。那就分类讨论。。

包含x,那就不包含y,有y-i-1个区间,同理包含y有i-x-1个区间。。

然后就可以算出该位置的贡献了。。进而得到答案。。

然而有个问题是a[i]相同的时候可能会有重复计算。。这个比较好解决。。再以下标为第二关键字,这样就把贡献转移给了一方,就不会重复。。。

这样弄下去时间复杂度为O(nlogn),理论上是能a的。。然而。。 T了!!没错出题人又卡常了!!!

那要怎么优化呢。。。想到的一个方法就是二分的时候二分边界就不改成中间值了,而是直接变为区间最大值所在的位置,这个理论上应该会快一点。。实际上也是快挺多的。。从tle(时限3S)卡到了300ms。。效果还是不错的。。



#include<bits/stdc++.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define inf 1e9
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define link(x) for(edge*j=h[x];j;j=j->next)
#define eps 1e-8
#define succ(x) (1<<(x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define lowbit(x) (x&(-x))
#define NM 100005
#define MAXN 100005
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
 
 
 
int n,a[NM],tmp[NM],mn[NM];
ll m;
int dp[MAXN][32];int pos[MAXN][32],b[NM];
void st(){
    mem(dp);mem(pos);mem(mn);
    inc(i,2,n)mn[i]=mn[i/2]+1;
    inc(i,0,n+1) dp[i][0]=a[i],pos[i][0]=i;
    for(int j=1;succ(j)<=n;j++)
        for(int i=1;i+succ(j)-1<=n;i++){
            if(dp[i][j-1]>dp[i+(1<<(j-1))][j-1]) dp[i][j]=dp[i][j-1],pos[i][j]=pos[i][j-1];
            else dp[i][j]=dp[i+(1<<(j-1))][j-1],pos[i][j]=pos[i+(1<<(j-1))][j-1];
        }
}
int rmq(int l,int r){
    int k=mn[r-l];
    int t1=dp[l][k];int t2=dp[r-(1<<k)+1][k];
    if(t1>=t2) return pos[l][k];
    else return pos[r-(1<<k)+1][k];
}
int work(int x,int y,int i){
    int s=n+1,k=x;
    for(;x<=y;){
        int t=x+y>>1;
        if(a[rmq(k,t)]>=a[i]){
            y=rmq(k,t)-1;s=rmq(k,t);
        }else x=t+1;
    }
    return s;
}
 
int _work(int x,int y,int i){
    int s=0,k=y;
    for(;x<=y;){
        int t=x+y>>1;
        if(a[rmq(t,k)]>a[i]){
            x=rmq(t,k)+1;s=rmq(t,k);
        }else y=t-1;
    }
    return s;
}
 
bool cmp(int x,int y){
    return a[x]>a[y];
}
 
int main(){
    //freopen("data.in","r",stdin);
    int _=read();
    while(_--){
        n=read();m=read();
        inc(i,1,n)a[i]=read();
        a[n+1]=inf;a[0]=inf;
        st();mem(b);
        inc(i,1,n){
            int x,y,_x,_y;         
            x=_work(0,i-1,i);
            y=work(i+1,1+n,i);
            if(x>1)_x=_work(0,x-1,i);else _x=0;
            if(y<n)_y=work(y+1,n+1,i);else _y=n+1;
        //  printf("%d %d %d %d\n",_x,x,y,_y);
            b[i]=(x-_x)+(_y-y)+(x>=1?(y-i-1)*(x-_x):0)+(y<=n?(i-x-1)*(_y-y):0);
        }
    //  inc(i,1,n)printf("%d ",b[i]);putchar('\n');
        inc(i,1,n)tmp[i]=i;
        sort(tmp+1,tmp+1+n,cmp);
        inc(k,1,n){
            int i=tmp[k];
            if(m<=b[i]){
                printf("%d\n",a[i]);break;
            }
            m-=b[i];
        }
    }
    return 0;
}




链接:https://www.nowcoder.com/acm/contest/102/C
来源:牛客网


The K-th Largest Interval

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

 We define a value of an interval is the second largest number of it's elements, and of course an interval has at least two elements.
Given an array A with n elements and a number k, can you find the value of the kth largest interval?

输入描述:

 
 
The first line contains an integer number T, the number of test cases. 
For each test case : 
The first line contains two integer numbers n,k(2 ≤ n ≤ 10 5,1 ≤ k ≤ n(n−1)/2), the number of test cases. 
The second lines contains n integers A i(1 ≤ A i ≤ 10 9), the elements of array A.

输出描述:

For each test case print the value of the kth largest interval.
示例1

输入

2
3 3
1 2 3
5 1
1 2 2 3 3

输出

1
3

说明

For the sample input, there are three intervals.
Interval [1 2 3] has value 2.
Interval [2 3] has value 2.
Interval [1 2] has value 1.
So the 3rd largest interval is [1 2] whose value is 1.

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

 We define a value of an interval is the second largest number of it's elements, and of course an interval has at least two elements.
Given an array A with n elements and a number k, can you find the value of the kth largest interval?

输入描述:

 
 
The first line contains an integer number T, the number of test cases. 
For each test case : 
The first line contains two integer numbers n,k(2 ≤ n ≤ 10 5,1 ≤ k ≤ n(n−1)/2), the number of test cases. 
The second lines contains n integers A i(1 ≤ A i ≤ 10 9), the elements of array A.

输出描述:

For each test case print the value of the kth largest interval.
示例1

输入

2
3 3
1 2 3
5 1
1 2 2 3 3

输出

1
3

说明

For the sample input, there are three intervals.
Interval [1 2 3] has value 2.
Interval [2 3] has value 2.
Interval [1 2] has value 1.
So the 3rd largest interval is [1 2] whose value is 1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值