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

来源:互联网 发布:机器人算法工程师 编辑:程序博客网 时间:2024/06/06 12:26

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;}
原创粉丝点击