• LQDOJ
  • Trang chủ
  • Bài tập
  • Bài nộp
  • Thành viên
  • Kỳ thi
  • Nhóm
  • Giới thiệu
    • Máy chấm
    • Khóa học
    • Đề xuất ý tưởng
    • Đề xuất bài tập
    • Tools
    • Báo cáo tiêu cực
    • Báo cáo lỗi

Tiếng Việt

Tiếng Việt
English

Đăng nhập

Đăng ký

buithithuypy83

  • Giới thiệu
  • Bài tập
  • Bài nộp

Rating
-
Bài tập
1
Điểm
301
Rating #
-
Điểm #
27081

Giới thiệu

include <iostream>

include <vector>

include <climits>

using namespace std;

int main() {
int n;
cin >> n;
vector<int> a(n);
int minVal = INT_MAX, maxVal = INT_MIN;

for (int i = 0; i < n; ++i) {
    cin >> a[i];
    minVal = min(minVal, a[i]);
    maxVal = max(maxVal, a[i]);
}

int lastMin = -1, lastMax = -1;
int res = n + 1;

for (int i = 0; i < n; ++i) {
    if (a[i] == minVal) {
        lastMin = i;
        if (lastMax != -1)
            res = min(res, i - lastMax + 1);
    }
    if (a[i] == maxVal) {
        lastMax = i;
        if (lastMin != -1)
            res = min(res, i - lastMin + 1);
    }
}

cout << res << endl;
return 0;

}


«    »
Thứ 2
Thứ 3
Thứ 4
Thứ 5
Thứ 6
Thứ 7
CN
Ít
Nhiều

proudly powered by DMOJ| developed by LQDJudge team