hdu6058-Kanade's sum
来源:互联网 发布:羽毛球计分软件 编辑:程序博客网 时间:2024/06/08 19:42
Give you an array A[1..n]A[1..n]of length nn.
Let f(l,r,k)f(l,r,k) be the k-th largest element of A[l..r]A[l..r].
Specially , f(l,r,k)=0 f(l,r,k)=0 if r-l+1< k
Give you kk , you need to calculate ∑nl=1∑nr=lf(l,r,k)∑l=1n∑r=lnf(l,r,k)
There are T test cases.
1≤T≤101≤T≤10
k≤min(n,80)k≤min(n,80)
A[1..n] is a permutation of [1..n]A[1..n] is a permutation of [1..n]
∑n≤5∗105∑n≤5∗105
Input
There is only one integer T on first line.
For each test case,there are only two integers nn,kk on first line,and the second line consists of nn integers which means the array A[1..n]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,寻找某个数位置在其前面的k个比它大的数以及k个位置在其后面的比他大的数。
2,题意可以转化为求某个数在多少个区间中是第k大的数,然后将结果加起来。
3,从小到大枚举可能成为第k大的数,由于是从小到大的枚举,那么剩下的数一定都是大于等于他的,这样也就省去了大小的比较。枚举之后维护链表将其删除。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=500009;int T,n,k,a[maxn],pos[maxn],pre[maxn],np[maxn];int s[maxn],t[maxn];long long ans=0;int main(){ scanf("%d",&T); while(T--) { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); pos[a[i]]=i;//记录位置 pre[i]=i-1;//链表前驱 np[i]=i+1;//链表后继 } ans=0; int s0=0,t0=0; for(int num=1;num<=n-k+1;num++)//枚举可能成为第k大的数 { int p=pos[num]; s0=0,t0=0; //寻找前后k个比它大的数 for(int i=p;i&&s0<=k+1;i=pre[i]) s[++s0]=i; for(int j=p;j<=n&&t0<=k+1;j=np[j]) t[++t0]=j; //首尾额外添加两个无限大的数 s[++s0]=0; t[++t0]=n+1; for(int i=1;i<=s0-1;i++) { int tmp=k-i+1; if(tmp<=t0-1&&tmp>0) ans+=(s[i]-s[i+1])*1LL*(t[tmp+1]-t[tmp])*num; } //从链表中将此数删除 int tpre=pre[p]; int tnp=np[p]; if(tpre) np[tpre]=tnp; if(tnp<=n) pre[tnp]=tpre; pre[p]=np[p]=0; } printf("%I64d\n",ans); } return 0;}
- HDU6058-Kanade's sum
- hdu6058-Kanade's sum
- hdu6058 Kanade's sum
- HDU6058-Kanade's sum
- HDU6058 Kanade's sum
- HDU6058-Kanade's sum
- HDU6058 Kanade's sum
- hdu6058 Kanade's sum
- HDU6058 Kanade's sum【模拟】
- 【HDU6058】Kanade's sum(暴力 or 链表)
- 【2017多校】HDU6058 Kanade's sum 【链表】
- HDU6058-Kanade's sum 链表+思维
- HDU6058 Kanade's sum(链表)
- HDU6058 2017杭电多校联赛第三场-Kanade's sum
- 2017杭电多校联赛team3 Kanade's sum hdu6058 快速幂
- Hdu6058 Kanade's sum(2017多校第3场)
- 2017多校训练Contest3: 1003 Kanade's sum hdu6058
- 2017杭电多校第三场 1003 Kanade's sum(hdu6058 区间内第k大)
- parser.add_argument中的action
- FTP时显示500 Illegal PORT command的解决
- 怎样从0开始搭建一个测试框架_0——总章
- Java文件操作
- NAT技术与代理服务器调研
- hdu6058-Kanade's sum
- SVN Checkout 问题 The XML response contains invalid XML
- elasticsearch5.2 集群
- iOS集成weex(0)
- properties文件配置
- SharedPreference的使用以及commit()和apply()的区别
- 拼多多笔试题总结
- hisi SDK安装记录
- SpringBoot之文件下载