結果

問題 No.3062 Rotate and Maximize
ユーザー 沙耶花
提出日時 2025-03-14 22:39:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,024 bytes
コンパイル時間 4,485 ms
コンパイル使用メモリ 253,516 KB
実行使用メモリ 7,328 KB
最終ジャッジ日時 2025-03-14 22:39:43
合計ジャッジ時間 6,951 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000000
#define Inf64 1000000000000000001LL

int main(){
	
	int n;
	cin>>n;
	
	vector<int> a(n);
	vector<vector<int>> pos(n);
	rep(i,n){
		cin>>a[i];
		a[i]--;
		pos[a[i]].push_back(i);
	}
	if(pos[n-1].size()==0){
		cout<<0<<endl;
		return 0;
	}
	
	vector<int> ngs(n,0);
	rep(i,n-1){
		rep(j,pos[i].size()){
			rep(k,pos[n-1].size()){
				int x = pos[i][j] - pos[n-1][k];
				if(x<0)x+=n;
				ngs[x]++;
			}
		}
	}
	mint ans = n;
	int used = 0;
	ngs[0] = 100;
	for(int i=n-2;i>=0;i--){
		vector<int> t(n);
		rep(j,pos[i].size()){
			rep(k,pos[n-1].size()){
				int x = pos[i][j] - pos[n-1][k];
				if(x<0)x+=n;
				ngs[x]--;
				t[x]++;
			}
		}
		int C = 0;
		rep(j,n){
			if(t[j]==pos[i].size() && ngs[j]==0)C++;
		}
		if(pos[i].size()>0)ans *= C;
		else ans *= C - (n-2-i);
	}
	cout<<ans.val()<<endl;
	return 0;
}
0