結果

問題 No.1827 最長部分スーパーリッチ門松列列
ユーザー 👑 ygussanyygussany
提出日時 2022-01-28 22:26:23
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 752 bytes
コンパイル時間 950 ms
コンパイル使用メモリ 28,936 KB
実行使用メモリ 5,820 KB
最終ジャッジ日時 2023-08-28 20:34:00
合計ジャッジ時間 3,671 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 20 ms
4,376 KB
testcase_05 AC 20 ms
4,376 KB
testcase_06 AC 21 ms
4,376 KB
testcase_07 AC 61 ms
5,800 KB
testcase_08 AC 60 ms
5,728 KB
testcase_09 AC 60 ms
5,712 KB
testcase_10 AC 60 ms
5,728 KB
testcase_11 AC 60 ms
5,640 KB
testcase_12 AC 60 ms
5,660 KB
testcase_13 AC 60 ms
5,676 KB
testcase_14 AC 59 ms
5,680 KB
testcase_15 AC 59 ms
5,588 KB
testcase_16 AC 59 ms
5,684 KB
testcase_17 AC 67 ms
5,640 KB
testcase_18 AC 68 ms
5,676 KB
testcase_19 AC 64 ms
5,628 KB
testcase_20 AC 64 ms
5,640 KB
testcase_21 AC 64 ms
5,660 KB
testcase_22 AC 62 ms
5,820 KB
testcase_23 AC 62 ms
5,752 KB
testcase_24 AC 62 ms
5,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

void chmax(int* a, int b)
{
	if (*a < b) *a = b;
}

int solve(int N, int P[])
{
	int i;
	static int P_inv[500001];
	for (i = 1; i <= N; i++) P_inv[P[i]] = i;
	
	int j, ans = 1, tmp = 1;
	for (i = 1; i <= N; i++) {
		j = P_inv[i];
		if (j == 1) {
			if (P[j] > P[j+1]) tmp--;
			else tmp++;
		} else if (j == N) {
			if (P[j-1] < P[j]) tmp--;
			else tmp++;
		} else {
			if (P[j-1] < P[j] && P[j] > P[j+1]) tmp -= 2;
			else if (P[j-1] > P[j] && P[j] < P[j+1]) tmp += 2;
		}
		chmax(&ans, tmp);
	}
	return ans;
}

int main()
{
	int i, t, T, N, P[500001];
	scanf("%d", &T);
	for (t = 1; t <= T; t++) {
		scanf("%d", &N);
		for (i = 1; i <= N; i++) scanf("%d", &(P[i]));
		printf("%d\n", solve(N, P));
	}
	fflush(stdout);
	return 0;
}
0