fork download
  1.  
  2. // Complexity O(log(n)+log(m))
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. #define f(i,x,y) for(int i = (x);i < (y);++i)
  7. #define F(i,x,y) for(int i = (x);i > (y);--i)
  8. int max(int a,int b){return (a > b?a:b);}
  9. int min(int a,int b){return (a < b?a:b);}
  10. int mod(int a){return (a > 0?a:((-1)*(a)));}
  11. #define INF 1000000
  12.  
  13.  
  14.  
  15. int func(int *arr1,int *arr2,int sz1,int sz2,int k)
  16. {
  17. if((k <= (sz1+sz2))&&(k > 0))
  18. {
  19. int s = 1,e,i,j;
  20. if(k > sz1)e = sz1+1;
  21. else e = k;
  22. while((e-s)>1)
  23. {
  24. i = (e+s)/2;
  25. j = ((k-1)-(i-1));
  26. j++;
  27. if(j > (sz2+1)){s = i;}
  28. else if((arr1[i] >= arr2[j-1])&&(arr1[i] <= arr2[j]))return arr1[i];
  29. else if((arr2[j] >= arr1[i-1])&&(arr2[j] <= arr1[i]))return arr2[j];
  30. else if(arr1[i] < arr2[j-1]){s = i;}
  31. else if(arr1[i] > arr2[j]){e = i;}
  32. else {;}
  33. }
  34. i = e,j = ((k-1)-(i-1));j++;
  35. if((arr1[i] >= arr2[j-1])&&(arr1[i] <= arr2[j]))return arr1[i];
  36. else if((arr2[j] >= arr1[i-1])&&(arr2[j] <= arr1[i]))return arr2[j];
  37. else
  38. {
  39. i = s,j = ((k-1)-(i-1));j++;
  40. if((arr1[i] >= arr2[j-1])&&(arr1[i] <= arr2[j]))return arr1[i];
  41. else return arr2[j];
  42. }
  43. }
  44. else
  45. {
  46. cout << "Data Invalid" << endl;
  47. return -INF;
  48. }
  49. }
  50.  
  51.  
  52.  
  53.  
  54. int main()
  55. {
  56. int n,m,k;
  57. cin >> n >> m >> k;
  58. int arr1[n+2];
  59. int arr2[m+2];
  60. f(i,1,n+1)
  61. cin >> arr1[i];
  62. f(i,1,m+1)
  63. cin >> arr2[i];
  64. arr1[0] = -INF;
  65. arr2[0] = -INF;
  66. arr1[n+1] = +INF;
  67. arr2[m+1] = +INF;
  68. int val = func(arr1,arr2,n,m,k);
  69. if(val != -INF)cout << val << endl;
  70. return 0;
  71. }
  72.  
Success #stdin #stdout 0s 16064KB
stdin
5
5
4
1 5 8 9 10
2 3 4 6 7
stdout
4