Submission #139718

#TimeUsernameProblemLanguageResultExecution timeMemory
139718mlyean00Growing Vegetable is Fun 3 (JOI19_ho_t3)C++14
5 / 100
1064 ms424 KiB
#include <bits/stdc++.h> #ifdef DEBUG #include "debug.hpp" #else #pragma GCC optimize("Ofast") #define trace(...) #include <bits/stdc++.h> #define endl '\n' #endif using namespace std; const string COL = "RGY"; int count_swaps(string s, string const& t) { int n = s.length(); int ans = 0; for (int i = 0; i < n; ++i) { int j; for (j = i; j < n; ++j) { if (s[j] == t[i]) break; } if (j == n) return INT_MAX; for (; j > i; --j) { ++ans; swap(s[j], s[j - 1]); } } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n; string s; cin >> n >> s; int ans = INT_MAX; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 1 << (n - 1); ++j) { string t; int prev_c = i; t.push_back(COL[prev_c]); for (int k = 0; k < n - 1; ++k) { int bit = (j >> k) & 1; prev_c = (prev_c + bit + 1) % 3; t.push_back(COL[prev_c]); } ans = min(count_swaps(s, t), ans); } } if (ans == INT_MAX) ans = -1; cout << ans << endl; 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...