Advertisement
rjlth

Untitled

Nov 9th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. const int N = 2000 * 1000 + 3000;
  2.  
  3. int n;
  4. int a[N];
  5.  
  6. inline bool read() {
  7. n = nextInt();
  8. forn(i, n) {
  9. a[i] = nextInt();
  10. }
  11. return true;
  12.  
  13. }
  14.  
  15. int cnt[N];
  16. int curVal;
  17.  
  18. inline int getsum(int l, int r) {
  19. if (l > r) return 0;
  20.  
  21. if (r >= N) {
  22. r = N - 1;
  23. }
  24.  
  25. int res = cnt[r];
  26.  
  27. if (l > 0) res -= cnt[l - 1];
  28. return res;
  29. }
  30.  
  31. inline bool f(int mid) {
  32. for(int i = curVal; i < N / 2; i += curVal) {
  33. int curpos = i + mid;
  34. if (getsum(curpos, i + curVal - 1) > 0)
  35. return true;
  36. }
  37.  
  38. return false;
  39. }
  40.  
  41. inline void solve() {
  42. forn(i, n) {
  43. cnt[a[i]] ++;
  44. }
  45. forn(i, N) {
  46. if (i > 0) {
  47. cnt[i] += cnt[i - 1];
  48. }
  49. }
  50.  
  51. sort(a, a + n);
  52. n = int(unique(a, a + n) - a);
  53.  
  54. int ans = 0;
  55.  
  56.  
  57. forn(i, n) {
  58. curVal = a[i];
  59.  
  60. int l = 0;
  61. int r = a[i];
  62.  
  63. while (r - l > 1) {
  64. int mid = (r + l) >> 1;
  65.  
  66. if (f(mid)) {
  67. l = mid;
  68. } else {
  69. r = mid;
  70. }
  71. }
  72.  
  73. ans = max(ans, l);
  74. }
  75.  
  76. cout << ans << endl;
  77. }
  78.  
  79. int main()
  80. {
  81.  
  82. #ifdef gridnevvvit
  83. freopen("input.txt", "rt", stdin);
  84. freopen("output.txt", "wt", stdout);
  85. #endif
  86.  
  87. //srand(time(NULL));
  88.  
  89. cout << setprecision(10) << fixed;
  90. cerr << setprecision(5) << fixed;
  91.  
  92. assert(read());
  93. solve();
  94.  
  95. #ifdef gridnevvvit
  96. cerr << "===Time: " << clock() << "===" << endl;
  97. #endif
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement