zcmu 1540 k-Tarsus

1540: The first large numbers k
Time Limit: 10 Sec Memory Limit: 128 MB
[the Submit] [the Status] [the Web Board]
the Description
There are two sequences a, b, their respective lengths n and m, then the two sequences What is the n * m elements corresponding to the elements in descending order obtained by multiplying the k-th element is the?

Input
of the first act input a positive integer T (T <= 10), T represents a total set of test data.

The first line of each test three positive integers n, m and k (1 <= n, m <= 100000,1 <= k <= n * m), representing the length of a sequence, sequence length b , and ask the element index. The second positive integer representing the behavior of n sequence a. The third positive integer representing the behavior of the m sequence b. The size of all the elements of the sequence satisfies [1,100000].

Output
For each test case, the output line comprises a large integer representing the k-th element is.

Sample Input
3
3 2 3
1 2 3
1 2
2 2 1
1 1
1 1
2 2 4
1 1
1 1
Sample Output
3
1
1

Obviously this question first of a, b sort the array, which is certain.
Then, l = a [0] * b [0], r = a [n-1] * b [m-1] are beginning to find several mid number
In addition to outside-half, where even the first of several large number of this idea will not be directly related to the TLE. Looking for is the first time a few large numbers. See specific comments section of the code.
When there is to be described in the second sub-time to pay attention not satisfying the condition when the mid returns the result directly, this time may be between the first mid k + 1 and k-th largest number of large numbers.
#include <cstdio> 
#include <algorithm>
 the using  namespace STD; 
 
int n-, m, K;
 Long  Long a [ 100000 ];
 Long  Long B [ 100000 ];
 
a number of ideas // o (m + n) is by a , b pairwise sequence c obtained in several numbers belonging
Long Long Judge ( Long Long MID) { int SUM = 0 ; int J = 0 ; for ( int I = N- . 1 ; I> = 0 ; I - ) // begins the enumeration of the maximum i, because a, b i are ordered when the situation after completion of the enumeration, the establishment of the subsequent enumeration in the case of a certain i-1 for (; J <m; J ++ ) // When a [i + 1] [j ] can not meet the time, a [i] [j] is not satisfied a certain, not repeated here enumerated, according to the previous wheel results recorded on the line iF (a [I] * B [j]> = MID) { SUM + M- = j; // j to satisfy the time when the subsequent result is satisfying certain BREAK ; } return SUM ; } int main () { int PP; Scanf ( " % D " , & PP); the while (PP - ) { Scanf ( " % D% D% D " , & n-, & m, & K); for ( int i =0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n); for(int i=0;i<m;i++)scanf("%d",&b[i]);sort(b,b+m); long long l=a[0]*b[0]; long long r=a[n-1]*b[m-1]; long long ans; while(l<=r) { long long mid=(r+l)/2; int sum=judge(mid); if(sum>=k) ans=mid,l=mid+1; else r=mid-1; } printf("%lld\n",ans); out:; } return 0; }

 

Guess you like

Origin www.cnblogs.com/z1141000271/p/11443331.html