제출 #1104082

#제출 시각아이디문제언어결과실행 시간메모리
1104082Champ_NamanGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
5 / 100
1087 ms460 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define nl '\n'

inline void solve(){
	int n;
	cin>>n;
	string s;
	cin>>s;
	vector<int> a(n);
	for(int i=0; i<n; i++){
		if(s[i] == 'R') a[i] = 0;
		else if(s[i] == 'G') a[i] = 1;
		else a[i] = 2;
	}

	auto b = a;
	sort(b.begin(), b.end());

	int ans = 1e18;
	do{
		auto c = b;
		bool tf = true;
		for(int i=0; i<n-1; i++){
			if(c[i] == c[i+1]){
				tf = false;
				break;
			}
		}
		if(!tf) continue;

		int subans = 0;
		for(int i=0; i<n; i++){
			if(c[i] == a[i]) continue;

			tf = false;
			for(int j=i+1; j<n; j++){
				if(c[j] != a[i]) continue;
 
				for(int k=j; k>i; k--){
					swap(c[k], c[k-1]);
					subans++;
				}
				tf = true;
				break;
			}

			if(!tf){
				subans = 1e18;
				break;
			}
		}

		ans = min(ans, subans);
	}while(next_permutation(b.begin(), b.end()));

	cout<<(ans == 1e18 ? -1 : ans);
}

signed main(){
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);cout.tie(NULL);

	int t = 1;
	//cin>>t;
	while(t--) solve();

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...