Submission #110432

#TimeUsernameProblemLanguageResultExecution timeMemory
110432pamajGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
15 / 100
9 ms384 KiB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 410;

int n;

bool check(string t)
{
	for(int i = 0; i < t.size() - 1; i++)
		if(t[i] == t[i + 1]) return false;

	return true;
}

void next_try(string &t)
{
	for(int p = t.size() - 1; p > 0; p--)
	{
		swap(t[p], t[p - 1]);
	}
}

int qt[4];

int main()
{
	cin >> n;
	
	vector<char> s, t;

	for(int i = 0; i < n; i++)
	{
		char c;
		cin >> c;
		int tipo;
		if(c == 'R') tipo = 1;
		else if(c == 'G') tipo = 2;
		else tipo = 3;

		qt[tipo]++;

		s.push_back(c);
	}

	string crct;

	int metade = n/2 + (n % 2 == 1); 

	if(qt[1] > metade or qt[2] > metade or qt[3] > metade)
	{
		cout << -1 << "\n";
		return 0;
	}

	int cont = 0;

	vector<char> ch[3];
	for(int i = 0; i < qt[1]; i++) ch[0].push_back('R');
	for(int i = 0; i < qt[2]; i++) ch[1].push_back('G');
	for(int i = 0; i < qt[3]; i++) ch[2].push_back('Y');


	for(int i = 0; i < n; i++)
	{
		while((int)ch[cont].size() == 0) cont++, cont %= 3;

		crct += ch[cont][0];
		ch[cont].pop_back();
		cont++;
		cont %= 3;
	}

	//cout << crct << "\n";

	int ans = 1e9;

	for(int j = 0; j < n; j++)
	{
		next_try(crct);

		if(!check(crct)) continue;

		//cout << crct << "\n";

		vector<int> v1r, v1g, v1y;
		vector<int> v2r, v2g, v2y;


		for(int i = 0; i < n; i++)
		{
			if(s[i] == 'R')
			{
				v1r.push_back(i);
			}
			if(s[i] == 'G')
			{
				v1g.push_back(i);
			}
			if(s[i] == 'Y')
			{
				v1y.push_back(i);
			}
			if(crct[i] == 'R')
			{
				v2r.push_back(i);
			}
			if(crct[i] == 'G')
			{
				v2g.push_back(i);
			}
			if(crct[i] == 'Y')
			{
				v2y.push_back(i);
			}
		}

		int num_dif = 0;

		for(int i = 0; i < v1y.size(); i++)
		{
			num_dif += abs(v1y[i] - v2y[i]);
		}
		for(int i = 0; i < v1r.size(); i++)
		{
			num_dif += abs(v1r[i] - v2r[i]);
		}
		for(int i = 0; i < v1g.size(); i++)
		{
			num_dif += abs(v1g[i] - v2g[i]);
		}

		ans = min(ans, num_dif/2);
		
	}
	cout << (ans == (int)1e9 ? -1 : ans) << "\n";

}

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'bool check(std::__cxx11::string)':
joi2019_ho_t3.cpp:9:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < t.size() - 1; i++)
                 ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:119:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < v1y.size(); i++)
                  ~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:123:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < v1r.size(); i++)
                  ~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:127:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < v1g.size(); i++)
                  ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...