Submission #140080

#TimeUsernameProblemLanguageResultExecution timeMemory
140080mlyean00Growing Vegetable is Fun 3 (JOI19_ho_t3)C++17
20 / 100
50 ms508 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_inversions(vector<int>& v, int l, int r) { if (r - l == 1) return 0; int mid = (l + r) / 2; int ans = 0; ans += count_inversions(v, l, mid); ans += count_inversions(v, mid, r); vector<int> m; m.reserve(r - l); int ptr1 = l, ptr2 = mid; while (ptr1 < mid || ptr2 < r) { if (ptr1 == mid) { m.push_back(v[ptr2++]); } else if (ptr2 == r) { m.push_back(v[ptr1++]); ans += ptr2 - mid; } else if (v[ptr1] < v[ptr2]) { m.push_back(v[ptr1++]); ans += ptr2 - mid; } else { m.push_back(v[ptr2++]); } } copy(m.begin(), m.end(), v.begin() + l); return ans; } int count_swaps(string s, string const& t) { int n = s.length(); vector<stack<int>> st(3); for (int i = n - 1; i >= 0; --i) { st[COL.find(t[i])].push(i); } vector<int> v(n); for (int i = 0; i < n; ++i) { int c = COL.find(s[i]); if (st[c].empty()) return INT_MAX; v[i] = st[c].top(); st[c].pop(); } return count_inversions(v, 0, n); } int subtask_1(int n, string 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; return ans; } int subtask_3(int n, string s) { string t1, t2; for (int i = 0; i < n; ++i) { t1.push_back("RG"[i % 2]); t2.push_back("GR"[i % 2]); } int ans = min(count_swaps(s, t1), count_swaps(s, t2)); if (ans == INT_MAX) ans = -1; return ans; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n; string s; cin >> n >> s; int ans; if (n <= 15) { ans = subtask_1(n, s); } else { ans = subtask_3(n, s); } 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...