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. CycleGAN --- Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks

    文章地址:http://openaccess.thecvf.com/content_ICCV_2017/papers/Zhu_Unpaired_Image-To-Image_Translation_I ...

  2. python脚本-上传apk至蒲公英

    import requests import os #账号配置信息 url = "https://upload.pgyer.com/apiv1/app/upload" uKey = ...

  3. storm集群搭建和java应用

    1. vim /etc/hosts ssh免密登录192.168.132.154 c0192.168.132.156 c1192.168.132.155 c2 storm集群:192.168.132. ...

  4. vue搭建项目之设置代理

    前面将项目页面.axios.组件等都准备好了,现在就差设置代理了: 首先在config下新建两个文件,分别叫做dev.uri.js和prod.uri.js,代码为: module.exports = ...

  5. Nginx 模块 - ngx_core_module

    原文地址 示例配置 指令 accept_mutex accept_mutex_delay daemon debug_connection debug_points env error_log even ...

  6. MySQL基础(创建库,创建表,添加数据)

    CREATE DATABASE 数据库名; CREATE TABLE student2(sno VARCHAR(20) NOT NULL PRIMARY KEY COMMENT"学号&quo ...

  7. How to exploit the x32 recvmmsg() kernel vulnerability CVE 2014-0038

    http://blog.includesecurity.com/2014/03/exploit-CVE-2014-0038-x32-recvmmsg-kernel-vulnerablity.html ...

  8. HDU 4012 Paint on a Wall(状压+bfs)

    Paint on a Wall Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  9. 2019牛客暑期多校训练营(第一场) - B - Integration - 数学

    https://ac.nowcoder.com/acm/contest/881/B https://www.cnblogs.com/zaq19970105/p/11210030.html 试图改写多项 ...

  10. KindEditor 文本编辑器

    官网:http://kindeditor.net/docs/usage.html 目前支持ASP.ASP.NET.PHP.JSP.