HDU6058(乱搞)

Kanade’s sum

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

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 r−l+1

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 10;
int n, k;
int a[maxn];
int q[2 * maxn];
int head,  tail;
int Hash[maxn];
int main()
{
    //freopen("C:\\Users\\creator\\Desktop\\1003.txt","r",stdin);
    //freopen("C:\\Users\\creator\\Desktop\\out.txt","w",stdout);
    //freopen("C:\\Users\\creator\\Desktop\\in1.txt","r",stdin) ;
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &k);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            Hash[a[i]] = i;
        }
        ll ans = 0;
        for(int i = 1; i <= n - k + 1; i++)//枚举第k大
        {
            head = tail = maxn;
            q[tail] = Hash[i];
            int L, R;
            L = R = 0;
            for(int j = Hash[i] - 1; j >= 1; j--)
            {
                if(a[j] > i)
                {
                    q[--tail] = j;
                    L++;
                }
                if(L >= k) break;
            }
            for(int j = Hash[i] + 1; j <= n; j++)
            {
                if(a[j] > i)
                {
                    q[++head] = j;
                    R++;
                }
                if(R >= k) break;
            }
            ll term = 0;
            int s;
            int judge = maxn - tail;
            if(judge == k) s = tail + 1;
            else
            {
                s = tail;
                q[tail - 1] = 0;
            }
            for(int j = s; j <= maxn; j++)
            {
                ll x1 = 0;
                ll x2 = 0;
                x1 = abs(q[j - 1] - q[j]);
                int mm = k - 1 + j;
                if(mm > head) x2 = 0;
                else if(mm == head)
                {
                    x2 = n -q[head] + 1;
                }
                else
                {
                    x2 = (ll)q[mm + 1] - (ll)q[mm];
                }
                term += x1 * x2;
            }
            ans += term * (ll)i;

        }
        printf("%lld\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值