Topic 1534: Small number of k in an array
time limit:2 seconds
Memory limit:128 MB
Special question: No
submitted:1524
Resolution:307
-
topic Description:
-
given two arrays of integers a and B. We add the element 22 in a and B to get the array C.
For example A is [1,2],b] [3,4]. Then the array c that is added by element 22 in A and B is [4,5,5,6]. The
now gives you arrays A and B, which are the numbers in the array C that are added by A and B 22, and the small number of K.
-
Input:
-
The input may contain multiple test cases.
For each test case, enter the first behavior of three integers m,n, K (1<=m,n<=100000, 1<= k <= n *m): The n,m represents the length of the array A and b that will be entered.
The next two lines, respectively, are the number of M and N, representing the elements in arrays A and B. The array element range is [0,1e9].
-
Output:
-
corresponding to each test case,
Outputs the small number of k in the array C obtained by adding the element 22 in a and B.
-
Sample input:
-
2 2 31 23 43 3 41 2 73 4 5
-
Sample output:
-
56
-
Source:
- Google face questions
True, Google interview problem really difficult, (⊙o⊙) ...
#include <stdio.h> #include <stdlib.h>long long m, N;long long K;long long a[100000], B[100000];long long i;int Compare (const void * p, const void * q) {return * (Long long *) P-* (Long long *) Q;} Long long cal (Long long mid) {//This Although and Cal2 have the same output, but do a lot of useless computation, will time out long I, J; A long long cnt = 0; j=0; for (I=0;i<m;++i) {j=0;//is initialized to 0 each time, restart the count while (J<n&&a[i]+b[j]<=mid) ++j; Cnt+=j; } return CNT;} Long Long Cal2 (long long mid) {/* This function can save unnecessary calculation value, a small to large traversal, B from large to small traversal can, if a[i]+b[j]<=mid, then at this time J for i+1 speaking, the scope is still some big, continue to decrement J , then, from start to finish, J only needs to be from n-1 to 0. */Long long I, J; A long long cnt = 0; j = n-1; For (i=0, i<m; ++i) {while (j>=0 && a[i]+b[j]>mid)--j; CNT + = (j + 1); } return CNT;} Long Long findkth (long long k) {long long min = a[0] + b[0]; Long Long max = a[m-1] + b[n-1]; Long long mid; Long long ans; while (min <= max) {mid = (Max-min)/2 + min; Long Long B=cal2 (mid); Long Long b=cal (mid);//useless computation too much, will time out if (k <= b) {max = mid-1; } else min = mid + 1; } return min;} int main (void) {while (scanf ("%lld%lld%lld", &m, &n, &k)! = EOF) {for (i=0; i<m; ++i) scanf ("%lld", &a[i]); for (i=0; i<n; ++i) scanf ("%lld", &b[i]); Qsort (A, M, sizeof (long long), compare); Qsort (B, N, sizeof (long long), compare); printf ("%lld\n", findkth (k)); } return 0;} /************************************************************** problem:1534 User:kirchhoff language:c Resu lt:accepted time:960 Ms Memory:3256 kb****************************************************************/
Nine degrees OJ 1534 K small number in the array