题意:略

  思路:要我们求每个区间第K大数之和,其实可以转换为求多少个区间的第K大数是X,然后我们在求和就好了。

     那么我们可以从小到大枚举所有可能成为第K大的数。为什么从小到大呢?

     因为从小到大我们就略去了大小的比较了,后面我们维护的链表就要把这个值除去。

/*  gyt
Live up to every day */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = +;
const ll maxm = 1e7;
const int modd = ;
const int INF = <<;
const db eps = 1e-;
int pos[maxn], pre[maxn], nex[maxn]; void solve() {
int n, k; scanf("%d%d", &n, &k);
for (int i=; i<=n; i++) {
int x; scanf("%d", &x);
pos[x]=i; pre[i]=i-, nex[i]=i+;
}
pre[]=; nex[n+]=n+;
ll sum=;
for (int j=; j<=n; j++) {
int x=pos[j];
int rq[];
int lc=, rc=;
// cout<<"x:"<<x<<endl;
for (int i=x; i<=n&&rc<k; i=nex[i]) {
rq[++rc]=nex[i]-i;
// cout<<nex[i]<<" "<<i<<endl;
}
ll ans=;
for (int i=x; i>&&lc<k; i=pre[i]) {
lc++;
int r=k-lc+;
if (r>rc) continue;
ans+=(i-pre[i])*rq[r];  //前面有多少个比他小的数,我们就可以构成那么多的区间
//cout<<"ans:"<<ans<<endl;
//cout<<(i-pre[i])<<" "<<rq[r]<<endl;
}
//cout<<"rc:"<<rc<<" lc:"<<lc<<endl;
sum+=ans*j;
pre[nex[x]]=pre[x];
nex[pre[x]]=nex[x];
//cout<<j<<" "<<ans<<endl;
//cout<<"---------------"<<endl;
}
cout<<sum<<endl;
}
int main() {
int t = ;
//freopen("in.txt", "r", stdin);
scanf("%d", &t);
//getchar();
while(t--)
solve();
return ;
}

hdu-6058 Kanade's sum的更多相关文章

  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. 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)Tota ...

  5. HDU - 6058 Kanade's sum

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

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

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

  7. 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 ...

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

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

  9. 2017 Multi-University Training Contest - Team 3 Kanade's sum hd6058

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6058 题目: Kanade's sum Time Limit: 4000/2000 MS (J ...

  10. hdu6058 Kanade's sum 区间第k大

    /** 题目:Kanade's sum 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6058 题意:给定[1,n]的排列,定义f(l,r,k)表示区间[l ...

随机推荐

  1. oracle连接错误:ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务解决

    自己的解决办法是,把数据库连接字符串的默认SID_NAME = ORCL改为 sid_name =test(自己安装数据库时候改的名字).即可正常连接. 网上搜罗的其他问题:把监听服务重启下.(自己的 ...

  2. with as 如何工作

    with as 如何工作   with如何工作? Python对with的处理还是很机智滴.基本思想就是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法 紧跟wi ...

  3. VMware Ubuntu安装

    不是每一个程序员都必须玩过linux,只是博主觉得现在的很多服务器都是linux系统的,而自己属于那种前端也搞,后台也搞,对框架搭建也感兴趣,但是很多生产上的框架和工具都是安装在服务器上的,而且有不少 ...

  4. xshell常用的快捷键

    删除ctrl + d      删除光标所在位置上的字符相当于VIM里x或者dlctrl + h      删除光标所在位置前的字符相当于VIM里hx或者dhctrl + k      删除光标后面所 ...

  5. WdatePicker控件Javascript取得当前时间、取得减30分钟时间

    1.取得当前时间 function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var se ...

  6. SSM提交了事物但数据库不执行

    从图中可以看到,spring已经给出事物提交成功,但数据库并未插入数据,找了老半天发现,数据库表上我加了个触发器,触发器执行失败造成没有数据库commit.但程序没什么不报异常吗?

  7. [leetcode]114. Flatten Binary Tree to Linked List将二叉树展成一个链表

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  8. YII2表单中上传单个文件

    有些时候我们提交的表单中含有文件.怎么样让表单里的数据和文件一起提交. 我的数据表tb_user内容如下: CREATE TABLE `tb_user` ( `id` int(11) unsigned ...

  9. JFinal Web开发学习(四)数据库连接与自动生成model

    1.新建数据库jfinal_test,user表 /* Navicat MySQL Data Transfer Source Server : . Source Server Version : 50 ...

  10. 在BCH硬分叉后防止重放攻击-1

    导致新加密货币的硬分叉为加密交换运营商带来了独特的机遇和挑战.Poloniex在最近的Bitcoin Cash硬叉之后面临的一个挑战是保护我们的客户免受重播攻击.由于SV链后面的开发团队选择在分叉后近 ...