hdu 6058 Kanade's sum (计算贡献,思维)
题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和
思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o(k)的时间内找到这k个区间,然后就一直挂机,惨惨惨
感觉官方题解的思路就很棒啊:
我们只要求出对于一个数x左边最近的k个比他大的和右边最近k个比他大的,扫一下就可以知道有几个区间的k大值是x.=
我们考虑从小到大枚举x,每次维护一个链表,链表里只有>=x的数,那么往左往右找只要暴力跳k次,删除也是O(1)的。
时间复杂度:O(nk)
代码:
/** @xigua */
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <climits>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 5e5 + 5;
const int mod = 1e9 + 9;
const int mod2 = 1e9 + 7;
const int INF = 1e8 + 5;
const ll inf = 1e15 + 5;
const db eps = 1e-5;
const ll hp = 233333;
int a[maxn], pos[maxn], pre[maxn], nex[maxn];
ll pp[105], np[105]; void solve() {
int n, k; cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
pos[a[i]] = i;
pre[i] = i - 1;
nex[i] = i + 1;
}
pre[0] = -1, nex[n+1] = n + 2;
ll ans = 0;
for (int i = 1; i <= n; i++) {
int t1 = 0, t2 = 0;
int curp = pos[i];
for (int j = curp; j >= 0 && t1 <= k; j = pre[j]) {
pp[++t1] = j;
}
for (int j = curp; j <= n + 1 && t2 <= k; j = nex[j]) {
np[++t2] = j;
}
for (int j = 1; j <= t1 - 1; j++) {
if (k - j + 2 > t2) continue;
ll tmp = (pp[j] - pp[j+1]) * (np[k - j + 2] - np[k - j + 1]);
ans += tmp * i;
}
//删除当前节点 相当于链表
int tp = pre[curp], tn = nex[curp];
nex[tp] = tn, pre[tn] = tp;
}
cout << ans << endl;
} int main() {
int t = 1, cas = 1;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// init();
scanf("%d", &t);
while(t--) {
// printf("Case %d: ", cas++);
solve();
}
return 0;
}
hdu 6058 Kanade's sum (计算贡献,思维)的更多相关文章
- 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(模拟链表)
Kanade's sum Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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 —— 2017 Multi-University Training 3
Kanade's sum Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 【链表】2017多校训练三 HDU 6058 Kanade's sum
acm.hdu.edu.cn/showproblem.php?pid=6058 [题意] 给定一个排列,计算 [思路] 计算排列A中每个数的贡献,即对于每个ai,计算有ni个区间满足ai是区间中的第k ...
- HDU - 6058 Kanade's sum
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6058 /* 思路是:找出每个x为第k大的区间个数有多少 用pos[i]保存当前x的位置, ...
- 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 ...
- HDU6058 Kanade's sum(思维 链表)
Kanade's sum Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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 ...
随机推荐
- Tomcat之the jre_home environment variable is not defined correctly this environment variable is need
参考https://blog.csdn.net/qq_30507287/article/details/53981851 今天在服务器的tomcat上部署.war文件,双击startup闪退,然后在t ...
- Flutter实战视频-移动电商-12.首页_GridView类别导航制作
12.首页_GridView类别导航制作 首页导航区的制作 外面用一个gridview来写.里面单独提出来 新建导航组件 还是在home_page.dart里面写代码 新建一个静态的组件: 快捷键写组 ...
- POJ - 2796 Feel Good 单调递增栈+前缀和
Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations ...
- Python3.6 字典的内置方法
1.dict.clear(self) 删除字典内所有元素 2.dict.copy(self) 返回一个字典的浅复制,拷贝父对象,不会拷贝对象的内部的子对象 3.dict.fromkeys(self, ...
- C#项目中一些文件类型说明
designer.cs 是窗体设计器生成的代码文件,作用是对窗体上的控件做初始化工作 (在函数InitializeComponent()中)VS2003以前都把这部分代码放到窗体的cs文件中,由于这 ...
- E20180601-hm
trade-off n. 权衡; 交易;(不是商业方面的交易,而是“利”与“弊”的权衡) vertex n. 顶点; 最高点; <数>(三角形.圆锥体等与底相对的)顶; (三角形.多边形等 ...
- SourceTree配置BeyondCompare代码冲突解决工具
一.工具准备:SourceTree这个你得有.然后下载BeyondCompare(破解教程) 二.配置环境:SourceTree->工具->选项->比较,外部对比工具和合并工具都选择 ...
- webstrom打开多个项目,webstrom常用快捷键
1.webstrom打开多个项目 默认情况下一次只能打开一个项目,如果需要打开多个就按照下面的方法 File -> settings -> Directories -> Add Co ...
- TextBox禁用与启用的方法
$('#text1').textbox('enable');// 启用 $('#text1').textbox('disable');// 禁用 注:以上方法也适用于datebox等等
- Server.MapPath()相关
Server.MapPath()相关 1. Server.MapPath()介绍 Server.MapPath(string path)作用是返回与Web服务器上的指定虚拟路径相对应的物理文 ...