fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int getVal(std::vector<int> v, int i) {
  6. if (i == -1)
  7. return INT_MIN;
  8. if (i == v.size())
  9. return INT_MAX;
  10. return v[i];
  11. }
  12.  
  13. int getIndices(int rShort, std::vector<int> aShort, std::vector<int> aLong) {
  14. int midIndex = (aShort.size() + aLong.size()) / 2;
  15. int rLong = midIndex - rShort ;
  16. return rLong;
  17. }
  18.  
  19.  
  20. int getDirection(int lShort, int rShort, int lLong, int rLong, vector<int> aShort, vector<int> aLong) {
  21. if (getVal(aShort, lShort) > getVal(aLong, rLong))
  22. return -1;
  23.  
  24. if (getVal(aLong, lLong) > getVal(aShort, rShort))
  25. return 1;
  26.  
  27. return 0;
  28. }
  29.  
  30. double getResult(int lShort, int rShort, int lLong, int rLong, vector<int> aShort, vector<int> aLong) {
  31. int odd;
  32. odd = (aShort.size() + aLong.size()) % 2;
  33. if (odd) {
  34. return min(getVal(aShort, rShort), getVal(aLong, rLong));
  35. }
  36. return min(max(getVal(aShort, lShort), getVal(aLong, lLong)) , min(getVal(aShort, rShort), getVal(aLong, rLong)));
  37. }
  38.  
  39. int main()
  40. {
  41. #ifndef ONLINE_JUDGE
  42. freopen("input.txt", "r", stdin);
  43. freopen("output.txt", "w", stdout);
  44. #endif
  45.  
  46. int n;
  47. cin >> n;
  48. if(n <= 0) return 0;
  49. vector<int> aShort, aLong;
  50. for (int i = 0; i < n; i++) {
  51. int j;
  52. cin >> j;
  53. aShort.push_back(j);
  54. }
  55. for (int i = 0; i < n; i++) {
  56. int j;
  57. cin >> j;
  58. aLong.push_back(j);
  59. }
  60.  
  61. int lShort = 1, rShort = 1, lLong = 1, rLong = 1, d = 1, l = 0, r = n;
  62. int m = 0;
  63.  
  64. while (d != 0) {
  65. m = l + (r - l) / 2;
  66. rShort = m;
  67. rLong = getIndices(m, aShort, aLong);
  68. lLong = rLong - 1;
  69. lShort = rShort - 1;
  70. d = getDirection(lShort, rShort, lLong, rLong, aShort, aLong);
  71. if (d < 0)
  72. r = m - 1;
  73. if (d > 0)
  74. l = m + 1;
  75. }
  76.  
  77. int result = getResult(lShort, rShort, lLong, rLong, aShort, aLong);
  78. cout << result << endl;
  79.  
  80. return 0;
  81.  
  82. }
Success #stdin #stdout 0s 4368KB
stdin
Standard input is empty
stdout
1