ABC155D - Pairs
本题的模型是典型的求第k小问题,这个问题有2个不一样的点,一是任意选出2个数,不能是同一个,二是这个题有负数,那我们在原有的基础上就需要特判这两点,经典模型是2个数组相乘,此处是1个,那么一样可以枚举每一个数,计算比该数小的数的数量,运用容斥,将重复的去掉即可,第一个问题就解决了,假设要判断的数是a,当前枚举到的数是b,第二个问题就是有0有负数有正数,b=0的情况很简单,若a大于0,那么一定所有的数乘a都小于a,直接累加即可,b=正数的情况就比较经典,二分查找比a/b小的数,累加即可,b=负数呢,那就反过来找呗,找比a/b大的数,用二分查找大于等于a/b的数,N-数量就是累加量,因为a是负的,负的乘比他小的负的一定变大,则b*比a/b大的数一定小于a,本题还要注意一个点,在a/b时,若是异号相除,取整时会出问题,需要特判各种情况,当b>0时,我们找的是正向的<=区间,直接对a/b向下取整就可以了,当b<0时,我们找的是反向的>区间,我们也需要"向下取整",但这里的b是负数,我们直接用floor取整会导致统计到的数变多,举个例子,7/3取整为2,2*3<7,floor(7/-3)=-3,-3*(-3)>7,那我们就要反过来向上取整,用ceil,这样保证取整后的数*b<a,总之就是取整后的数一定要保证*b<a,如果有疑问可以自己举几个例子,7/-3,6/-3.-6/4,-6/3等
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; const int maxm = 2e5+;
LL n, k; LL buf[maxm]; bool check(LL x) {
LL cnt = , i, j;
for(i = ; i < n; ++i) {
if(buf[i]*buf[i] <= x) cnt--;
if(buf[i] > ) {
j = floor((long double)x / buf[i]);
LL pos = upper_bound(buf, buf+n, j) - buf;
cnt += pos;
} else if(buf[i] < ) {
j = ceil((long double)x / buf[i]);
LL pos = lower_bound(buf, buf+n, j) - buf;
cnt += n - pos;
} else {
if(x >= ) cnt += n;
}
}
cnt /= ;
return cnt >= k;
} void run_case() {
cin >> n >> k;
for(int i = ; i < n; ++i) cin >> buf[i];
sort(buf, buf+n);
LL l = -(1LL<<), r = 1LL<<, ans, mid;
while(l <= r) {
mid = (l+r)>>;
if(check(mid)) {
ans = mid;
r = mid-;
} else
l = mid+;
}
cout << ans;
} int main() {
ios::sync_with_stdio(false), cin.tie();
//cout.setf(ios_base::showpoint);cout.precision(8);
run_case();
cout.flush();
return ;
}
ABC155D - Pairs的更多相关文章
- 题解 AT4867 【[ABC155D] Pairs】
题目 两次二分 首先对ans进行二分,在\([-10^{18},10^{18}]\)之间 考虑怎么check 对于每个ans,枚举每个\(a_i\),二分查找有几个\(a_j\),使得\(a_i\ti ...
- [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- Leetcode-24 Swap Nodes in Pairs
#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- 数论 - Pairs(数字对)
In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- Palindrome Pairs -- LeetCode 336
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- 336-Palindrome Pairs
336-Palindrome Pairs Given a list of unique words, find all pairs of distinct indices (i, j) in the ...
随机推荐
- 80端口被system占用
# 开始 今天配置wampserver 3.0.6的时候 发现右下角的图标一直是红色的 经验告诉我两个服务都没有运行 # 解决思路 wampserver有两个服务 一个是 Apache 服务 一个是 ...
- EF中 GroupJoin 与 Join
数据: GroupJoin: 返回左表所有数据 using (tempdbEntities context = new tempdbEntities()) { var query = context. ...
- 慕课网:剑指Java面试-Offer直通车视频课程
慕课网:剑指Java面试-Offer直通车视频课程,一共有10个章节. 目录结构如下: 目录:/2020036-慕课网:剑指Java面试-Offer直通车 [6G] ┣━━第10章 Java常用类库与 ...
- Maven学习笔记:Maven简介
Maven的概念 Maven是基于项目对象模型(POM,Project Object Model),可以通过描述信息来管理项目的构建,报告和文档的软件管理工具 Maven除了以程序构建能力为特色之外, ...
- 比较好的IT教程网
MDN最完整的Web标准,Webapi https://developer.mozilla.org/zh-CN/docs/Web 主要几个比较常用教程网都可以 gitbo ...
- 杭电2033 人见人爱A+B
人见人爱A+B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- Python学习第二十三课——Mysql 表记录的一些基本操作 (查)
查(select * from 表名) 基本语法: select <字段1,字段2,...> from <表名> where <表达式>; 例如,查询student ...
- 动态规划-国王的金矿问题java
紧接着上一篇动态规划问题,现在我们开始探讨一个新的问题,问:有一个发现了5个金矿,每一个金矿的储量不同,需要参与挖掘的工人数也不通,参与挖矿工人的总数量是10人,每一座金矿要么全挖,要么不挖,不能派一 ...
- 【Html 页面布局】
float:left方式布局 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /& ...
- 单元测试及框架简介 --junit、jmock、mockito、powermock的简单使用
转 单元测试及框架简介 --junit.jmock.mockito.powermock的简单使用 2013年08月28日 14:33:06 luvinahlc 阅读数:6413 标签: 测试工具单元测 ...