51Nod 1105 第K大的數 ( 二分

2021-08-19 23:13:23 字數 2112 閱讀 8630

陣列a和陣列b,裡面都有n個整數。陣列c共有n^2個整數,分別是a[0] * b[0],a[0] * b[1] ……a[1] * b[0],a[1] * b[1]……a[n - 1] * b[n - 1](陣列a同陣列b的組合)。求陣列c中第k大的數。

例如:a:1 2 3,b:2 3 4。a與b組合成的c包括2 3 4 4 6 8 6 9 12共9個數。

第1行:2個數n和k,中間用空格分隔。n為陣列的長度,k對應第k大的數。(2 <= n <= 50000,1 <= k <= 10^9)

第2 - n + 1行:每行2個數,分別是a[i]和b[i]。(1 <= a[i],b[i] <= 10^9)

輸出第k大的數。

input示例32

1223

34output示例

9

排序後二分即可

#pragma comment(linker, "/stack:1024000000,1024000000")

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using

namespace

std;

#define ls st<<1

#define rs st<<1|1

#define fst first

#define snd second

#define mp make_pair

#define pb push_back

#define ll long long

#define pii pair

#define vi vector

#define clr(a,b) memset(a, (b), sizeof(a))

#define all(x) x.begin(),x.end()

#define ber(i,s,e) for(int i=(s); i<=(e); i++)

#define rep(i,s,e) for(int i=(s); i>=(e); i--)

const

int inf = 0x3f3f3f3f;

const

int maxn = 1e5+10;

const

int mod = 1e9+7;

const

double eps = 1e-8;

void fe()

ll read()

while (ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();

return x*f;

}ll arr[maxn], brr[maxn];

ll n, k;

ll check(ll x)

sum += j;

}return sum;

}int main()

sort(arr+1,arr+n+1);

sort(brr+1,brr+1+n);

ll l = arr[1]*brr[1], r = arr[n]*brr[n];

ll p = n*n-k+1;

ll mid, ans=0;

while(l <= r)

if(check(r+1) >= p)

r++;

cout

<< r << endl;

return

0;}

51Nod 1105 第K大的數

acm模版 這裡使用二分套二分查詢即可。一般的二分查詢是通過下標範圍查詢,而二分套二分是為了求兩個陣列組合乘積的問題,查詢第k大的值,這裡我們需要通過資料的範圍查詢,而不是下標的範圍,這裡需要兩次快排。需要強調的一點是資料範圍問題!一定要使用long long型,避免資料溢位!include inc...

51nod 1105 第K大的數

1105 第k大的數 基準時間限制 1 秒 空間限制 131072 kb 分值 40 難度 4級演算法題 陣列a和陣列b,裡面都有n個整數。陣列c共有n 2個整數,分別是a 0 b 0 a 0 b 1 a 1 b 0 a 1 b 1 a n 1 b n 1 陣列a同陣列b的組合 求陣列c中第k大的數...

51nod 1105第K大的數

1105 第k大的數 基準時間限制 1 秒 空間限制 131072 kb 分值 40 難度 4級演算法題 陣列a和陣列b,裡面都有n個整數。陣列c共有n 2個整數,分別是 a 0 b 0 a 0 b 1 a 0 b n 1 a 1 b 0 a 1 b 1 a 1 b n 1 a n 1 b 0 a ...