Submission #1144886

#TimeUsernameProblemLanguageResultExecution timeMemory
1144886SmuggingSpunGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++20
20 / 100
68 ms7864 KiB
#include<bits/stdc++.h>
#define taskname "C"
using namespace std;
int n;
string s;
namespace sub1{
	void solve(){
		unordered_map<string, int>f;
		f[s] = 1;
		queue<string>q;
		q.push(s);
		while(!q.empty()){
			s = q.front();
			q.pop();
			bool flag = true;
			for(int i = 1; i < n; i++){
				if(s[i] == s[i - 1]){
					flag = false;
					break;
				}
			}
			if(flag){
				return void(cout << f[s] - 1);
			}
			int N = f[s];
			for(int i = 1; i < n; i++){
				swap(s[i], s[i - 1]);
				if(f[s] == 0){
					f[s] = N + 1;
					q.push(s);
				}
				swap(s[i], s[i - 1]);
			}
		}
		cout << -1;
	}
}
namespace sub3{
	void solve(){
		int R = count(s.begin(), s.end(), 'R'), G = n - R;
		if(abs(R - G) > 1){
			return void(cout << -1);
		}
		if(R == G){
			int ans_1 = 0, ans_2 = 0;
			for(int i = 0, r = 0, g = 0; i < n; i++){
				if(s[i] == 'R'){
					ans_1 += abs(i - r);
					r += 2;
				}
				else{
					ans_2 += abs(i - g);
					g += 2;
				}
			}
			cout << min(ans_1, ans_2);
		}
		else if(R == G + 1){
			int ans = 0;
			for(int i = 0, j = 0; i < n; i++){
				if(s[i] == 'R'){
					ans += abs(i - j);
					j += 2;
				}
			}
			cout << ans;
		}
		else{
			int ans = 0;
			for(int i = 0, j = 0; i < n; i++){
				if(s[i] == 'G'){
					ans += abs(i - j);
					j += 2;
				}
			}
			cout << ans;
		}
	}
}
namespace sub24{
	void solve(){
		
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
	cin >> n >> s;
	if(n <= 15){
		sub1::solve();
	}
	else if(find(s.begin(), s.end(), 'Y') == s.end()){
		sub3::solve();
	}
	else{
		sub24::solve();
	}
}

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:88:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...