Submission #1118873

#TimeUsernameProblemLanguageResultExecution timeMemory
1118873gdragonGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
20 / 100
51 ms508 KiB
#include <bits/stdc++.h> using namespace std; #define TASK "long" #define fi first #define se second #define ll long long #define pb push_back #define ALL(x) (x).begin(), (x).end() #define GETBIT(mask, i) ((mask) >> (i) & 1) #define MASK(i) ((1LL) << (i)) #define SZ(x) ((int)(x).size()) #define mp make_pair #define CNTBIT(mask) __builtin_popcount(mask) template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;}; template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;}; typedef pair<int, int> ii; const int N = 400 + 5; const int inf = 1e9 + 7; const long long INF = (long long)1e18 + 7; const int mod = 1e9 + 7; int n; int a[N]; string s; void read() { cin >> n >> s; for(int i = 0; i < n; i++) { if (s[i] == 'R') a[i + 1] = 0; else if (s[i] == 'G') a[i + 1] = 1; else a[i + 1] = 2; } } bool check3() { for(int i = 1; i <= n; i++) if (a[i] == 2) return false; return true; } void sub3() { vector<int> cnt[2]; for(int i = 1; i <= n; i++) cnt[a[i]].push_back(i); if (abs(SZ(cnt[0]) - SZ(cnt[1])) > 1) { cout << -1; return; } if (n & 1) { if (SZ(cnt[0]) < SZ(cnt[1])) swap(cnt[0], cnt[1]); reverse(ALL(cnt[0])); reverse(ALL(cnt[1])); int ans = 0; for(int i = 1; i <= n; i++) { int c = 1; if (i & 1) c = 0; int p = cnt[c].back(); cnt[c].pop_back(); for(int j = 0; j < 2; j++) { for(auto &pos: cnt[j]) if (pos < p) ++pos; } ans += p - i; // cerr << p << ' ' << i << ' ' << ans << endl; } cout << ans; } else { vector<int> tmp[2]; tmp[0] = cnt[0]; tmp[1] = cnt[1]; int ans = inf; for(int iter = 0; iter < 2; iter++) { cnt[0] = tmp[0]; cnt[1] = tmp[1]; reverse(ALL(cnt[0])); reverse(ALL(cnt[1])); int res = 0; for(int i = 1; i <= n; i++) { int c = 1; if (i & 1) c = 0; int p = cnt[c].back(); cnt[c].pop_back(); for(int j = 0; j < 2; j++) { for(auto &pos: cnt[j]) if (pos < p) ++pos; } res += p - i; } ans = min(ans, res); swap(tmp[0], tmp[1]); } cout << ans; } } void sub1() { vector<int> cnt[3], tmp[3]; for(int i = 1; i <= n; i++) cnt[a[i]].push_back(i); for(int i = 0; i < 3; i++) { reverse(ALL(cnt[i])); tmp[i] = cnt[i]; } sort(a + 1, a + n + 1); int ans = inf; do { for(int i = 0; i < 3; i++) cnt[i] = tmp[i]; int res = 0; for(int i = 1; i <= n; i++) { if (i > 1 && a[i] == a[i - 1]) { res = inf; break; } int p = cnt[a[i]].back(); cnt[a[i]].pop_back(); for(int j = 0; j < 3; j++) { for(auto &pos: cnt[j]) if (pos < p) ++pos; } res += p - i; // cerr << p << ' ' << i << ' ' << ans << endl; } ans = min(ans, res); } while(next_permutation(a + 1, a + n + 1)); cout << (ans == inf ? -1 : ans); } void solve() { if (check3()) sub3(); else if (n <= 15) sub1(); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); } int test = 1; // cin >> test; while(test--) { read(); solve(); } return 0; } // rmq - rmq2d // hash // fw - fw2d // segtree

Compilation message (stderr)

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