HDU6058(乱搞)

来源:互联网 发布:java web工作流开发 编辑:程序博客网 时间:2024/06/05 10:53

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