hdu 多校联赛 Kanade's sum

来源:互联网 发布:电表数据丢失 编辑:程序博客网 时间:2024/06/05 10:16

Kanade's sum

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


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
15 21 2 3 4 5
 

Sample Output
30
题意:输入n和k 有n个数 n个数的数值为1—n  f(l,r,k)=0 if  的值为l到r内的第k大的值  nl=1nr=lf(l,r,k)

求这个公式的值 

刚开始并没有想到能够把这个公式转化为更简单的公式 后来看到题解说将这个公式转化 为i可能在某个区间内成为第k大的值的区间个数乘以i这个值本身 

解法:

这道题主流解法大致有两种 一种是暴力枚举的方法 另一种则是建立模拟链表的方法

方法一(枚举):

通过枚举进行遍历 每一个数遍历他的左右边的值 记录有多少比他大的数 这样就可以知道他成为区间第k大的区间组合的个数

ac代码(看脸ac):

#include<bits/stdc++.h>using namespace std;#define LL long longconst int maxn=500000+10;int a[maxn];int l[maxn],r[maxn];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]);        }        LL ans=0;        for(int i=0;i<n;i++)        {            int lcnt=1,rcnt=1,j;//lcnt代表元素a[i]左边比他大的数有多少个,rcnt同理            for( j=i+1;j<n;j++)            {                if(rcnt>k)                    break;                if(a[j]>a[i])                {                    r[rcnt++]=j-i;//r[rcnt]代表右边第rcnt个比a[i]大的数距离a[i]的距离,这个是方便计算的,可以等于j,                                  //但是计算的时候要特殊处理右边只有一个比a[i]大的时候,下方的rnum=1,比较麻烦,                                  //原来是那样做的,不建议                }            }            if(j>=n)                r[rcnt]=n-i; //如果a[i]右边比他大的数没超过k个,                             //那么我们知道a[i]右边比他大的数只有rcnt-1个,                             //我们假设距离a[i]最远的比他大的那个数为righht,                             //(程序中没有right这个变量,这里就是为了方便理解)                             //这里的r[rcnt]就是为了方便后面统计right右边有多少个比a[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;//同理上面            for(j=0;j<lcnt;j++)            {                if(k-j-1>=rcnt)                    continue;                int lnum=l[j+1]-l[j];                int rnum=r[k-j]-r[k-j-1];                ans+=(LL)a[i]*lnum*rnum;            }        }        printf("%lld\n",ans);    }    return 0;}
这中方是存在缺点的 因为它需要遍历每一个数并且遍历他周围的数 时间复杂度比较高 算起来比较慢 交到oj上花费的时间大约是2000ms左右 所以有时会过 有时会超时
方法二(模拟链表):

通过数组建立类似与链表的结构来存储数据 其实收据还是存储再数组中 只不过是测试数据成了下标 而数组的值成了位置 能实现这一条非常关键的条件也是因为a【1】-a【n】的数值范围在1-n之内且无重复 我们通过链表先从最小的数值开始向链表的两端找可能使他成为第k大的区间的范围 找完后删除这个值 再找下一个最小的值 因为是用了链表 用完即删 而且从小开始找 所以每一个数的都是链表所剩的数中最小的数 

ac代码:

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int K=1e6+7;int n,k,p[K],pre[K],nxt[K],pos[K],tl[85],tr[85];LL ans; int main() {     int t;cin>>t;     while(t--)     {         ans=0;         scanf("%d%d",&n,&k);//读入n和k n为已知数的个数 k为公式条件         for(int i=1;i<=n;i++)             {                scanf("%d",&p[i]);//读入p数组                pos[p[i]]=i;//将p数组的下标和数值互换 pos数组这里体现了模拟链表的思想 pos的下标为数 pos的值为这个数的位置             }         for(int i=1;i<=n;i++)             pre[i]=i-1,nxt[i]=i+1;//对pre数组和nxt进行赋值 pre为下标-1,nxt为下标+1         pre[1]=0,nxt[n]=n+1;//对两个数组的头和未进行处理 防止越界         for(int i=1;i<=n-k+1;i++)//开始最外层的循环 i的值是开始输入时p【i】中的值 pos【i】是i这个值的位置         {//因为位置和数值都是从1到n的 找区间第k大的数 大于n-k+1的数不可能在任何区间成为第k大的数 直接让i<n-k+1就行             int la=0,lb=0;             for(int j=pos[i];j>0&&la<=k;j=pre[j])//i这个数的位置向前推 找到能形成k大区间的前限可能位置个数                 tl[la++]=j-pre[j];             for(int j=pos[i];j<=n&&lb<=k;j=nxt[j])//i这个数的位置向后推 找到能形成k大区间的后限的可能位置的个数                 tr[lb++]=nxt[j]-j;             for(int j=0;j<la;j++)             if(k-j-1<lb)//判断这个位置是否越界                 ans+=i*1LL*tl[j]*tr[k-j-1];//左边区间长度*右边区间长度就是这个可能区间个数的总量 再乘i值本身也就是答案了             pre[nxt[pos[i]]]=pre[pos[i]];//重新更新去掉已被处理完i值之后前驱后驱数组的值             nxt[pre[pos[i]]]=nxt[pos[i]];         }        printf("%lld\n",ans);    }     return 0; }


r