hdu 6058 Kanade's sum


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

Let  f(l,r,k)  be the k-th largest element of  A[l..r] .

Specially ,  f(l,r,k)=0  if  rl+1<k .

Give you  k  , you need to calculate  nl=1nr=lf(l,r,k)

There are T test cases.

1T10

kmin(n,80)

A[1..n] is a permutation of [1..n]

n5105
 

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
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6066  6065  6064  6063  6062 
 

Statistic |  Submit |  Discuss |  Note
题意:求所有区间的第k大的权值和,不存在第k大权值为0。

思路:问题等价于求每个数字分别是多少个区间的第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 0x3f3f3f3f
using 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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值