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 ...
随机推荐
- JS-格式化json
一 使用原生 JSON.stringify 实现 <textarea name="" id="myTA" cols="30" rows ...
- jmeter之集合点的使用
通过jmeter并不能1秒立即达到某一并发,这时候,可以通过集合点来实现,达到某一并发时,然后再一起执行某一动作,仅作用于第一次动作的时候 目录 1.集合点元件 2.简单的概念介绍 1.集合点元件 集 ...
- Grafana 下载与安装(v5.4.1)
官网地址: https://grafana.com/grafana/download Linux Ubuntu & Debian(64 Bit) SHA256: 3ccbdba9e7429f5 ...
- C++ std::isnan等函数的使用
C和C++11标准提供了类似于isnan.isfinite.isinf.isnormal.fpclassify分别用于判断是非数(NaN)值.有限制.无穷值.正常数值等. 今天在使用Modbus读取设 ...
- Spring事务传播及数据库事务操作
从Spring 事务配置说起 先看看Spring 事务的基础配置 <aop:aspectj-autoproxy proxy-target-class="true"/> ...
- mybatis使用Dao和Mapper方式
1.配置jdcp.properties数据库连接文件 #mysql database setting jdbc.type=mysql jdbc.driver=com.mysql.jdbc.Driver ...
- Meet in the middle算法总结 (附模板及SPOJ ABCDEF、BZOJ4800、POJ 1186、BZOJ 2679 题解)
目录 Meet in the Middle 总结 1.算法模型 1.1 Meet in the Middle算法的适用范围 1.2Meet in the Middle的基本思想 1.3Meet in ...
- CodeChef Sereja and GCD
Sereja and GCD Problem code: SEAGCD Submit All Submissions All submissions for this problem ar ...
- Codeforces - 1195E - OpenStreetMap - 单调队列
https://codeforc.es/contest/1195/problem/E 一个能运行但是会T的版本,因为本质上还是\(O(nmab)\)的算法.每次\(O(ab)\)初始化矩阵中的可能有用 ...
- Panabit的各种配置文件
Panabit的各种配置文件 1.启动配置文件路径:/etc/rc.local作用:这个文件里的内容是BSD启动后会自动执行的命令默认配置如下:fsck -y -t ufs /dev/ad0s2a & ...