115272024-10-19 10:20:05MagyarKendeSZLGVáltakozó (75 pont)cpp17Wrong answer 69/7545ms1304 KiB
#include <bits/stdc++.h>
using namespace std;

bool can_finish(const array<int, 26>& h, int j) {
    int sum = 0, mx = 0, mxi = -1;
    for (int i = 0; i < 26; i++) {
        sum += h[i];
        if (mx < h[i]) {
            mx = h[i];
            mxi = i;
        }
    }
    return mx <= sum / 2 + (sum % 2 != 0 && j != mxi);
}

int main() {
    string s;
    cin >> s;

    array<int, 26> h = {0};
    for (char c : s) h[c - 'a']++;

    string result;
    for (int i = 0; i < s.size(); i++) {
        for (int j = 0; j < 26; j++) {
            h[j]--;
            if (h[j] >= 0 && can_finish(h, j) &&
                (result.empty() || result.back() != j + 'a')) {
                result += j + 'a';
                break;
            }
            h[j]++;
        }
    }

    cout << result;
}
SubtaskSumTestVerdictTimeMemory
base69/75
1Accepted0/01ms320 KiB
2Accepted0/01ms508 KiB
3Accepted3/31ms320 KiB
4Accepted3/31ms500 KiB
5Accepted3/31ms320 KiB
6Accepted3/31ms320 KiB
7Accepted3/33ms320 KiB
8Accepted3/321ms1084 KiB
9Wrong answer0/327ms880 KiB
10Wrong answer0/327ms928 KiB
11Accepted3/31ms320 KiB
12Accepted3/31ms512 KiB
13Accepted3/31ms320 KiB
14Accepted3/31ms320 KiB
15Accepted3/319ms1076 KiB
16Accepted3/319ms1112 KiB
17Accepted3/326ms1172 KiB
18Accepted3/335ms1148 KiB
19Accepted3/318ms1080 KiB
20Accepted3/318ms1304 KiB
21Accepted3/323ms1176 KiB
22Accepted6/617ms1084 KiB
23Accepted6/620ms1292 KiB
24Accepted6/645ms1112 KiB