hdu 6058 Kanade's sum
来源:互联网 发布:mac 浏览器 编辑:程序博客网 时间:2024/06/07 07:15
Kanade's sum
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 516 Accepted Submission(s): 182
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 | We have carefully selected several similar problems for you: 6066 6065 6064 6063 6062
思路:问题等价于求每个数字分别是多少个区间的第k大,也就是可以枚举位置,然后再枚举左边x个数比这个数要大,那么右边必须要有k-1-x个数比这个位置要大,思路大概是这样。因为序列是1~n的排列,我们先记录每个数字出现的位置,我们维护一个链表,从最大值n开始枚举到1,每次把值插入到链表中,然后通过遍历链表用滑动窗口的办法达到O(n*(logn+k)),那个logn是我用set来记录已经插入的坐标有哪些,然后可以在logn的时间内找到第一个比当前插入坐标要大的坐标,从而达到修改链表的操作。
具体方法看代码吧。也有标注释,不懂留言。
//#pragma comment(linker, "/STACK:102400000,102400000") #include<iostream> #include<cmath> #include<queue> #include<cstdio> #include<queue> #include<algorithm> #include<cstring> #include<string> #include<utility>#include<set>#include<map>#include<stack>#include<vector>#define maxn 500005#define inf 0x3f3f3f3fusing namespace std;typedef long long LL;const double eps = 1e-8;const int mod = 1e9 + 7;int n, left1[maxn], right1[maxn], vis[maxn];const int read(){char ch = getchar();while (ch<'0' || ch>'9') ch = getchar();int x = ch - '0';while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';return x;}int main(){int t;scanf("%d", &t);while (t--){LL ans = 0;int k;scanf("%d%d", &n, &k);set<int>s;set<int>::iterator it;for (int i = 1; i <= n; i++){int x = read();left1[i] = 0;right1[i] = n + 1;vis[x] = i;//标记每个数字所在的位置}if (k < 1)printf("0\n");left1[n + 1] = 0;right1[n + 1] = n + 1;s.insert(0);s.insert(n + 1);//默认两位第0位和n+1位最大for (int i = n; i > 0; i--){s.insert(vis[i]);it = s.find(vis[i]);int l , r;it++;//找到第一个比当前位置大的位置r = *it;l = left1[r];left1[r] = vis[i];right1[vis[i]] = r;right1[l] = vis[i];left1[vis[i]] = l;//链表的插入int nowl = vis[i];for (int j = 0; j < k&&nowl; j++)nowl = left1[nowl];int nowr = nowl;for (int j = 0; j < k&&nowr != n + 1; j++)nowr = right1[nowr];//找到左端点和右端点for (int j = 0; j < k; j++){if (nowl == vis[i] || nowr == n + 1)break;int nextl = right1[nowl];int nextr = right1[nowr];ans += 1ll*(nextl - nowl)*(nextr - nowr)*i;nowl = nextl;nowr = nextr;}}printf("%lld\n", ans);}}
阅读全文
2 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
- 你需要来自XXX的权限才能对此文件夹进行更改 win10
- struts2初学笔记
- CentOS7简单配置网络
- 超好用的网络抓包框架(Windivert)之四(实例二)
- 数据库和缓存同步机制
- hdu 6058 Kanade's sum
- 2743: [HEOI2012]采花(离线树状数组)
- Python库之Requests(iMooc)---(1)
- 菜鸟心历之路(6)
- 【JavaScript】最简单的一个例子
- 《Visual Basic 程序设计》——>基本概念
- String类的常用方法(1)A
- LeetCode 231 Power of Two AND LeetCode 326 Power of Three
- bootstrap table中显示头行结构