hdu 6058 Kanade's sum
来源:互联网 发布:我的世界编程猫 编辑:程序博客网 时间:2024/06/06 16:47
Kanade's sum
Problem Description
Give you an array A[1..n] of length n .
Letf(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 youk , 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
Let
Specially ,
Give you
There are T test cases.
Input
There is only one integer T on first line.
For each test case,there are only two integersn ,k on first line,and the second line consists of n integers which means the array A[1..n]
For each test case,there are only two integers
Output
For each test case,output an integer, which means the answer.
Sample Input
15 21 2 3 4 5
Sample Output
30
题意:给你n个数,这n个数是一种全排列,问你所有子区间的第k大的数之和。
思路:遍历A中所有元素 令当前元素为第K大 往右找 上限 再往左找下限 具体看代码
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; typedef long long LL; const int INF=0x3f3f3f3f; const LL MOD=1e9+7; const int MAXN=1e6+7; int a[MAXN],pos[MAXN]; int s[MAXN],t[MAXN]; int pre[MAXN],nxt[MAXN]; int n,k; void erase(int x)//把下标为x的这个数从数组中删掉 { int pp=pre[x],nn=nxt[x]; if(pre[x]) nxt[pre[x]]=nn; if(nxt[x]<=n) pre[nxt[x]]=pp; pre[x]=nxt[x]=0; } int main() { int T; 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;//每一个数对应的下标 } for(int i=1;i<=n;++i) { pre[i]=i-1; nxt[i]=i+1; } LL ans=0; for(int num=1;num<=n-k+1;++num) { int p=pos[num]; int s0=0,t0=0; for(int d=p;d&&s0<=k;d=pre[d]) { s[++s0]=d; } for(int d=p;d!=n+1&&t0<=k;d=nxt[d]) { t[++t0]=d; } s[++s0]=0;t[++t0]=n+1; for(int i=1;i<=s0-1;++i) { if(k+1-i<=t0-1&&k+1-i>=1) { ans+=(LL)(t[k+1-i+1]-t[k+1-i])*(s[i]-s[i+1])*num; //计算每一次的贡献 } } erase(p); } printf("%lld\n",ans); } return 0; }
阅读全文
0 0
- HDU-6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu 6058 Kanade's sum
- [HDU]6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu--6058Kanade's sum
- Kanade's sum HDU 6058
- HDU 6058 Kanade's sum
- Kanade's sum(hdu 6058)
- HDU 6058 Kanade's sum
- HDU 6058 Kanade's sum
- hdu 6058 Kanade's sum
- hdu 6058 Kanade's sum
- HDU 6058 Kanade's sum
- HDU 6058 Kanade's sum
- 观察者模式
- InnoDB关键特性之自适应hash索引
- ios 中 localstorage有时失效的问题
- HDOJ 3790 (Dijkstra最短路,注意,这里是多权情况!)
- oracle 之关键字exists
- hdu 6058 Kanade's sum
- 使用Protues进行Arduino仿真——Blink
- SpringMVC学习之EL表达式一
- 2017/8/4训练日记(最小生成树 ||Prim算法和Kruskal算法|_·))
- 杭电-1000-第一篇自然是最简单的求和
- spring boot session redis的使用
- 使用七牛图床
- 项目第二天
- int main() 和 int main( int argc, char * argv[])的区别