hdu 6058
\(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的更多相关文章
- 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
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6058 /* 思路是:找出每个x为第k大的区间个数有多少 用pos[i]保存当前x的位置, ...
- 【链表】2017多校训练三 HDU 6058 Kanade's sum
acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...
- hdu 6058 思维
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6058 分析题目的时候,由于枚举的区间很多,而第k大的值范围小,应该要想到去枚举第k大的值然后找到这个值对答 ...
- 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(模拟链表)
Kanade's sum Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu 6058 Kanade's sum (计算贡献,思维)
题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和 思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o( ...
- 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 ...
- HDU 6058:Kanade's sum(思维)
题目链接 题意 给出一个n和一个k,求1~n的每个区间的第k大的总和是多少,区间长度小于k的话,贡献为0. 思路 首先有一个关系:当一个数是第k大的时候,前面有x个比它大的数,那么后面就有k-x-1个 ...
随机推荐
- android listview 添加数据
<span style="white-space:pre"> </span>listView = (ListView) findViewById(R.id. ...
- 拦截recyclerview 的item 的点击事件
recyclerview.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(),recyclerview, new Re ...
- com关于IUnknown接口
com定义的每个接口都必须从IUnknown继承过来,主要原因是IUnknown接口提供了两个很重要的特性:生存期控制和接口查询. 客户程序仅仅能通过接口与com对象进行通信.尽管客户程序能够无论对象 ...
- xml文件的根节点layout_width或者layout_height设置无效果的原因分析
在android开发中相信大家对ListView.GridView等组建都非常熟悉,在使用它们的时候须要自己配置相关的Adapter,而且配置现骨干的xml文件作为ListView等组建的子View, ...
- 测试 MD
上面是一张图片 总店?
- tensor搭建--windows 10 64bit下安装Tensorflow+Keras+VS2015+CUDA8.0 GPU加速
windows 10 64bit下安装Tensorflow+Keras+VS2015+CUDA8.0 GPU加速 原文见于:http://www.jianshu.com/p/c245d46d43f0 ...
- 转载---- 使用opencv源码自己编制android so库的过程
http://blog.csdn.net/lantishua/article/details/21182965 工作需要,在Android上使用OpenCV.opencv当前的版本(2.4.8)已经有 ...
- Linux机器间ssh免密登录
前言 一台Linux机器通过ssh的方式连接别的机器或通过scp的方式传输文件,都需要输入密码. 为了解决每次输入密码的困扰,可采用添加密钥的方式实现. 实现过程 源服务器A,目标服务器B. 1.在源 ...
- ComboBox在WPF中的绑定示例:绑定项、集合、转换,及其源代码
示例1.Selector(基类) 的示例Controls/SelectionControl/SelectorDemo.xaml <Page x:Class="Windows10.Con ...
- 使用kbmmw 的调度事件动态显示时间
kbmmw 里面提供了强大的事件调度功能,今天简单演示一个使用调度事件在窗体上显示时间. 建立一个新工程. 放上几个控件 在窗体里面引用单元 kbmMWScheduler, 然后添加过程 unit U ...