Kanade's sum

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2512    Accepted Submission(s): 1045

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 r−l+1<k.

Give you k , 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

 
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
 
题目大意:给定数列A,A是1,2,...,n的一个排序,求数列中所有区间的第k小的数之和。
思路:对于1~n的某个数 i,向左向右分别数出 k 个比 i 大的数,求得第 k 小的数是 i 的最大区间。然后在这个最大区间里,从左往右每次取k个不小于 i 的数,每一次更新区间时加上前后两个区间的差值即可。
 
AC代码(借鉴了下网上的代码):

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
int a[];
long long l[], r[];
int main()
{
int T,n,k;
//ifstream cin("ylq.txt");
cin>>T;
while(T--)
{
scanf("%d %d", &n, &k);
//cin>>n>>k;
for(int i=;i<=n;i++) scanf("%d", &a[i]);
int t;
long long ans=;
for(int i=;i<=n;i++){
t=;
l[t++]=i;
int j;
for(j=i-;j>&&t<k;j--){
if(a[j]>a[i]){
l[t++]=j;
}
}
long long sum=;
if(t==k)
{
int tmp=;
for(;j>;j--){
if(a[j]<a[i]) tmp++;
else break;
}
sum+=tmp;
for(j=i+;j<=n&&t>=;j++){
if(a[j]<a[i]) sum+=tmp;
else{
t--;
if(t==) break;
tmp=l[t-]-l[t];
sum+=tmp;
}
}
}
else
{
for(j=i+;j<=n&&t<k;j++){
if(a[j]>a[i]) l[t++]=j;
}
if(t==k)
{
sort(l, l+t);
int tmp=l[];
int p=;
sum+=tmp;
for(;j<=n&&p<t;j++){
if(a[j]<a[i]) sum+=tmp;
else{
p++;
if(l[p]>i) break;
tmp=abs(l[p]-l[p-]);
sum+=tmp;
} }
}
}
ans+=(long long)(sum*a[i]);
}
cout<<ans<<endl;
}
}
 

HDU 6058 Kanade's sum —— 2017 Multi-University Training 3的更多相关文章

  1. HDU 6058 - Kanade's sum | 2017 Multi-University Training Contest 3

    /* HDU 6058 - Kanade's sum [ 思维,链表 ] | 2017 Multi-University Training Contest 3 题意: 给出排列 a[N],求所有区间的 ...

  2. hdu 6058 Kanade's sum(模拟链表)

    Kanade's sum Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  3. HDU 6058 Kanade's sum 二分,链表

    Kanade's sum Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th larg ...

  4. 【链表】2017多校训练三 HDU 6058 Kanade's sum

    acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...

  5. HDU - 6058 Kanade's sum

    Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6058 /* 思路是:找出每个x为第k大的区间个数有多少 用pos[i]保存当前x的位置, ...

  6. 2017ACM暑期多校联合训练 - Team 3 1003 HDU 6058 Kanade's sum (模拟)

    题目链接 Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th largest elem ...

  7. hdu 6058 Kanade's sum (计算贡献,思维)

    题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和 思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o( ...

  8. HDU 6059 - Kanade's trio | 2017 Multi-University Training Contest 3

    思路来自题解(看着题解和标程瞎吉尔比划了半天) /* HDU 6059 - Kanade's trio [ 字典树 ]  |  2017 Multi-University Training Conte ...

  9. HDU 6057 - Kanade's convolution | 2017 Multi-University Training Contest 3

    /* HDU 6057 - Kanade's convolution [ FWT ] | 2017 Multi-University Training Contest 3 题意: 给定两个序列 A[0 ...

随机推荐

  1. IQueryable不能使用异步方法的解决方案

    ---恢复内容开始--- 看见别人用Linq to Sql的Async好久了,我还没开始用,感觉太土了,跟不上潮流了,打开vs,就准备写个查询,然后发现我用一个IQueryable的对象,怎么都点不出 ...

  2. JDBC常用接口、类介绍

    JDBC常用接口.类介绍 JDBC提供对独立于数据库统一的API,用以执行SQL命令.API常用的类.接口如下: DriverManager 管理JDBC驱动的服务类,主要通过它获取Connectio ...

  3. 【ABAP系列】SAP SAP中关于编码的解释

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP SAP中关于编码的解释 ...

  4. JSP基础--javaBean

    JavaBean 1 JavaBean概述 1.1 什么是JavaBean JavaBean是一种规范,也就是对类的要求.它要求Java类的成员变量提供getter/setter方法,这样的成员变量被 ...

  5. [Linux] 017 网络命令与挂载命令

    1. 网络命令:write 命令名称:write 命令所在路径:/usr/bin/write 执行权限:所有用户 语法:write [用户名] 功能描述:给用户发信息,以 Ctrl-d 保存结束 范例 ...

  6. Java双链表

    一.概述 二.英雄类 class HeroNode { //值域 public int id; public String name; public String nickName; //指针域 pu ...

  7. UVAlive 6756 Increasing Shortest Path

    We all love short and direct problems, it is easier to write, read and understand the problem statem ...

  8. C. Trailing Loves (or L'oeufs?) (质因数分解)

    C. Trailing Loves (or L'oeufs?) 题目传送门 题意: 求n!在b进制下末尾有多少个0? 思路: 类比与5!在10进制下末尾0的个数是看2和5的个数,那么 原题就是看b进行 ...

  9. ios-实现ARC与MRC混编

    选择target -> build phases -> compile sources -> 用ARC的文件将compiler flags设置为:-fobjc-arc,用MRC的文件 ...

  10. Linux就该这么学11学习笔记

    参考链接:https://i.cnblogs.com/EditPosts.aspx?opt=1 文件传输协议 一般来讲,人们将计算机联网的首要目的就是获取资料,而文件传输是一种非常重要的获取资料的方式 ...