QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#854652#3230. Beautiful NowIBory100 ✓1819ms3580kbC++23934b2025-01-12 04:12:392025-01-12 04:12:41

Judging History

你现在查看的是最新测评结果

  • [2025-01-12 04:12:41]
  • 评测
  • 测评结果:100
  • 用时:1819ms
  • 内存:3580kb
  • [2025-01-12 04:12:39]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

int Decompose(vector<int>& G, string& T) {
	int ret = G.size(), V = 0;
	for (int i = 0; i < G.size(); ++i) {
		if (V & (1 << i)) continue;
		ret--;
		int cur = i;
		while (!(V & (1 << cur))) {
			if (G[cur] != i) swap(T[cur], T[G[cur]]);
			V |= 1 << cur;
			cur = G[cur];
		}
	}
	return ret;
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	int _T;
	cin >> _T;
	while (_T--) {
		string S;
		int K;
		cin >> S >> K;

		vector<int> P(S.size());
		iota(P.begin(), P.end(), 0);
		string ansMin = "999999999999", ansMax = "000000000000";

		sort(P.begin(), P.end());
		do {
			string T = S;
			int k = Decompose(P, T);
			if (k <= K) {
				if (T[0] != '0') {
					ansMin = min(ansMin, T);
					ansMax = max(ansMax, T);
				}
			}
		} while (next_permutation(P.begin(), P.end()));

		cout << ansMin << ' ' << ansMax << '\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 100
Accepted
time: 1819ms
memory: 3580kb

input:

100
645637224 8
397815246 8
239758591 7
152472214 2
485635752 8
128947635 5
938124675 8
311384311 8
665595968 7
284675191 6
3596920 776160768
428993752 4
796354812 4
342871569 7
952970714 2
399469166 8
774997047 1
534968712 5
574918236 3
564999511 645561291
791543286 7
171222224 7
319647582 2
241573...

output:

223445667 766544322
123456789 987654321
123557899 998755321
112272454 754412212
234555678 876555432
123456789 987652134
123456789 987654321
111133348 843331111
555666899 998666555
112456798 987654211
2035699 9965320
223458799 998753422
123456879 987654312
123456789 987654312
102975794 997570214
1346...

result:

ok 100 lines