HDU6058 Kanade's sum(链表)
来源:互联网 发布:安卓微信数据恢复 编辑:程序博客网 时间:2024/06/08 15:27
Kanade's sum
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2859 Accepted Submission(s): 1182
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
Source
2017 Multi-University Training Contest - Team 3
Recommend
liuyiding
题意:给一个数组,找到所有区间的第k大数求和
对于每一个数x,我们只需要在x的左边找k个比x大的数,x的右边找k个比x大的数,线性扫一遍就能知道有多少个区间包含x而且x是区间第k大。
用链表维护,从小到大枚举所以可以直接删除。
#include <map>#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <queue>using namespace std;const int MAXN=1000100;const int mod=1e9+7;int a[MAXN],pos[MAXN];int pre[MAXN],nxt[MAXN];void del(int x){ pre[nxt[x]]=pre[x]; nxt[pre[x]]=nxt[x];}int n,k;long long L[100],R[100];long long solve(int x){ int l=0,r=0; //往前跳k步 for(int i=x;i&&l<=k;i=pre[i]){ L[++l]=i-pre[i];//中间距离多少个数 } //往后跳k步 for(int i=x;i<=n&&r<=k;i=nxt[i]){ R[++r]=nxt[i]-i; } long long ans=0; for(int i=1;i<=l;i++){ if(k-i+1<=r&&k-i+1>=1){ ans+=L[i]*R[k-i+1]; } } return ans;}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=0;i<=n+1;i++){ pre[i]=i-1; nxt[i]=i+1; } pre[0]=0,nxt[n+1]=n+1; long long res=0; for(int i=1;i<=n;i++){ res+=solve(pos[i])*i; //因为是从小到大来算的,所以可以直接删除 del(pos[i]); } printf("%lld\n",res); }}
阅读全文
0 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【模拟】
- 2017杭电多校第三场 1003 Kanade's sum(hdu6058 区间内第k大)
- HDU6058 2017杭电多校联赛第三场-Kanade's sum
- 2017杭电多校联赛team3 Kanade's sum hdu6058 快速幂
- Hdu6058 Kanade's sum(2017多校第3场)
- 2017多校训练Contest3: 1003 Kanade's sum hdu6058
- 成功的背后!(给所有IT人)
- 字典树模板
- ROS中使用USB摄像头
- win10快捷键冲突
- 组建AR、VR和MR的硬件共享实验室,你认为可能吗?
- HDU6058 Kanade's sum(链表)
- win10 windows文件夹就占用40G
- 前端,前端轮播的做法
- 手把手教你不使用任何工具将腾讯视频qlv格式转换为MP4格式的方法
- 【hpuoj 1400 神奇的操作 】
- HTML之JavaScript自学笔记(3)
- 计算机二级C选择题精华
- Oracle学习笔记 -- day07 PL/SQL基本语法之变量、常量、游标、异常
- 关于MERGE JOIN CARTESIAN