Submission #316767

#TimeUsernameProblemLanguageResultExecution timeMemory
316767aZvezdaGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
100 / 100
95 ms163068 KiB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "

const int MAX_N = 410;
int dp[MAX_N][MAX_N][MAX_N][3];
int n;
string in;
int cnt[MAX_N][3];
vector<int> ind[3];

int getCode(char c) {
	if(c == 'R') {
		return 0;
	} else if(c == 'G') {
		return 1;
	} else if(c == 'Y') {
		return 2;
	}
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	cin >> n;
	cin >> in;

	for(int i = 0; i < n; i ++) {
		if(i != 0) {
			for(int j = 0; j < 3; j ++) {
				cnt[i][j] = cnt[i - 1][j];
			}
		}
		cnt[i][getCode(in[i])] ++;
		ind[getCode(in[i])].push_back(i);
	}

	for(int i = 0; i <= ind[0].size(); i ++) {
		for(int j = 0; j <= ind[1].size(); j ++) {
			for(int k = 0; k <= ind[2].size(); k ++) {
				for(int last = 0; last < 3; last ++) {
					dp[i][j][k][last] = mod;
				}
			}
		}
	}
	
	dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;

	for(int i = 0; i <= ind[0].size(); i ++) {
		for(int j = 0; j <= ind[1].size(); j ++) {
			for(int k = 0; k <= ind[2].size(); k ++) {
				for(int last = 0; last < 3; last ++) {
					if(dp[i][j][k][last] == mod) {continue;}
					if(last != 0 && i != ind[0].size()) {
						int toGet = ind[0][i];
						int cost = max(0, cnt[toGet][1] - j) + max(0, cnt[toGet][2] - k);
						chkmin(dp[i + 1][j][k][0], dp[i][j][k][last] + cost);
					} 
					if(last != 1 && j != ind[1].size()) {
						int toGet = ind[1][j];
						int cost = max(0, cnt[toGet][0] - i) + max(0, cnt[toGet][2] - k);
						chkmin(dp[i][j + 1][k][1], dp[i][j][k][last] + cost);
					} 	
					if(last != 2 && k != ind[2].size()) {
						int toGet = ind[2][k];
						int cost = max(0, cnt[toGet][0] - i) + max(0, cnt[toGet][1] - j);
						chkmin(dp[i][j][k + 1][2], dp[i][j][k][last] + cost);
					} 
				}
			}
		}
	}
	
	int ans = mod;
	for(int i = 0; i < 3; i ++) {
		chkmin(ans, dp[ind[0].size()][ind[1].size()][ind[2].size()][i]);
	}
	
	if(ans >= mod) {
		cout << -1 << endl;
	} else {
		cout << ans << endl;
	}
	return 0;
}

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:46:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |  for(int i = 0; i <= ind[0].size(); i ++) {
      |                 ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:47:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |   for(int j = 0; j <= ind[1].size(); j ++) {
      |                  ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:48:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |    for(int k = 0; k <= ind[2].size(); k ++) {
      |                   ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:58:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |  for(int i = 0; i <= ind[0].size(); i ++) {
      |                 ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |   for(int j = 0; j <= ind[1].size(); j ++) {
      |                  ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |    for(int k = 0; k <= ind[2].size(); k ++) {
      |                   ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:63:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |      if(last != 0 && i != ind[0].size()) {
      |                      ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:68:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |      if(last != 1 && j != ind[1].size()) {
      |                      ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:73:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |      if(last != 2 && k != ind[2].size()) {
      |                      ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int getCode(char)':
joi2019_ho_t3.cpp:29:1: warning: control reaches end of non-void function [-Wreturn-type]
   29 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...