結果

問題 No.1827 最長部分スーパーリッチ門松列列
コンテスト
ユーザー ygussany
提出日時 2022-01-28 22:26:23
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 752 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 238 ms
コンパイル使用メモリ 38,720 KB
最終ジャッジ日時 2026-02-22 08:46:46
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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