62092023-11-08 09:30:35EsVagyDiana and Numberscpp17Wrong answer 0/10041ms6448 KiB
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    int tests;
    cin >> tests;
    for (int test = 0; test < tests; test++)
    {
        string number;
        cin >> number;

        vector<vector<int>> numbers(10);
        for (int i = 0; i < 10; i++)
        {
            numbers.push_back(vector<int>());
        }

        int mod1Count = 0;
        int mod2Count = 0;
        int mod = 0;

        for (int i = 0; i < number.size(); i++)
        {
            int curr = number[i] - 48;
            int currMod = curr % 3;

            switch (currMod)
            {
            case 1:
                mod1Count++;
                mod += 1;
                mod %= 3;
                break;
            case 2:
                mod2Count++;
                mod += 2;
                mod %= 3;
                break;
            }

            numbers[curr].push_back(i);
        }

        if (mod == 0)
        {
            cout << number << "\n";
            continue;
        }

        int findCount = 0;
        int findMod = 0;
        
        if (mod == 1)
        {
            if (mod1Count >= 1)
            {
                findCount = 1;
                findMod = 1;
            }
            else if (mod2Count >= 2)
            {
                findCount = 2;
                findMod = 2;
            }
            else
            {
                cout << -1 << "\n";
                continue;
            }
        }
        else
        {
            if (mod2Count >= 1)
            {
                findCount = 1;
                findMod = 2;
            }
            else if (mod1Count >= 2)
            {
                findCount = 2;
                findMod = 1;
            }
            else
            {
                cout << -1 << "\n";
                continue;
            }
        }

        vector<int> removeAt;

        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < numbers[i].size(); j++)
            {
                if (i % 3 != findMod)
                {
                    continue;
                }
                removeAt.push_back(numbers[i][j]);
                findCount--;
                if (findCount == 0)
                {
                    break;
                }
            }
            if (findCount == 0)
            {
                break;
            }
        }

        int start = 0;
        int size = 0;

        for (int i : removeAt)
        {
            cout << number.substr(start, i - start);
            size += i - start;
            start = i + 1;
            while (size == 0 && number[start] == '0')
            {
                start++;
            }
        }

        cout << number.substr(start, number.size() - start);
        size += number.size() - start;

        if (size == 0)
        {
            cout << "-1";
        }

        cout << "\n";
    }
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1680 KiB
subtask20/9
2Wrong answer3ms1932 KiB
3Wrong answer3ms2176 KiB
subtask30/12
4Wrong answer3ms2392 KiB
5Wrong answer3ms2620 KiB
6Wrong answer3ms2848 KiB
7Wrong answer3ms2816 KiB
8Wrong answer3ms2876 KiB
9Wrong answer3ms3120 KiB
subtask40/27
10Wrong answer32ms5188 KiB
subtask50/52
11Wrong answer18ms3580 KiB
12Wrong answer34ms3860 KiB
13Wrong answer39ms5600 KiB
14Wrong answer41ms5308 KiB
15Wrong answer26ms4040 KiB
16Wrong answer37ms5828 KiB
17Wrong answer32ms5652 KiB
18Wrong answer26ms4216 KiB
19Wrong answer27ms5796 KiB
20Wrong answer37ms6448 KiB
21Wrong answer3ms4280 KiB
22Wrong answer3ms4316 KiB
23Wrong answer3ms4420 KiB
24Wrong answer3ms4532 KiB
25Wrong answer3ms4768 KiB
26Wrong answer3ms4404 KiB