답안 #1089344

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1089344 2024-09-16T10:17:56 Z vjudge1 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
20 / 100
500 ms 796 KB
//don't copy pls)
/*TAAK ZDES NADO RECURSIU PISAT*/

//I'm not in the danger i am the DANGER
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
// #define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define sigma signed
using namespace std;
using namespace __gnu_pbds;
const int N = 3e5 + 5;
int mod = 1e9 + 7;
const int INF = 1e9;
int n;
int cnt1 = 0 , cnt2 = 0 , cnt3 = 0;
string s,t;
vector <string> q;
void rec(char last , int sz){
	if(sz == n){
		// cout << t << '\n';
		q.pb(t);
		return;
	}
	int ans = 0;
	if(last != 'R' && cnt1){
		t.pb('R');
		// cout << t << '\n';
		cnt1--;
		rec('R' , sz + 1);
		cnt1++;
		t.pop_back();
	}
	if(last != 'G' && cnt2){
		t.pb('G');
		// cout << t << '\n';
		cnt2--;
		rec('G' , sz + 1);
		cnt2++;
		t.pop_back();
	}
	if(last != 'Y' && cnt3){
		t.pb('Y');
		// cout << t << '\n';
		cnt3--;
		rec('Y' , sz + 1);
		cnt3++;
		t.pop_back();
	}
}
void Gold(){
	cin >> n >> s;
	int ans = 0 , ans1 = INF;
	for(auto it : s){
		if(it == 'R') cnt1++;
		else if(it == 'G') cnt2++;
		if(it == 'Y') cnt3++;
	}
	if(cnt1 + cnt2 == n){
		s = '+' + s;
		t = s;
		if(abs(cnt1 - cnt2) > 1){
			cout << "-1\n";
			return;
		}
		// cout << cnt1 << ' ' << cnt2 << '\n';
		if(cnt1 >= cnt2){
			for(int i = 1 ; i <= n ; i++){
				if(i % 2){
					for(int j = i ; j <= n ; j++){
						if(s[j] == 'R'){
							ans += (j - i);
							swap(s[i] , s[j]);
							// cout << i << ' ' << j << '\n';
							break;
						}
					}
				}
				else{
					for(int j = i ; j <= n ; j++){
						if(s[j] == 'G'){
							ans += (j - i);
							swap(s[i] , s[j]);
							break;
						}
					}
				}
			}
			ans1 = ans;
		}
		if(cnt2 >= cnt1){
			ans = 0;
			s = t;
			for(int i = 1 ; i <= n ; i++){
				if(i % 2 == 0){
					for(int j = i ; j <= n ; j++){
						if(s[j] == 'R'){
							ans += (j - i);
							swap(s[i] , s[j]);
							break;
						}
					}
				}
				else{
					for(int j = i ; j <= n ; j++){
						if(s[j] == 'G'){
							ans += (j - i);
							swap(s[i] , s[j]);
							break;
						}
					}
				}
			} 	
		}
		cout << min(ans1 , ans);
		return;
	}

	// cout << cnt1 << ' ' << cnt2 <<  ' ' << cnt3 << '\n';
	// rec('A' , 0);
	string pp = s;
	sort(all(pp));
	do{
		bool oknt = 0;
		for(int i = 0 ; i < n - 1 ; i++){
			if(pp[i] == pp[i + 1]){
				oknt= 1;
				break;
			}
		}
		if(!oknt){
			q.pb(pp);	
		}
	}while(next_permutation(all(pp)));
	ans = INF;
	s = '+' + s;
	string qq = s;
	for(auto it : q){
		s = qq;
		int ans1 = 0;
		it = '=' + it;
		// cout << it << '\n';
		for(int i = 1 ; i <= n ; i++){
			bool ok = 0;
			for(int j = i ; j <= n ; j++){
				if(it[i] == s[j]){
					ans1 += (j - i);
					for(int p = j ; p > i ; p--){
						swap(s[p] , s[p - 1]);
					}
					// cout << "ss = " << s << '\n';
					ok = 1;
					break;
				}
			}
			if(!ok){
				ans1 = INF;
				break;
			}
		}
		// cout << ans1 << '\n';
		ans = min(ans , ans1);
	}
	if(ans == INF){
		cout << "-1\n";
		return;
	}
	cout << ans ;
}
sigma main(){
	//freopen("txt.in","r",stdin);
	//freopen("txt.out","w",stdout);
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int TT = 1;
	// cin >> TT;
	for(int i = 1 ; i <= TT ; i++){
		//cout << "Case " << i << ": ";
		Gold();
	}
}

Compilation message

joi2019_ho_t3.cpp: In function 'void rec(char, int)':
joi2019_ho_t3.cpp:30:6: warning: unused variable 'ans' [-Wunused-variable]
   30 |  int ans = 0;
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 6 ms 736 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 5 ms 604 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 6 ms 796 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 3 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 6 ms 736 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 5 ms 604 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 6 ms 796 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 3 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Execution timed out 1030 ms 344 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 6 ms 736 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 5 ms 604 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 6 ms 796 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 3 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Execution timed out 1030 ms 344 KB Time limit exceeded
19 Halted 0 ms 0 KB -