/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 50ms 580.0 KiB
#3 Accepted 64ms 532.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 6ms 532.0 KiB
#6 Accepted 166ms 572.0 KiB
#7 Accepted 155ms 532.0 KiB
#8 Accepted 61ms 584.0 KiB
#9 Accepted 56ms 580.0 KiB

Code

#include <bits/stdc++.h>

using i64 = long long;

constexpr int MAX = 2001;

std::vector<int> f(MAX), p;

void init() {
    std::fill(f.begin(), f.end(), 1);
    f[1] = 0;
    for (int i = 2; i < MAX; i++) {
        if (f[i]) {
            p.push_back(i);
            for (int j = i + i; j < MAX; j += i) {
                f[j] = 0;
            }
        }
    }
}

void solve() {
    int n;
    std::cin >> n;
    std::vector<int> cnt(3);
    std::string s;
    std::cin >> s;
    for (int i = 0; i < n; i++) {
        cnt[s[i] - 'a']++;
    }
    int ans = INT_MAX;
    for (int i = 0; i <= n; i++) {
        if (!f[i]) {
            continue;
        }
        for (int j = 0; j <= n; j++) {
            if (!f[j]) {
                continue;
            }
            if (n - i - j < 0) {
                continue;
            }
            if (!f[n - i - j]) {
                continue;
            }
            ans = std::min(ans, (std::abs(cnt[0] - i) + std::abs(cnt[1] - j) + std::abs(cnt[2] - (n - i - j))) / 2);
        }
    }
    if (ans == INT_MAX) {
        std::cout << -1 << "\n";
    } else {
        std::cout << ans << "\n";
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    init();
    int t;
    std::cin >> t;
    while (t--) {
        solve();
    }
}

Information

Submit By
Type
Submission
Problem
P1158 Yet another Beautiful String
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 15:08:08
Judged At
2025-01-02 15:08:08
Judged By
Score
100
Total Time
166ms
Peak Memory
584.0 KiB