답안 #819130

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
819130 2023-08-10T08:08:43 Z vjudge1 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
20 / 100
500 ms 157440 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MAXN = 1e5 + 5;
const ll INF = 1e9;
#define fi first
#define se second

ll n;
string s;
map <string, bool> vis;
queue <pair <ll, string> > q;

bool cek(string x){
    for(ll i = 0; i < n-1; i++){
        if(x[i] == x[i+1]){
            return false;
        }
    }
    return true;
}

void bfs(string x){
    vis[x] = true;
    q.push({0, x});
    while(!q.empty()){
        ll discur = q.front().fi;
        string cur = q.front().se;
        // cout << cur << " " << discur << endl;
        q.pop();
        if(cek(cur)){
            cout << discur << endl;
            exit(0);
        }
        for(ll i = 0; i < n-1; i++){
            if(((i-1 >= 0 && cur[i] == cur[i-1]) || (i+2 <n && cur[i+1] == cur[i+2])) && cur[i] != cur[i+1]){
                swap(cur[i], cur[i+1]);
                if(!vis[cur]){
                    vis[cur] = true;
                    q.push({discur+1, cur});
                }
                swap(cur[i], cur[i+1]);
            }
        }
    }
    cout << -1 << endl;
    return;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> s;
    bool kun = false;
    for(ll i = 0; i < n; i++){
        if(s[i] == 'Y') kun = true;
    }
    if(!kun){
        ll mer = 0, ijo = 0;
        for(ll i = 0; i < n; i++){
            if(s[i] == 'R') mer++;
            else ijo++;
        }
        if(abs(mer - ijo) > 1){
            cout << -1 << endl;
            return 0;
        }
        // cout << mer << " " << ijo << endl;
        ll ans1 = INF, ans2 = INF;
        if(mer >= ijo){
            ans1 = 0;
            ll las = -1;
            for(ll i = 0; i < n; i++){
                // cout << las << endl;
                if(i % 2 == 0){
                    ll tmp = las + 1;
                    while(s[tmp] != 'R'){
                        tmp++;
                    }
                    // cout << tmp << " " << i << endl;
                    ans1 += abs(tmp - i);
                    las = tmp;
                }
            }
        }
        if(ijo >= mer){
            ans2 = 0;
            ll las = -1;
            for(ll i = 0; i < n; i++){
                if(i % 2 == 0){
                    ll tmp = las + 1;
                    while(s[tmp] != 'G'){
                        tmp++;
                    }
                    ans2 += abs(tmp - i);
                    las = tmp;
                }
            }
        }
        // cout << ans1 << " " << ans2 << endl;
        cout << min(ans1, ans2) << endl;
        exit(0);
    }
    bfs(s);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 320 KB Output is correct
18 Execution timed out 1088 ms 157440 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 320 KB Output is correct
18 Execution timed out 1088 ms 157440 KB Time limit exceeded
19 Halted 0 ms 0 KB -