fork download
  1. //Sub2;-;
  2. #include<bits/stdc++.h>
  3. #define ull unsigned long long
  4. #define ll long long
  5. #define all(x) x.begin(), x.end()
  6. using namespace std;
  7. const int maxn = 1e6 + 5;
  8. int A[maxn];
  9. map<int, vector<int>> mp;
  10. int main() {
  11. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  12. int n;
  13. int ma = INT_MIN, mi = INT_MAX;
  14. cin >> n;
  15. for (int i = 1; i <= n; ++i) {
  16. cin >> A[i];
  17. ma = max(ma, A[i]);
  18. mi = min(mi, A[i]);
  19. }
  20. int res = INT_MAX;
  21. if (ma == mi) {
  22. for (int i = 1; i <= n; ++i) {
  23. if (A[i] == ma) {
  24. mp[ma].push_back(i);
  25. }
  26. }
  27. for (int i = 1; i < mp[ma].size(); ++i) {
  28. res = min(res, mp[ma][i] - mp[ma][i - 1]);
  29. }
  30. }
  31. else {
  32. for (int i = 1; i <= n; ++i) {
  33. if (A[i] == ma) {
  34. mp[ma].push_back(i);
  35. }
  36. if (A[i] == mi) {
  37. mp[mi].push_back(i);
  38. }
  39. }
  40. for (int i = 0; i < mp[ma].size(); ++i) {
  41. int it = lower_bound(mp[mi].begin(), mp[mi].end(), mp[ma][i]) - mp[mi].begin();
  42. if (it != mp[mi].size()) {
  43. res = min(res, abs(mp[mi][it] - mp[ma][i] + 1));
  44. }
  45. }
  46. for (int i = 0; i < mp[mi].size(); ++i) {
  47. int it = lower_bound(mp[ma].begin(), mp[ma].end(), mp[mi][i]) - mp[ma].begin();
  48. if (it != mp[ma].size()) {
  49. res = min(res, abs(mp[ma][it] - mp[mi][i] + 1));
  50. }
  51. }
  52. }
  53. cout << res;
  54.  
  55. }
  56.  
  57.  
Success #stdin #stdout 0.01s 5284KB
stdin
8
1 2 3 4 5 6 7 8
stdout
8