题意:略

  思路:要我们求每个区间第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. .resources文件转为可视化.resx文件

    ResGen.exe启动闪退.--方法不对 参考:http://www.opdown.com/soft/101205.html 在cmd中启动ResGen.exe. 打开cmd:输入 C:\Windo ...

  2. WDlinux 修改后台默认8080端口的方法

    修改8080端口正确方法 新版本: 方法一: apache sed -i 's/8080/8088/' /www/wdlinux/wdapache/conf/httpd.conf 然后记得修改防火墙i ...

  3. 8个纯CSS3制作的动画应用及源码

    对于一个复杂的图形或者动画来说,之前我们的处理方式是图片叠加或者利用CSS+JavaScript的方法,然而随着CSS3标准的不断成熟,我们甚至完全可以利用CSS3来绘制一些图片和制作丰富的动画特效. ...

  4. Linux系统一本通(实用篇)

    本人最近一直在ubuntu,接下来和大家分享我曾经踩过的坑,和一些非常实用的命令知识- 安装中的磁盘分配 一般来说,在linux系统中都有最少两个挂载点,分别是/ (根目录)及 swap(交换分区), ...

  5. python3与python2的区别(目前遇到的)

    1.进击的print,变成一个函数,print() 2.urllib大一统,呵呵 3.python3默认绝对路径导入

  6. Codeforces Round #520 (Div. 2)

    Codeforces Round #520 (Div. 2) https://codeforces.com/contest/1062 A #include<bits/stdc++.h> u ...

  7. ActiveMQ之HelloWorld

    JMS实现JMS接口的消息中间件 Provider:生产者 Consumer:消费者 PTP:Point to Point:点对点的消息模型 Pub/Sub:Publish/Subscribe:发布订 ...

  8. [LeetCode_98]Validate Binary Search Tree

    题目链接 https://leetcode.com/problems/validate-binary-search-tree/ 题意 判断给定树是否是BST 思路 根据定义判断.递归. 代码 clas ...

  9. 【转】微信公众号h5网页被嵌入广告 不知道什么原因

    这个是因为http劫持导致的.HTTP劫持是在使用者与其目的网络服务所建立的专用数据通道中,监视特定数据信息,提示当满足设定的条件时,就会在正常的数据流中插入精心设计的网络数据报文,目的是让用户端程序 ...

  10. windows+nginx+tomcat实现集群负载均衡(生产环境必读)

    概念理解(原文链接) 集群:多个tomcat服务器运行同一个web服务就能称之为集群 负载均衡:apache按照一定方式将不同的客户端访问分配到不同的tomcat服务器 简单负载均衡实现: 网上参考了 ...