6058 Kanade's sum

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Puppettt/article/details/76583012

Kanade's sum

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1789    Accepted Submission(s): 728


Problem Description
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 rl+1<k.

Give you k , you need to calculate nl=1nr=lf(l,r,k)

There are T test cases.

1T10

kmin(n,80)

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

n5105
 

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 n 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
 

Source

2017 Multi-University Training Contest - Team 3 

自己做的时候想不通 看了别人的题解 怎么人家这么聪明呢qaq 

直通车:http://blog.csdn.net/yjf3151731373/article/details/76559550


题意:A 中所有区间内第K大的数求和

分析:遍历A中所有元素 令当前元素为第K大 往右找 上限 再往左找下限 具体看代码qaq


AC:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
typedef long long LL;
using namespace std;
const int N=1e6;
const LL mod=1e9+7;
int a[N]={0};
int b[N]={0};
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
       int n,k;scanf("%d%d",&n,&k);
       for(int i=1;i<=n;i++)scanf("%d",&a[i]);
       LL ans=0;
       //当前为第i大;
       for(int i=1;i<=n;i++)
       {
           int x=a[i],num=0,d=0,j;
           LL sum=0;

           b[++d]=i;//记位置
           for(j=i+1;j<=n&&d<k;j++)if(a[j]>x)b[++d]=j;

           if(d==k)//右边存a[j]>x使x成为第k大的数
           {
                num=1; //b当前区间算一个区间
                //算后面比它小的数量
                for(;j<=n;j++){
                    if(a[j]<x)num++;
                    else break;
                }

                sum+=num;
                //算前面比它小的数和比它大的数(前遇到一个比它大的数 后面比它大的数前进一个)

                for(j=i-1;j>=1&&d>=1;j--)
                {
                    if(a[j]<x)sum+=num;  //因为x是第二大的数区间不能跑到x左边 所以加上x右边的num
                    else
                    {
                        if(d==1)break;//b[1]就是本身 此时在右边界 不能左移了;

                        num=b[d]-b[d-1]; //右边比它大的数去掉一个区间
                        d--;
                        sum+=num;
                    }
                }
           }
           else{ //右边不存在
                for(j=i-1;j>=1&&d<k;j--)if(a[j]>x)b[++d]=j;
                if(d==k)
                {
                    sort(b+1,b+d+1);//
                    num=n-b[d]+1; //n-最大位置+1 = 右边比它大的最后一个位置右边比它小(a[j]<x)的数量;
                    sum+=num;
                    for(;j>=1&&b[d]>=i;j--)
                    {
                        if(a[j]<x)sum+=num;
                        else
                        {
                            if(b[d]==i)break;// i为右边界
                            num=abs(b[d]-b[d-1]);
                            d--;
                            sum+=num;
                        }
                    }
                }
           }
           ans+=x*sum;//sum为区间数
       }
       printf("%lld\n",ans);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/Puppettt/article/details/76583012
sum