1105 第K大的數

2021-08-14 06:00:11 字數 1400 閱讀 4188

陣列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個數。

input

第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)

output

輸出第k大的數。
input示例

3 2

1 22 3

3 4

output示例

9

題解:二分列舉答案,每次檢查比該數大的數的個數。原先聽過別人說過類似的題目,就一直往那方面想,結果沒有絲毫頭緒,到頭來還是二分方便。

**:

#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;

typedef long long ll;

typedef pairp;

const int inf = 0x3f3f3f3f;

const ll linf = 0x3f3f3f3f3f3f3f3f;

const double pi = acos(-1.0);

const double eps = 1e-10;

const int maxn = 1e6+7;

const int mod = 1e9+7;

int n,k;

ll a[maxn],b[maxn];

ll check(ll now)

if(j>n) continue;

else res+=(n-j+1);

}return res;

}int main()

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

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

ll r = a[n]*b[n],l = a[1]*b[1];

while(r-l>1)

else r = mid;

}printf("%lld\n",l);

return 0;

}

經驗:

1、考慮到第幾大問題,二分權值線段樹主席樹。

2、資料範圍大,log級別,也應當考慮二分。

1105 第K大的數

陣列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個數。...

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大的數

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