/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.77 MiB
#2 Wrong Answer 3ms 2.77 MiB
#3 Wrong Answer 4ms 2.812 MiB

Code

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define       ll           long long
#define       dub          double
#define       ull          unsigned long long
#define       CY           cout << "YES\n"
#define       CN           cout << "NO\n"
#define       pb           push_back
#define       eb           emplace_back
#define       ft           first
#define       sd           second
#define       srt(v)       sort(v.begin(), v.end())
#define       csrt(v)      sort(v.begin(), v.end(), cmp)
#define       rsrt(v)      sort(v.rbegin(), v.rend())
#define       pll          pair<ll, ll>
#define       vll          vector<pll>
#define       mod1         1000000000+7 // 10^9 + 7
#define       pbds         tree<pair<ll, ll>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>
const ll mod = 1000000007;
const ll inf = 9000000000000000000;

bool cmp(const pair<string, pair<string, ll>> &a, const pair<string, pair<string, ll>> &b)
{
    if(a.sd.ft != b.sd.ft) return a.sd.ft < b.sd.ft;
    if(a.ft != b.ft) return a.ft < b.ft;
    return a.sd.sd > b.sd.sd;
}

const ll mx = 1e5+15;
vll adj[mx];

void solve(int cs)
{
    string s;
    cin >> s;
    string ans;
    map<char, ll> mp;
    for(auto it: s) mp[it]++;
    priority_queue<pair<ll, char>> pq;
    for(auto it: mp){
        pq.push({it.sd, it.ft});
    }
    while(!pq.empty()){
        auto cur = pq.top();
        pq.pop();
        if(!ans.empty() && cur.sd == ans.back()){
            if(pq.empty()){
                cout << "-1\n";
                return;
            }
            auto nxt = pq.top();
            pq.pop();
            ans += nxt.sd;
            nxt.ft--;
            if(nxt.ft) pq.push(nxt);
            pq.push(cur);
        }
        else{
            ans += cur.sd, cur.ft--;
            if(cur.ft) pq.push(cur);
        }
    }
    reverse(ans.begin(), ans.end());
    cout << ans << endl;
}

int main() {

        //ios::sync_with_stdio(false);
        //cin.tie(nullptr);

        int tc=1, cnt=1;
        cin >> tc;
        while (tc--){
            solve(cnt);
            cnt++;
        }


 return 0;
}

Information

Submit By
Type
Submission
Problem
P1209 B. Rearrange the String
Contest
Educational Round 1
Language
C++17 (G++ 13.2.0)
Submit At
2025-07-14 16:29:54
Judged At
2025-07-14 16:29:54
Judged By
Score
0
Total Time
4ms
Peak Memory
2.812 MiB