【多校训练】hdu 6058 Kanade's sum

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
 

题意:

给出一个1-n的排列,求所有区间(区间内第k大的数)的和。

思路:

可以转换成对每一个数,求它的统计次数*它的值。对每个数枚举左右两边比它大的数(枚举k个就行,k最大为80)。然后计算一下。

//
//  main.cpp
//  1003
//
//  Created by zc on 2017/8/7.
//  Copyright © 2017年 zc. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int N=550000;
int a[N],l[N],r[N];

int main(int argc, const char * argv[]) {
    int T,n,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)   scanf("%d",&a[i]);
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            if(n-a[i]+1<k)  continue;
            int ln=0,rn=0,le=i-1,re=i+1;
            l[0]=r[0]=i;
            while(le>0&&ln<k)
            {
                if(a[le]>a[i])  l[++ln]=le;
                le--;
            }
            l[++ln]=0;
            while(re<=n&&rn<k)
            {
                if(a[re]>a[i])  r[++rn]=re;
                re++;
            }
            r[++rn]=n+1;//cout<<i<<" "<<ln<<" "<<rn<<endl;
            for(int j=k-1;j>=0;j--)
            {
                if(j<ln&&k-j-1<rn)  ans+=(ll)(l[j]-l[j+1])*(r[k-j]-r[k-j-1])*a[i];//cout<<i<<" "<<j<<" "<<ans<<endl;
                if(k-j-1>rn)   break;
            }
        }
        printf("%lld\n",ans);
    }
    
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值