/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 564.0 KiB
#2 Wrong Answer 1ms 532.0 KiB
#3 Wrong Answer 2ms 504.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
const int Prime = 1e9+7;
const int Base = 251;


//...................................................................//
#define endl                '\n'
#define Int                 long long int
#define gcd(x, y)           (__gcd(x, y))
#define lcm(x, y)           ((x/gcd(x, y))*y)
#define rand(x, y)          rand()%(y-x+1)+x
#define pow2(x)             (1<<x)
#define pi                  3.141592653589793
#define decimal(x, y)       fixed<<setprecision(x)<<y
#define unsync              ios_base::sync_with_stdio(0); cin.tie(0);
#define TxtI                freopen("input.txt","r",stdin);
#define TxtO                freopen("output.txt","w",stdout);
//...................................................................//


int main()
{
    //srand(time(0));
    //TxtI TxtO
    unsync

    int t;
    
    cin >> t;
    
    while(t--)
    {
        Int mx=0;
        char mxC;
        string s, ans="";
        map<char, int> mp;
        priority_queue<pair<int, char>> pq;

        cin >> s;

        for(int i=0; i<s.size(); i++)
        {
            mp[s[i]]++;

            if(mp[s[i]]>mx)
            {
                mx = mp[s[i]];
                mxC = s[i];
            }
        }

        if(s.size()-mp[mxC]<mp[mxC]-1) cout << -1 << endl;
        else
        {
            for(pair<char, int> i : mp) pq.push({i.second, i.first});

            while(pq.size())
            {
                pair<int, char> a = pq.top();
                pair<int, char> b;
                pq.pop();

                ans += a.second;

                if(pq.size()>0)
                {
                    b = pq.top();
                    ans += b.second;
                    pq.pop();

                    if(b.first-1>0) pq.push({b.first-1, b.second});
                }

                if(a.first-1>0) pq.push({a.first-1, a.second});
            }

            for(int i=ans.size()-1; i>=0; i--) cout << ans[i];
            cout << endl;
        }
    }
}

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:08:00
Judged At
2025-07-14 16:08:00
Judged By
Score
0
Total Time
2ms
Peak Memory
564.0 KiB