fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdio>
  4. #include <iomanip>
  5. #include <sstream>
  6. #include <cstring>
  7. #include <string>
  8. #include <cmath>
  9. #include <stack>
  10. #include <list>
  11. #include <queue>
  12. #include <deque>
  13. #include <set>
  14. #include <map>
  15. #include <vector>
  16. #include <algorithm>
  17. #include <numeric>
  18. #include <utility>
  19. #include <functional>
  20. #include <limits>
  21. using namespace std;
  22.  
  23. typedef long long ll;
  24. typedef unsigned long long ull;
  25. typedef unsigned int ui;
  26. typedef pair<int,int> pii;
  27. typedef vector<vector<int> > graph;
  28.  
  29. const double pi = acos(-1.0);
  30.  
  31. #define oned(a, x1, x2) { cout << #a << ":"; for(int _i = (x1); _i < (x2); _i++){ cout << " " << a[_i]; } cout << endl; }
  32. #define twod(a, x1, x2, y1, y2) { cout << #a << ":" << endl; for(int _i = (x1); _i < (x2); _i++){ for(int _j = (y1); _j < (y2); _j++){ cout << (_j > y1 ? " " : "") << a[_i][_j]; } cout << endl; } }
  33.  
  34. #define mp make_pair
  35. #define pb push_back
  36. #define fst first
  37. #define snd second
  38.  
  39. const int MAXN = 200005;
  40.  
  41. int n, x[MAXN];
  42.  
  43. int mxl[MAXN], mxr[MAXN];
  44.  
  45. void solve() {
  46. sort(x,x+n);
  47.  
  48. int ans;
  49. if(n%2 == 0) {
  50. ans = 2*1e9;
  51. for(int i = 0; i < n/2; i++) {
  52. ans = min(ans,x[i+n/2]-x[i]);
  53. }
  54. } else {
  55. mxl[1] = x[1]-x[0];
  56. for(int i = 3; i < n; i += 2) {
  57. mxl[i] = max(mxl[i-2],x[i]-x[i-1]);
  58. }
  59. mxr[n-2] = x[n-1]-x[n-2];
  60. for(int i = n-4; i >= 0; i -= 2) {
  61. mxr[i] = max(mxr[i+2],x[i+1]-x[i]);
  62. }
  63. ans = min(mxl[n-2],mxr[1]);
  64. for(int i = 2; i+2 < n; i += 2) {
  65. ans = min(ans,max(mxl[i-1],mxr[i+1]));
  66. }
  67. }
  68. printf("%d\n", ans);
  69. }
  70.  
  71. int main() {
  72. // freopen("input.in", "r", stdin);
  73. while(scanf("%d", &n)==1) {
  74. for(int i = 0; i < n; i++) {
  75. scanf("%d", x+i);
  76. }
  77. solve();
  78. }
  79. }
Success #stdin #stdout 0s 5760KB
stdin
5
1 2 50 99 101
stdout
2