Submission #497469

# Submission time Handle Problem Language Result Execution time Memory
497469 2021-12-23T06:26:32 Z kingline Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
15 / 100
1 ms 204 KB
/*#pragma GCC optimize("O3")
#pragma GCC target ("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")*/
#include <bits/stdc++.h>
#pragma GCC optimize ("unroll-loops,Ofast,O3")
#pragma GCC target("avx,avx2,fma")
//#define file(data) freopen(data".in", "r", stdin); freopen(data".out", "w", stdout);
#define pb push_back
//#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(data) data.begin() , data.end()
#define endl '\n'
//freopen("nenokku_easy.in", "r", stdin);
//freopen("nenokku_easy.out", "w", stdout);
#define int long long
#define pii pair < int, int >
#define pll pair < long long, long long >

using namespace std;

typedef long long ll;
const int N = 1e6 + 5;
const int M = 350;
const int mod = 1e9 + 7;

int n, a[N], ans = -1;
char s[N];

void rec(int pos, int res) {
    if(pos == n) {
        bool ok = 1;
        for(int i = 2; i <= n; i++) {
            if(s[i] == s[i - 1]) ok = 0;
        }
        if(ok) {
            if(ans == -1) ans = res;
            else ans = min(ans, res);
        }
        return;
    }
    swap(s[pos + 1], s[pos]);
    rec(pos + 1, res + 1);
    swap(s[pos + 1], s[pos]);
    rec(pos + 1, res);
}

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //file("pieaters");
    cin >> n;
    int r = 0, g = 0;
    for(int i = 1; i <= n; i++) {
        cin >> s[i];
        if(s[i] == 'R') ++r;
        else if(s[i] == 'G')g++;
    }
    if(r + g == n) {
        if(g != r && g + 1 != r && g != r + 1) {
            cout << -1;
        } else {
            int res1 = 1e9, res2 = 1e9;
            if(g >= r) {
                res1 = 0;
                int num = 1;
                for(int i = 1; i <= n; i++) {
                    if(s[i] == 'G') {
                        res1 += abs(num - i);
                        num += 2;
                    }
                }
            }
            if(g <= r) {
                res2 = 0;
                int num = 1;
                for(int i = 1; i <= n; i++) {
                    if(s[i] == 'R') {
                        res2 += abs(num - i);
                        num += 2;
                    }
                }
            }
            cout << min(res1, res2);
        }
    } else {
        rec(1, 0);
        cout << ans;
    }
}

/*
5 9 7
...##....
..#.##..#
..#....##
.##...#..
....#....
WS?EE??
*/









Compilation message

joi2019_ho_t3.cpp:48:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   48 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 1 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 1 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 1 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -