JZOJ 1078. 【GDOI2006】The Kth Element
\(\text{Problem}\)
给定一个整数序列 \(a[1..N]\),定义 \(sum[i][j]=a[i]+a[i+1]+...+a[j]\),将所有的 \(sum[i][j]\) 从小到大排序(其中 \(i,j\) 满足 \(1<=i<=j<=N\) ),得到一个长为 \(N*(N+1)/2\) 的序列,求该序列中的第 \(K\) 个元素。
\(\text{Solution}\)
非常经典的题
二分答案然后 \(check\)
\(\text{Code}\)
#include <cstdio>
#include <algorithm>
#include <iostream>
#define ls (p << 1)
#define rs (ls | 1)
#define RE register
#define IN inline
using namespace std;
typedef long long LL;
const int N = 2e4 + 5;
const LL INF = 1e18;
int n, m, len;
LL a[N], b[N], sum[N * 4];
void build(int p, int l, int r)
{
sum[p] = 0;
if (l == r) return;
int mid = l + r >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
}
void Modify(int p, int l, int r, int x, int v)
{
if (x > r || x < l) return;
if (l == r) return sum[p] += v, void();
int mid = l + r >> 1;
if (x <= mid) Modify(ls, l, mid, x, v);
else Modify(rs, mid + 1, r, x, v);
sum[p] = sum[ls] + sum[rs];
}
LL Query(int p, int l, int r, int x)
{
if (x > r || x < l) return 0;
if (l == r) return sum[p];
int mid = l + r >> 1;
if (x <= mid) return Query(ls, l, mid, x) + sum[rs];
return Query(rs, mid + 1, r, x);
}
IN int check(LL mid)
{
int res = 0;
build(1, 1, len), Modify(1, 1, len, lower_bound(b + 1, b + len + 1, 0) - b, 1);
for(RE int i = 1; i <= n; i++)
{
int x = lower_bound(b + 1, b + len + 1, a[i] - mid) - b;
res += Query(1, 1, len, x);
Modify(1, 1, len, lower_bound(b + 1, b + len + 1, a[i]) - b, 1);
}
return res >= m;
}
int main()
{
scanf("%d%d", &n, &m);
for(RE int i = 1; i <= n; i++) scanf("%lld", &a[i]), a[i] += a[i - 1], b[i] = a[i];
b[n + 1] = 0, sort(b + 1, b + n + 2), len = unique(b + 1, b + n + 2) - b - 1;
LL l = -INF, r = INF, mid, ans;
while (l <= r)
{
mid = (l + r) >> 1;
if (check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
printf("%lld\n", ans);
}
JZOJ 1078. 【GDOI2006】The Kth Element的更多相关文章
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- JZOJ 3223. 【HBOI2013】Ede的新背包问题
3223. [HBOI2013]Ede的新背包问题 (Standard IO) Time Limits: 2000 ms Memory Limits: 262144 KB Detailed Lim ...
- JZOJ 2137. 【GDKOI2004】城市统计 (Standard IO)
2137. [GDKOI2004]城市统计 (Standard IO) Time Limits: 1000 ms Memory Limits: 128000 KB Detailed Limits ...
- JZOJ 2136. 【GDKOI2004】汉诺塔
2136. [GDKOI2004]汉诺塔 (Standard IO) Time Limits: 3000 ms Memory Limits: 128000 KB Detailed Limits ...
- JZOJ 1154. 【GDOI2003】购物
1154. [GDOI2003]购物 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description GDOI商场推出优惠 ...
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...
- 【LeetCode】230. Kth Smallest Element in a BST (2 solutions)
Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...
随机推荐
- (Java)设计模式:行为型
前言 此篇博文内容续接的是 UML建模语言.设计原则.创建型设计模式 的内容,有兴趣的可以点前面的链接去看一下 3.2.行为型 这类设计模式是专门用于:对象间的高效沟通和职责委派 * 3.2.1.责任 ...
- day27-过滤器Filter02
Filter过滤器02 5.Filter过滤器生命周期 Filter生命周期图解 验证-Tomcat来创建Filter实例,只会创建一个实例 package com.filter; import ja ...
- linux学习相关资料整理
linux常用指令记录 Python3.9.9安装 supervisor安装与监控nginx 使用supervisor监控mysql supervisor监控tomcat配置文件 nginx-1.22 ...
- 【kafka】JDBC connector进行表数据增量同步过程中的源表与目标表时间不一致问题解决
〇.参考资料 一.现象 1.Oracle源表数据 2.PG同步后的表数据 3.现象 时间不一致,差了8个小时 4.查看对应的connector信息 (1)source { "connecto ...
- 【Java EE】Day07 HTML
一.WEB概念 1.软件架构 C/S:安卓.QQ.迅雷,开发两端 B/S 2.资源分类 静态资源:浏览器内置解析引擎 HTML:展示内容 CSS:页面布局 JavaScript:控制页面元素,产生动态 ...
- 【实时数仓】Day00:数据流程、课程内容、框架结构、知识点总结
一.数据流程 1.离线数仓 2.实时数仓 二.课程内容 1.数据采集层(ODS) 2.DWD层与DIM层数据准备 3.DWM层业务实现 4.DWS层业务实现 5.ClickHouse 6.数据可视化接 ...
- 全都会!预测蛋白质标注!创建讲义!解释数学公式!最懂科学的智能NLP模型Galactica尝鲜 ⛵
作者:韩信子@ShowMeAI 机器学习实战系列:https://www.showmeai.tech/tutorials/41 深度学习实战系列:https://www.showmeai.tech/t ...
- Vue DevUI v1.4 版本发布:从体验、效率、质量三个方面做了全方位的优化🎉
2022年9月1日,我们正式宣布 Vue DevUI 组件库发布 v1.0 版本. Vue DevUI 1.0 正式发布 经过100多天的持续迭代,我们正式发布 v1.4.0 版本,共新增: 11位贡 ...
- pyqt5制作俄罗斯方块小游戏-----源码解析
一.前言 最近学习pyqt5中文教程时,最后一个例子制作了一个俄罗斯方块小游戏,由于解释的不是很清楚,所以源码有点看不懂,查找网上资料后,大概弄懂了源码的原理. 二.绘制主窗口 将主窗口居中,且设置了 ...
- python Flask 操作数据库
Flask数据库 转载:Flask数据库 - 苦行僧95 - 博客园 (cnblogs.com) Flask-SQLAlchemy Flask-SQLAlchemy是在Flask中操作关系型数据库的拓 ...