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个 ...
随机推荐
- iOS -- xxxViewController进行pop时直接crash进main.m,EXC_BAD_ACCESS(code=1,address=0x20)
今天在调试程序时,遇到了奇怪的错误.我从主页跳进(push)一个ViewController时一切正常,但是返回主页(pop)时却crash了,直接跳进了main.m(EXC_BAD_ACCESS(c ...
- 彻底搞懂oracle的标量子查询
oracle标量子查询和自己定义函数有时用起来比較方便,并且开发者也常常使用.数据量小还无所谓.数据量大,往往存在性能问题. 下面測试帮助大家彻底搞懂标量子查询. SQL> create tab ...
- Cucumber+Rest Assured快速搭建api自动化测试平台
转载:http://www.jianshu.com/p/6249f9a9e9c4 什么是Cucumber?什么是BDD?这里不细讲,不懂的直接查看官方:https://cucumber.io/ 什么是 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- 英特尔和Red Hat合作实现Gnome桌面的Wayland支持
在发布支持XMir的Linux图形驱动程序xf86-video-intel 2.99.901后数天,英特尔宣布撤回对XMir的支持,XMir补丁不会合并到上游项目.XMir是Mir显示服务器的X11兼 ...
- oracle ORA-06550
declare cnt integer; begin select count(0) into cnt from user_all_tables where table ...
- ActiveMQ 消息持久化到Mysql数据库
[root@txylucky local]# tar -zxvf apache-activemq-5.15.8-bin.tar.gz[root@txylucky local]# mv apache-a ...
- 互联网金融MySQL优化参数标准
InnoDB配置 从MySQL 5.5版本开始,InnoDB就是默认的存储引擎并且它比任何其它存储引擎的使用要多得多.那也是为什么它需要小心配置的原因. innodb_file_per_table 表 ...
- CentOS 安装和配置 Mantis
Mantis是一个基于PHP技术的轻量级的开源缺陷跟踪系统,以Web操作的形式提供项目管理及缺陷跟踪服务.在功能上.实用性上足以满足中小型项目的管理及跟踪.更重要的是其开源,不需要负担任何费用. 1. ...
- Android自己定义控件--下拉刷新的实现
我们在使用ListView的时候.非常多情况下须要用到下拉刷新的功能.为了了解下拉刷新的底层实现原理,我採用自己定义ListView控件的方式来实现效果. 实现的基本原理是:自己定义ListView, ...