HDU 6058 Kanade's sum —— 2017 Multi-University Training 3
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
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
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]
#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的更多相关文章
- HDU 6058 - Kanade's sum | 2017 Multi-University Training Contest 3
/* HDU 6058 - Kanade's sum [ 思维,链表 ] | 2017 Multi-University Training Contest 3 题意: 给出排列 a[N],求所有区间的 ...
- hdu 6058 Kanade's sum(模拟链表)
Kanade's sum Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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 ...
- 【链表】2017多校训练三 HDU 6058 Kanade's sum
acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...
- HDU - 6058 Kanade's sum
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6058 /* 思路是:找出每个x为第k大的区间个数有多少 用pos[i]保存当前x的位置, ...
- 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 ...
- hdu 6058 Kanade's sum (计算贡献,思维)
题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和 思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o( ...
- HDU 6059 - Kanade's trio | 2017 Multi-University Training Contest 3
思路来自题解(看着题解和标程瞎吉尔比划了半天) /* HDU 6059 - Kanade's trio [ 字典树 ] | 2017 Multi-University Training Conte ...
- 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 ...
随机推荐
- 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 ...
- python脚本-上传apk至蒲公英
import requests import os #账号配置信息 url = "https://upload.pgyer.com/apiv1/app/upload" uKey = ...
- storm集群搭建和java应用
1. vim /etc/hosts ssh免密登录192.168.132.154 c0192.168.132.156 c1192.168.132.155 c2 storm集群:192.168.132. ...
- vue搭建项目之设置代理
前面将项目页面.axios.组件等都准备好了,现在就差设置代理了: 首先在config下新建两个文件,分别叫做dev.uri.js和prod.uri.js,代码为: module.exports = ...
- Nginx 模块 - ngx_core_module
原文地址 示例配置 指令 accept_mutex accept_mutex_delay daemon debug_connection debug_points env error_log even ...
- MySQL基础(创建库,创建表,添加数据)
CREATE DATABASE 数据库名; CREATE TABLE student2(sno VARCHAR(20) NOT NULL PRIMARY KEY COMMENT"学号&quo ...
- 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 ...
- 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) ...
- 2019牛客暑期多校训练营(第一场) - B - Integration - 数学
https://ac.nowcoder.com/acm/contest/881/B https://www.cnblogs.com/zaq19970105/p/11210030.html 试图改写多项 ...
- KindEditor 文本编辑器
官网:http://kindeditor.net/docs/usage.html 目前支持ASP.ASP.NET.PHP.JSP.