[HDU]6058 Kanade's sum
来源:互联网 发布:seo零基础 编辑:程序博客网 时间:2024/06/07 00:25
http://acm.hdu.edu.cn/showproblem.php?pid=6058
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
There are T test cases.
1≤T≤10
k≤min(n,80)
A[1..n] is a permutation of [1..n]
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
题解
这是一道技巧性非常强的一道题
如果能想到从小枚举第k大的值的话这个题也就能AC了,因为比当前值小的数字对当前值的第k大无影响,我们要知道的是比当前值大的所在位置即可,利用链表保存数据,每次枚举完了对当前数字进行删除
#pragma GCC optimize ("O2")#include<stdio.h>#include<vector>#include<algorithm>using namespace std;typedef long long LL;const int MAXS = 20 * 1024 * 1024;char buf[MAXS], *ch;inline void read(int &x) { while(*ch <= 32) ++ch; for(x = 0; *ch >= 48; ++ch) x = x * 10 + *ch - 48;}const int MAXN = 5e5 + 5;int pos[MAXN], par[MAXN], nxt[MAXN], n, k;int lst[85], rst[85], lsz, rsz;int main(){ fread(buf, MAXS, 1, stdin); ch = buf; int T, x; read(T); while(T--) { read(n); read(k); for(int i = 1; i <= n; ++i) { read(x); pos[x] = i; par[i] = i - 1; nxt[i] = i + 1; } LL ans = 0; for(int i = 1; i <= n; ++i) { lsz = rsz = 0; int it = pos[i]; while(0 < it && lsz < k) { lst[lsz++] = it - par[it]; it = par[it]; } it = pos[i]; while(it <= n && rsz < k) { rst[rsz++] = nxt[it] - it; it = nxt[it]; } int lp = lsz - 1, rp = k - lsz; while(rp < rsz && i + rp <= n) { ans += 1LL * i * lst[lp--] * rst[rp++]; } par[nxt[pos[i]]] = par[pos[i]]; nxt[par[pos[i]]] = nxt[pos[i]]; } printf("%lld\n", ans); } return 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
- 01背包问题
- Myeclipse、eclipse安装lombok
- ORACLE startup报错之ORA-01154&&ORA-01155&&ORA-01033&&ORA-03113
- 2017 Multi-University Training Contest
- 【逐梦旅程Windows游戏编程学习笔记 ①】基本GDI绘图
- [HDU]6058 Kanade's sum
- Zookeeper
- 排序算法的时间复杂度和空间复杂度
- P2P中的NAT穿越方案简介
- 从日常开发说起,浅谈HTTP协议是做什么的
- js的image()循环创建,src始终为最近创建的那个解决办法(给image.onload传参)
- Javascript之Object.assign()
- 第十七章:容器深入研究
- codeforces 835C(二维前缀和)