本题的模型是典型的求第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的更多相关文章

  1. 题解 AT4867 【[ABC155D] Pairs】

    题目 两次二分 首先对ans进行二分,在\([-10^{18},10^{18}]\)之间 考虑怎么check 对于每个ans,枚举每个\(a_i\),二分查找有几个\(a_j\),使得\(a_i\ti ...

  2. [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 ...

  3. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  4. 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 ...

  5. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  6. 数论 - Pairs(数字对)

    In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...

  7. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  8. 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 ...

  9. 336-Palindrome Pairs

    336-Palindrome Pairs Given a list of unique words, find all pairs of distinct indices (i, j) in the ...

随机推荐

  1. 台电X16pro刷机记录

    Android: 如果要刷安卓,需要使用win7系统电脑,且需要安装java环境,同时按住 音量减+电源键进入刷机模式(DNX BOOT MODE..),这时在PhoneFlashTool_5.3.2 ...

  2. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  3. DAG 计数

    以下方法都是按照这个套路: 考虑一个不一定连通的 DAG ,你枚举一些零度点(拓扑序第一层)集合 \(S\),这些点之间不能连边,这些点跟剩下点(点集为 \(T\),\(|S| \cup |T| = ...

  4. JS-防抖与节流

    问题的由来:一些事件频繁的被触发而导致频繁的调用事件处理程序,从而造成程序不必要的开销,影响程序性能:防抖和节流就是为了解决这种情况造成的性能消耗. 场景1:使用keyup事件监听输入框的值进行请求搜 ...

  5. 解决 IDEA 下 struts.xml 中 extends="struts-default" 报红的问题

    现象 在IDEA中配置struts.xml时 extends="struts-default" 报红,配置拦截器时属性无预选项提示,也爆红. struts.xml本身的配置并没有错 ...

  6. iOS中的主要框架framework

    在日常的iOS项目开发中,主要使用的就是Foundation和UIKit这两个框架. (一)Foundation框架 Foundation是对Core Foundation框架的一个封装,使用Foun ...

  7. Linux - 命令 - top命令

    负载检查:https://blog.csdn.net/HANLIPENGHANLIPENG/article/details/79172053 参考:https://blog.csdn.net/gxia ...

  8. CSS——textarea多行文本框禁止拖动问题解决

    文本框这样配置就好 textarea{ resize:none; }

  9. fastJson javaBean和JSON对象相互转换

    fastjson的作用就是把java 对象转化为字符串,把字符串转化为java对象,然后方便进行后续的逻辑处理. java对象和json互相转换都是通过JSON对象操作的: JavaBean bean ...

  10. Python学习第二十二课——Mysql 表记录的一些基本操作 (增删改)

    记录基本操作: 增:(insert into) 基本语法: insert into 表名(字段) values(对应字段的值): 例子1: insert into employee(id,name,a ...