\(f(l,r,k)=\)区间[\(l\),\(r\)]的第k大。

\(\sum_{l=1}^{n}{\sum_{r=l}^{n}{f(l,r,k)}}\)

参考题解,claris大佬题解。赛后AC。。。

题目链接

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <string> using namespace std;
typedef long long int LL;
const int MOD = 1e9 + 7;
const int INF = 2e9 + 1e8;
const int maxn = 1e6 + 100; struct dsajkbdsuhvjgfdwsaghvfewdsjhfcvdsghvfdsjhfbcgvdsgh
{
int pre,next,val;
}lst[maxn];
int pos[maxn],n,k;;
int temp[maxn];
void del(int x)
{
int pre=lst[x].pre,nxt=lst[x].next;
lst[pre].next=nxt;
lst[nxt].pre=pre;
}
LL calc(int x)
{
int lft[100],rit[100];
int il=0,ir=0;
for(int i=x;i>0;i=lst[i].pre)
{
lft[++il]=i-lst[i].pre;
if(il==k) break;
}
for(int i=x;i<=n;i=lst[i].next)
{
rit[++ir]=lst[i].next-i;
if(ir==k) break;
}
LL res=0;
for(int i=1;i<=il;i++)
{
if(k-i+1<=ir) res+=1ll*lft[i]*rit[k-i+1];
}
return res;
}
int main()
{
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
lst[i].pre=i-1,lst[i].next=i+1;
scanf("%d",&lst[i].val);
pos[lst[i].val]=i;
}
lst[0].next=1,lst[n+1].pre=n;
LL sum=0;
for(int i=1;i<=n-k+1;i++)
{
sum+=i*calc(pos[i]);
del(pos[i]);
}
cout<<sum<<endl;
}
return 0;
}

hdu 6058的更多相关文章

  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

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

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

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

  4. hdu 6058 思维

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6058 分析题目的时候,由于枚举的区间很多,而第k大的值范围小,应该要想到去枚举第k大的值然后找到这个值对答 ...

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

  6. hdu 6058 Kanade's sum(模拟链表)

    Kanade's sum Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

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

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

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

  9. HDU 6058:Kanade's sum(思维)

    题目链接 题意 给出一个n和一个k,求1~n的每个区间的第k大的总和是多少,区间长度小于k的话,贡献为0. 思路 首先有一个关系:当一个数是第k大的时候,前面有x个比它大的数,那么后面就有k-x-1个 ...

随机推荐

  1. 【ActiveMQ】2.spring Boot下使用ActiveMQ

    在spring boot下使用ActiveMQ,需要一下几个条件 1.安装并启动了ActiveMQ,参考:http://www.cnblogs.com/sxdcgaq8080/p/7919489.ht ...

  2. 26个高效工作的小技巧 z

    1.时间常有,时间优先. 2.时间总会有的:每天只计划4-5 小时真正的工作. 3.当你在状态时,就多干点:不然就好好休息:有时候会连着几天不是工作状态,有时在工作状态时却又能天天忙活 12 小时,这 ...

  3. Android---简单的动画

  4. angular md-toast 颜色

    How to show md-toast with background color https://codepen.io/neilkalman/pen/jWBqve <div ng-contr ...

  5. 矩形嵌套-记忆化搜索(dp动态规划)

    矩形嵌套 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 有n个矩形,每个矩形能够用a,b来描写叙述,表示长和宽. 矩形X(a,b)能够嵌套在矩形Y(c,d)中当且仅 ...

  6. Swift中的switch 和 do while

    switch后面的()能够省略 OC中的switch假设没有break就会穿透(依次运行),在Swift中不会穿透(可理解默认就有break) OC中入股要在case中定义变量,必要要加上{}确定作用 ...

  7. 生产追溯系统-Wifi+传感器,实现计数器以及监控机器是否停止

    物联网听上去是一个高大上的词儿,还有什么大数据.云.智能制造等等,今天我也往这方面稍微靠一靠,这篇文章主要介绍的是通过 wifi 模块与传感器组合,实现感应计数器,应用场景主要如下: 1.统计 SMT ...

  8. Mac终端处理MySql

    进入数据库: mysql -u root -p 随后输入密码:root   原文出处: GarveyCalvin的博客(@GarveyCalvin) MySQL有很多的可视化管理工具,比如“mysql ...

  9. THE MARTIAN

    影片的最后一段自白 When I was up there, stranded by myself …… “did I think I was going to die?” Yes, absolute ...

  10. HBase写请求分析

    HBase作为分布式NoSQL数据库系统,不单支持宽列表.而且对于随机读写来说也具有较高的性能.在高性能的随机读写事务的同一时候.HBase也能保持事务的一致性. 眼下HBase仅仅支持行级别的事务一 ...