\(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. Andriod PopupWindow 键盘冲突

    调起键盘的时候,弹出PopupWindow,但是键盘没有隐藏. private void init() { View contentView = LayoutInflater.from(mContex ...

  2. win10安装 迅雷

    下载安装文件所在目录 ,右键,选择以管理员身份运行,输入程序地址,如D:\download\ThunderMini_dl1.5.3.288.exe,回车,这样就出来了程序安装页面,之后就可以就行正常的 ...

  3. POJ 1018 Communication System 题解

    本题一看似乎是递归回溯剪枝的方法.我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了非常久,无奈忍不住偷看了下提示.发现方法真多.有贪心,DP,有高级剪枝的.还有三分法的.八仙过海各 ...

  4. Effective Go(官方文档)笔记

    Effective Go(官方文档)笔记 自己主动局部变量提升(编译期完毕?):return &...; 内置函数: new/make copy, append delete range(这是 ...

  5. C++学习总结1

    一.内存管理 一般new 与 delete 同时出现.假如释放一个对象用 delete p即可.多个对象用delet [ ]p  即:new与delete需要搭配好. C++继承了C的许多函数,mal ...

  6. Jememeter和Loadrunner测试MySQL性能

    From:http://blog.csdn.net/testingstar/article/details/60579454 MySQL数据库性能测试的方法 前置条件: 安装系统:windows 7 ...

  7. Andriod三步学会安卓自己定义视图及其属性

    第一步:自己定义属性 第二步:自己定义控件解析属性 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcm5adW9adW8=/font/5a6L5L2T/fo ...

  8. 爬虫框架_scrapy1

    介绍: Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可用 ...

  9. LeetCode:二叉树的非递归中序遍历

    第一次动手写二叉树的,有点小激动,64行的if花了点时间,上传leetcode一次点亮~~~ /* inorder traversal binary tree */ #include <stdi ...

  10. 平滑的滚动listview到一个指定位

    http://blog.csdn.net/w8320273/article/details/9043339