Submission #818905

# Submission time Handle Problem Language Result Execution time Memory
818905 2023-08-10T07:15:43 Z vjudge1 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
0 / 100
1 ms 212 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin >> n;
    vector<char> C(n);
    ll r = 0, y = 0, g = 0, mx = (n + 1) / 2;
    for (int i = 0; i < n; i++)
    {
        cin >> C[i];
        if (C[i] == 'R')
        {
            r++;
        }
        else if (C[i] == 'Y')
        {
            y++;
        }
        else
        {
            g++;
        }
    }
    if (r > mx || y > mx || g > mx)
    {
        cout << -1;
    }
    else
    {
        ll ans = 0;
        for (int i = 1; i < n; i++)
        {
            if (C[i] == C[i - 1])
            {
                ll ser = i + 1;
                while (ser < n && C[ser] == C[i])
                {
                    ser++;
                }
                if (ser >= n)
                {
                    continue;
                }
                while (C[i] == C[i - 1])
                {
                    ans++;
                    swap(C[ser], C[ser - 1]);
                    // for (auto x : C)
                    // {
                    //     cout << x;
                    // }
                    // cout << "\n";
                    ser--;
                }
            }
        }
        for (int i = n - 2; i >= 0; i--)
        {
            if (C[i] == C[i + 1])
            {
                ll ser = i - 1;
                while (ser >= 0 && C[ser] == C[i])
                {
                    ser--;
                }
                if (ser < 0)
                {
                    continue;
                }
                while (C[i] == C[i + 1])
                {
                    ans++;
                    swap(C[ser], C[ser + 1]);
                    // for (auto x : C)
                    // {
                    //     cout << x;
                    // }
                    // cout << "\n";
                    ser++;
                }
            }
        }

        cout << ans;
    }
    return 0;
}

/*


20
YYGYYYGGGGRGYYGRGRYG 1 (0)
YGYYYYGGGGRGYYGRGRYG 2
YGYYYGYGGGRGYYGRGRYG 3
YGYYGYYGGGRGYYGRGRYG 4
YGYGYYYGGGRGYYGRGRYG 5
YGYGYYGYGGRGYYGRGRYG 6
YGYGYGYYGGRGYYGRGRYG 7
YGYGYGYGYGRGYYGRGRYG 8
YGYGYGYGYGRGYGYRGRYG 9

20
YYGYYYGGGGRGYYGRGRYG


*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 0 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 1 ms 212 KB Output isn't correct
5 Halted 0 ms 0 KB -