結果

問題 No.1425 Yet Another Cyclic Shifts Sorting
コンテスト
ユーザー 箱星
提出日時 2020-12-09 16:53:40
言語 C++17(gcc12)
(gcc 12.4.0 + boost 1.90.0)
コンパイル:
g++-12 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 1,274 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,350 ms
コンパイル使用メモリ 256,016 KB
実行使用メモリ 13,688 KB
最終ジャッジ日時 2026-06-15 20:36:51
合計ジャッジ時間 9,851 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <algorithm>
#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<int> 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<bool> is_sorted(n, false);	// a[i],...,a[n-1]がソートされているかどうか
	vector<int> decrease(n, 0);			// a[0],...,a[i]における減少回数
	vector<int> 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;
}
0