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 ...
随机推荐
- 存储引擎,MySQL中的数据类型及约束
存储引擎,MySQL中的数据类型及约束 一.存储引擎 1.不同的数据应该有不同的处理机制 2.mysql存储引擎 Innodb:默认的存储引擎,查询速度叫myisam慢,但是更安全 支持事务, ...
- 吴裕雄 python 机器学习——数据预处理包裹式特征选取模型
from sklearn.svm import LinearSVC from sklearn.datasets import load_iris from sklearn.feature_select ...
- 微信小程序GET 400 (Bad Request)解决方案
解决了接口的问题,接下来就是请求不正确,得不到数值 400 (Bad Request) 可以用这个方法:wx.request传入的对象参数中的head改一下 wx.request({ header: ...
- 传奇脚本:#AutoRun NPC SEC 参数说明
传奇脚本:#AutoRun NPC SEC 参数说明 SEC:按秒运行MIN:按分运行HOUR:按小时运行DAY:按天运行RunOnDay:按每天什么时候运行RUNONWEEK:按星期几及时间运行机器 ...
- Leader:这样的 Bug 你也写的出来???
Hello~各位读者新年好!不知道大家春节假期是否已延长,小黑哥刚接到通知,假期延长到 2 月 2 号,另外回去之后需要在家办公,自行隔离两周.还没试过在家办公,小黑哥就怕到时候生物钟还没调整过来,一 ...
- Tornado项目简单创建
Tornado是使用Python编写的一个强大的.可扩展的Web服务器.它在处理严峻的网络流量时表现得足够强健,但却在创建和编写时有着足够的轻量级,并能够被用在大量的应用和工具中. tornado技术 ...
- laravel 报错The Mix manifest does not exist.
这是因为我们在 resources/views/layouts/app.blade.php 中使用 mix() 方法,而我们还未运行 Laravel Mix 进行编译,找不到 mix-manifest ...
- 安卓开发:用ImageView放上图片后上下有间隙
想不到在我使用第一个UI控件ImageView时候就遇上了问题,简单使用ImageView如下: <ImageView android:layout_width="match_pare ...
- 创建Ajax兼容
var request = new XMLHttpRequest(); IE7以下: var request = new ActiveXObject("Microsoft.XMLHTTP&q ...
- Cocos纹理理解
原文:https://blog.csdn.net/u010223072/article/details/78287294 理论要点 要点一: 文件格式与像素格式的区别:文件格式是图像为了存储信息而使用 ...