本题的模型是典型的求第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. linux 命令——screen

    最近遇到一个东西aria2,这个玩意,这个是啥呢?Aria2是一个轻量级Linux下载软件,支持HTTP/HTTPS, FTP, SFTP, BitTorrent和磁力链接(官方版),公司系统插件配套 ...

  2. spring中@Component注解

    1.@controller 控制器(注入服务) 2.@service 业务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...

  3. [代码记录] C语言链表

    #开始 · 简单的创建链表 #代码 #include<conio.h> #include<stdio.h> #include<stdlib.h> struct St ...

  4. C# 酒店管理系统知识点

    identity (m,n)自增 m开始n每次增加的值  默认(1,1) 列名  数据类型  约束  identity(m,n) 重新设置identity的值 1.语法 dbcc checkident ...

  5. layui 延时加载

    //延时关闭当前页面,并刷新父页面layer.msg('提交成功',{time: 1800},function () { parent.layer.close(index); window.paren ...

  6. pycharm新建Django时,遇到的坑,安装index包失败

    https://blog.csdn.net/li93675/article/details/89418097 如果在pycharm中导入django包 ,只对当前项目有效,建议使用命令pip inst ...

  7. centos610无桌面安装JDK

     Centos610系列配置 1.使用yum查找jdk: yum search java|grep jdk    2.选择安装截图中选中的版本 yum install java-1.8.0-openj ...

  8. .NET中的字符串(4):字符串 - 特殊的引用类型

    字符串驻留 看一下这段代码: 1using System; 2 3namespace Demo4 4{ 5 /**//// <summary> 6 /// String的驻留 7 /// ...

  9. Jmeter进行分布式性能测试

    由于Jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起JAVA内存溢出的错误.要解决这个问题,可以使用分布式测试,运行多台机器运行所谓的 ...

  10. Jmeter在linux下的安装

    Apache Jmeter简介   Apache JMeter 是Apache组织的开放源代码项目,是一个100%纯Java桌面应用,用于压力测试和性能测量.它最初被设计用于Web应用测试但后来扩展到 ...