62172023-11-08 10:33:26CWMDiana and Numberscpp17Wrong answer 0/10034ms5704 KiB
#include <iostream>

using namespace std;

string largerString(string a, string b) {
	int idxa = 0;
	int idxb = 0;
	while (idxa<a.size() && a[idxa] == '0') {
		idxa++;
	}
	while (idxb < b.size() && b[idxb] == '0') {
		idxb++;
	}
	a.erase(0, idxa);
	b.erase(0, idxb);
	if (a.size() > b.size()) return a;
	else if (b.size() > a.size()) return b;
	else {
		for (size_t i = 0; i < a.size(); i++)
		{
			if (a[i] > b[i]) return a;
			if (b[i] > a[i]) return b;
		}
	}
	return a;
}
bool validString(string s) {
	for (size_t i = 0; i < s.size(); i++)
	{
		if (s[i] != '0') return true;
	}
	return false;
}

int main()
{
	int a;
	cin >> a;
	for (size_t i = 0; i < a; i++)
	{
		string input;
		cin >> input;
		int inpSum = 0;
		int oneLeft = 0;
		int twoLeft = 0;
		for (size_t j = 0; j < input.size(); j++)
		{
			inpSum += input[j] - '0';
			if (input[j] % 3 == 1) oneLeft++;
			else if (input[j] % 3 == 2) twoLeft++;
		}
		inpSum %= 3;
		// 0
		if (inpSum == 0) {
			cout << input << "\n";
			continue;
		}
		// 1
		if (inpSum == 1) {
			int changeVal1 = -10;
			int changeVal2 = -10;
			int changeVal3 = -10;
			int lr1 = 100'001;
			int lr2 = 100'001;
			int lr3 = 100'001;
			int lr1Index = -1;
			int lr2Index = -1;
			int lr3Index = -1;
			if (oneLeft > 0) {
				for (size_t j = 0; j < input.size(); j++)
				{
					int val = input[j] - '0';
					if (val % 3 == 1) {
						if (j == input.size()-1) {
							if (changeVal1 < 0) {
								lr1 = 1;
								lr1Index = j;
								changeVal1 = 0;
							}
						}
						for (size_t k = j+1; k < input.size(); k++)
						{
							if (j>0 || input[k] != '0') {
								int curlr = k - j;
								int curCV = input[k] - input[j];
								if (curlr == 1 && curCV > 0) {
									changeVal1 = curCV;
									lr1 = 1;
									lr1Index = j;
									j = input.size();
									k = input.size();
								}
								else if (curlr < lr1) {
									lr1 = curlr;
									changeVal1 = curCV;
									lr1Index = j;
								}
								else if (curlr == lr1 && curCV >= changeVal1) {
									changeVal1 = curCV;
									lr1Index = j;
								}
								break;
							}
						}
					}
				}
				//cout << lr1Index << " " << changeVal1 << " " << lr1 << "\n";
			}
			//1.2
			if (twoLeft > 1 && lr1>1) {
				for (size_t j = 0; j < input.size(); j++)
				{
					int val = input[j] - '0';
					if (val % 3 == 2) {
						for (size_t k = j + 1; k < input.size(); k++)
						{
							if (j > 0 || input[k] != '0') {
								int curlr = k - j;
								int curCV = input[k] - input[j];
								if (curlr == 1 && curCV > 0) {
									if (lr2 != 1 || changeVal2 < 0) {
										changeVal3 = changeVal2;
										lr3 = lr2;
										lr3Index = lr2Index;
										changeVal2 = curCV;
										lr2 = 1;
										lr2Index = j;
									}
									else if (lr3 != 1 || changeVal3 < 0) {
										changeVal3 = curCV;
										lr3 = 1;
										lr3Index = j;
									}
								}
								else if (curlr < lr2) {
									changeVal3 = changeVal2;
									lr3 = lr2;
									lr3Index = lr2Index;
									changeVal2 = curCV;
									lr2 = curlr;
									lr2Index = j;
								}
								else if (curlr < lr3) {
									changeVal3 = curCV;
									lr3 = curlr;
									lr3Index = j;
								}
								else if (curlr == lr2 && curCV >= changeVal2) {
									changeVal3 = changeVal2;
									lr3 = lr2;
									lr3Index = lr2Index;
									changeVal2 = curCV;
									lr2 = curlr;
									lr2Index = j;
								}
								else if (curlr == lr3 && curCV >= changeVal3) {
									changeVal3 = curCV;
									lr3 = curlr;
									lr3Index = j;
								}
								break;
							}
						}
					}
				}
				//cout << lr2Index << " " << changeVal2 << " " << lr2 << "\n";
				//cout << lr3Index << " " << changeVal3 << " " << lr3 << "\n";
			}
			//compare!
			string oldInput = input;
			if (lr1 != 100'001) {
				input.erase(lr1Index, lr1);
			}
			string res1 = input;
			input = oldInput;
			if (lr3 != 100'001) {
				input.erase(lr3Index, lr3);
			}
			if (lr2 != 100'001) {
				input.erase(lr2Index, lr2);
			}
			string res2 = input;
			input = oldInput;
			if (res1 == oldInput) {
				res1 = "0";
			}
			if (res2 == oldInput) {
				res2 = "0";
			}
			string sol = largerString(res1, res2);
			if (validString(sol)) cout << sol << "\n";
			else cout << "-1\n";
		}
		// 2
		if (inpSum == 2) {
			int changeVal1 = -10;
			int changeVal2 = -10;
			int changeVal3 = -10;
			int lr1 = 100'001;
			int lr2 = 100'001;
			int lr3 = 100'001;
			int lr1Index = -1;
			int lr2Index = -1;
			int lr3Index = -1;
			if (twoLeft > 0) {
				for (size_t j = 0; j < input.size(); j++)
				{
					int val = input[j] - '0';
					if (val % 3 == 2) {
						if (j == input.size() - 1) {
							if (changeVal1 < 0) {
								lr1 = 1;
								lr1Index = j;
								changeVal1 = 0;
							}
						}
						for (size_t k = j + 1; k < input.size(); k++)
						{
							if (j > 0 || input[k] != '0') {
								int curlr = k - j;
								int curCV = input[k] - input[j];
								if (curlr == 1 && curCV > 0) {
									changeVal1 = curCV;
									lr1 = 1;
									lr1Index = j;
									j = input.size();
									k = input.size();
								}
								else if (curlr < lr1) {
									lr1 = curlr;
									changeVal1 = curCV;
									lr1Index = j;
								}
								else if (curlr == lr1 && curCV >= changeVal1) {
									changeVal1 = curCV;
									lr1Index = j;
								}
								break;
							}
						}
					}
				}
				//cout << lr1Index << " " << changeVal1 << " " << lr1 << "\n";
			}
			//2.2
			if (oneLeft > 1 && lr1 > 1) {
				for (size_t j = 0; j < input.size(); j++)
				{
					int val = input[j] - '0';
					if (val % 3 == 1) {
						for (size_t k = j + 1; k < input.size(); k++)
						{
							if (j > 0 || input[k] != '0') {
								int curlr = k - j;
								int curCV = input[k] - input[j];
								if (curlr == 1 && curCV > 0) {
									if (lr2 != 1 || changeVal2 < 0) {
										changeVal3 = changeVal2;
										lr3 = lr2;
										lr3Index = lr2Index;
										changeVal2 = curCV;
										lr2 = 1;
										lr2Index = j;
									}
									else if (lr3 != 1 || changeVal3 < 0) {
										changeVal3 = curCV;
										lr3 = 1;
										lr3Index = j;
									}
								}
								else if (curlr < lr2) {
									changeVal3 = changeVal2;
									lr3 = lr2;
									lr3Index = lr2Index;
									changeVal2 = curCV;
									lr2 = curlr;
									lr2Index = j;
								}
								else if (curlr < lr3) {
									changeVal3 = curCV;
									lr3 = curlr;
									lr3Index = j;
								}
								else if (curlr == lr2 && curCV >= changeVal2) {
									changeVal3 = changeVal2;
									lr3 = lr2;
									lr3Index = lr2Index;
									changeVal2 = curCV;
									lr2 = curlr;
									lr2Index = j;
								}
								else if (curlr == lr3 && curCV >= changeVal3) {
									changeVal3 = curCV;
									lr3 = curlr;
									lr3Index = j;
								}
								break;
							}
						}
					}
				}
				//cout << lr2Index << " " << changeVal2 << " " << lr2 << "\n";
				//cout << lr3Index << " " << changeVal3 << " " << lr3 << "\n";
			}
			//compare!
			string oldInput = input;
			if (lr1 != 100'001) {
				input.erase(lr1Index, lr1);
			}
			string res1 = input;
			input = oldInput;
			if (lr3 != 100'001) {
				input.erase(lr3Index, lr3);
			}
			if (lr2 != 100'001) {
				input.erase(lr2Index, lr2);
			}
			string res2 = input;
			input = oldInput;
			if (res1 != oldInput) {
				cout << res1 << " ";
			}
			if (res2 != oldInput) {
				cout << res2;
			}
			cout << "\n";
		}
	}
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1820 KiB
subtask20/9
2Wrong answer3ms2060 KiB
3Wrong answer3ms2268 KiB
subtask30/12
4Wrong answer3ms2516 KiB
5Wrong answer3ms2720 KiB
6Runtime error4ms3484 KiB
7Wrong answer3ms3040 KiB
8Wrong answer3ms2936 KiB
9Wrong answer3ms3048 KiB
subtask40/27
10Wrong answer28ms4584 KiB
subtask50/52
11Wrong answer16ms3516 KiB
12Wrong answer32ms3760 KiB
13Accepted34ms5120 KiB
14Accepted34ms5096 KiB
15Runtime error17ms4212 KiB
16Accepted32ms5108 KiB
17Wrong answer28ms5392 KiB
18Wrong answer23ms4232 KiB
19Wrong answer23ms5468 KiB
20Accepted30ms5704 KiB
21Wrong answer3ms4228 KiB
22Wrong answer3ms4504 KiB
23Runtime error4ms5016 KiB
24Wrong answer3ms4452 KiB
25Wrong answer3ms4452 KiB
26Wrong answer3ms4580 KiB