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 ...
随机推荐
- python3获取列表逆序的五种方式
前言 我们将这几种方式分为两类,一种是对列表本身进行操作,改变对应内存中的值,另一种是带有返回值的,相当于拷贝了一份 对列表内存中进行操作 sort() 函数 a = [1,2,3,4] a.sort ...
- 【大数据-课程】高途-天翼云侯圣文-Day3-实时计算原理解析
〇.老师及课程介绍 一.今日内容 二.实时计算理论解析 1.什么是实时计算 微批处理.流式处理.实时计算 水流和车流的例子 spark streaming就是一种微批处理,水满了才处理,进入下一个地方 ...
- 《HTTP权威指南》– 6.代理
代理的概念: Web代理服务器是网络的中间实体.位于客户端和服务器之间,扮演"中间人"的角色,在各端点之间来回传送HTTP报文. 私有和共享代理: 代理服务器可以是某个客户端专用的 ...
- web项目部署上线(无虚拟主机,待学习)
购买阿里云服务器 阿里云服务器ECS 系统镜像使用Ubuntu 20.04 LTS 使用ssh连接服务器,终端或者CMD中执行:$ssh root@x.x.x(阿里云服务器账号名@公网地址) 输入账号 ...
- python 之定时任务(schedule)
import schedule import time def job(): print("定时通报...") # 定义一个叫job的函数,函数的功能是打印'定时通报...' sc ...
- Potree 001 Potree介绍
1.Potree是什么 Potree是一种基于WebGL的点云数据可视化解决方案,包含点云数据转化,以及进行可视化的源码.该解决方案的主要优势在于对点云数据进行了多尺度的管理,在数据传输和可视化上都做 ...
- 对Asp.net WebApi中异步(async+await)接口实际使用及相关思考(示例给出了get,post,提交文件,异步接口等实践).
[很多初学者的疑问] 为何作为web api这样的天然的并发应用,还需要在controller的action上声明使用async这些呢? <参考解答> 在 web 服务器上,.NET Fr ...
- 我的第一个自动刷作业脚本(大起大落的selenium经验分享)
起因 故事的开始是大二的上学期,有一门叫计算机结构(computer organization)的课.新教授这门课的教授在原来的政策上做了一些变动.他引入了一个叫做zybook的作业平台来确保我们能跟 ...
- ArcGIS工具 - 导出数据库结构
为了保证数据的一致性,数据库结构的正确性在数据库建设和管理过程中显示十分重要,在各个地理信息类项目的技术规定中都对空间数据库的结构进行明确和详细的定义,有时为了方便检查或文档编辑需要将数据结构导出,为 ...
- [Untiy]贪吃蛇大作战(四)——游戏主界面
游戏主界面: 由于这个场景比较复杂,需要分几个部分实现: 1.游戏背景 首先我们的游戏场景上包括了一个大的背景图片,之外再包围一个红色的区域.中间的区域才是可活动的区域,周围通过碰撞检测盒来检测是否有 ...