2017 Multi-University Training Contest - Team 3 Kanade's sum hd6058

2023-03-15,,

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6058

题目:

Kanade's sum

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

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<k.

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

There are T test cases.

1≤T≤10

k≤min(n,80)

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

∑n≤5∗105

 
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
 
思路:
  很容易想到按公式算是不可行的(O(n^2)的时间复杂度),然后想到枚举计算每个数的贡献:即第k大为x的区间个数乘以x
  然后只要考虑怎么快速求出第k大为x的区间个数。如果能知道左边大于x的80个数的位置和右边大于x的80个数的位置就可以计算区间个数了。
  之后想到用从小到大枚举或者从大到小,用链表维护即可。
  比赛时用的set模拟的,结果被卡了,T的连妈都不认识。还是觉得时限卡太紧了。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int n,k,p[K],pre[K],nxt[K],pos[K],tl[],tr[];
LL ans; int main(void)
{
int t;cin>>t;
while(t--)
{
ans=;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d",p+i),pos[p[i]]=i;
for(int i=;i<=n;i++)
pre[i]=i-,nxt[i]=i+;
pre[]=,nxt[n]=n+;
for(int i=;i<=n;i++)
{
int la=,lb=;
for(int j=pos[i];j>&&la<=k;j=pre[j])
tl[la++]=j-pre[j];
for(int j=pos[i];j<=n&&lb<=k;j=nxt[j])
tr[lb++]=nxt[j]-j;
for(int j=;j<la;j++)
if(k-j-<lb)
ans+=i*1LL*tl[j]*tr[k-j-];
pre[nxt[pos[i]]]=pre[pos[i]];
nxt[pre[pos[i]]]=nxt[pos[i]];
}
printf("%lld\n",ans);
}
return ;
}

 

2017 Multi-University Training Contest - Team 3 Kanade's sum hd6058的相关教程结束。

《2017 Multi-University Training Contest - Team 3 Kanade's sum hd6058.doc》

下载本文的Word格式文档,以方便收藏与打印。