2017 HDU 6058 多校联合赛 Kanade's sum

Give you an array A[1..n] of length n .

Let f(l,r,k) be the k-th largest element of A[l..r].

Specially , f(l,r,k)=0 if r−l+1< k

Give you k , you need to calculate ∑(l =1 to n)∑(r = l to n)f(l,r,k)

There are T test cases.

1≤T≤10

k≤min(n,80)

A[1..n] is a permutation of [1..n]

∑n≤5∗100000

Input

There is only one integer T on first line.

For each test case,there are only two integers n , k on first line,and the second line consists

of nn integers which means the array A[1..n]

Output

For each test case,output an integer, which means the answer.

Sample Input

1

5 2

1 2 3 4 5

Sample Output

30

题意:

给我们一个数组 a [ ],给了我们一个区间,两层循环for(l=1 to n) for(r = l to n) 求区间 [ r , l ] 内第K大的数,把这些第K大的数累计求和。

解题思路:

采用枚举的思想,在 a 数组中遍历每一个元素,并求出该元素左右方各有多少个比它大的数,这样我们就可以知道该元素可以在多少个区间内充当第K大的数。

PS:该算法是暴力枚举,所以最终代码是卡时间过的,大约1800ms左右,有可能超时,所以如果超时了可以换个姿势再来一遍 orz

这里写图片描述

详解:

举个例子,例如上图,此时遍历到 9 号点了,圆圈代表比它大的数,黑点代表比它小的数。假设 K=3

那么我们在它的左边取两个比它大的数,右边不取,可以发现区间 [ 2,9 ]中,9 号点位于第三大,所以这区间是符合条件的,然后我们把区间的边界进行平移一下,可以发现在 [ 1 , 9 ] 中也可以满足,最终可以发现左边界可以用 ( 1 , 2 ) ,右边界可以用( 9 , 10 ),所以这种情况下可以组成 number( 1 , 2 ) * number( 9,10 ) =4 种区间。

然后找下一种情况,它的左边取一个比它大的数,右边再取一个比它大的数,于是发现区间[ 5,11 ]符合条件,同理,我们对区间边界进行一下扩展,发现左边界可以用( 3 , 4 , 5 ),右边界可以用( 11 , 12 , 13 , 14 ),所以这种情况可以组成 number( 3,4,5 ) * number( 11,12,13,14 ) =12种区间。

然后继续找下一种情况,左边不放比它大的数,右边放两个比它大的数,可以发现区间 [ 9,15 ]是符合条件的,扩展一下区间边界,左边界可以用 ( 6 ,7 ,8 ) ,右边界可以用( 15,16 ,17 ),所以这种情况下可以组成 number ( 6 ,7 ,8 ) * number( 15,16 ,17 ) =9 种区间。

所以这个 9 号元素就遍历完了,以 9 号元素为第三大的数的区间一共有 4+12+9=25 种,每次 9 号元素都会加一次,所以 9 号元素对总和的贡献是 a[ 9 ] * 25 。然后继续遍历下一个元素即可,当所有元素遍历完,总和就求出来了。

代码:

#include<cstdio>
#include<cstring>
using namespace std;
const int Max = 5*10e5+10;
int a[Max],r[Max],l[Max];   ///r[x]用来存该元素右边第x个比它大的数与该元素的距离,l[x]同理
int rcnt,lcnt;             ///rcnt用来标记该元素右边有多少个比该元素大的数,rcnt同理
int rnum,lnum;             ///rnum表示初始右边界的右边有多少个可以扩展的点,lrum同理
long long ans;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,k;
        scanf("%d%d",&n,&k);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        ans=0;
        for(int i=0; i<n; i++)
        {
            rcnt=lcnt=1;
            int j;
            for(j=i+1; j<n; j++)
            {
                if(rcnt>k)
                    break;
                if(a[j]>a[i])
                    r[rcnt++]=j-i;
            }
            if(j>=n)
                r[rcnt]=n-i;
            ///---------------左右方向分界线--------------///
            for(j=i-1; j>=0; j--)
            {
                if(lcnt>k)
                    break;
                if(a[j]>a[i])
                    l[lcnt++]=i-j;
            }
            if(j<0)
                l[lcnt]=i-(-1);
            if(lcnt+rcnt-2+1<k)
                continue;
            else
            {
                for(int x=0; x<lcnt; x++)
                {
                    if(x+rcnt-1+1<k)
                        continue;
                    else
                    {
                        lnum=l[x+1]-l[x];
                        rnum=r[k-x]-r[k-x-1];
                        ans+=(long long)a[i]*lnum*rnum;
                    }
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值