#include #include #include #include "testlib.h" using namespace std; int main(int argc, char* argv[]) { registerValidation(argc, argv); int n = inf.readInt(2, 200000, "n"); inf.readEoln(); vector a(n, 0); for (int i = 0; i < n; i++) { a[i] = inf.readInt(1, 1000000000, "a_i"); if (i != n - 1) { inf.readSpace(); } } inf.readEoln(); inf.readEof(); vector is_sorted(n, false); // a[i],...,a[n-1]がソートされているかどうか vector decrease(n, 0); // a[0],...,a[i]における減少回数 vector m(n, 0); // a[0],...,a[i]の最大値 is_sorted[n - 1] = true; for (int i = n - 2; i >= 0; i--) { if (a[i] <= a[i + 1]) { is_sorted[i] = true; } else { break; } } if (is_sorted[0]) { cout << 0 << endl; return 0; } for (int i = 1; i < n; i++) { decrease[i] = decrease[i - 1]; if (a[i - 1] > a[i]) { decrease[i]++; } m[i] = max(m[i - 1], a[i]); } for (int i = 0; i < n - 1; i++) { if (decrease[i] == 1 && a[i] <= a[0] && is_sorted[i + 1] && m[i] <= a[i + 1]) { cout << 1 << endl; return 0; } } if (decrease[n - 1] == 1 && a[n - 1] <= a[0]) { cout << 1 << endl; return 0; } cout << 2 << endl; }