QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#346508#4777. TichuPetroTarnavskyi#AC ✓157ms3616kbC++202.0kb2024-03-08 16:57:372024-03-08 16:57:38

Judging History

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

  • [2024-03-08 16:57:38]
  • 评测
  • 测评结果:AC
  • 用时:157ms
  • 内存:3616kb
  • [2024-03-08 16:57:37]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

map<char, int> value;
void init()
{
	FOR(i, 2, 10)
		value[char('0' + i)] = i;
	value['T'] = 10;
	value['J'] = 11;
	value['Q'] = 12;
	value['K'] = 13;
	value['A'] = 14;
}


const int SZ = 13;
void solve()
{
	VI a(SZ);
	vector<string> s(SZ);
	FOR(i, 0, SZ)
	{
		cin >> s[i];
		a[i] = value[s[i][0]];
	}
	
	VI ok(1 << SZ, 0);
	FOR(mask, 1, 1 << SZ)
	{
		VI vals;
		FOR(i, 0, SZ)
		{
			if(mask & (1 << i))
				vals.PB(a[i]);
		}
		sort(ALL(vals));
		bool stre = true;
		FOR(i, 0, SZ(vals))
			stre &= vals[i] == vals[0] + i;
		if(vals.back() == vals[0] || (stre && SZ(vals) >= 5))
		{
			ok[mask] = 1;
			continue;
		}
		if(SZ(vals) == 5)
		{
			int cnt0 = 0, cnt1 = 0;
			for(int x : vals)
			{
				if(x == vals[0])
					cnt0++;
				if(x == vals.back())
					cnt1++;
			}
			if(min(cnt0, cnt1) == 2 && max(cnt0, cnt1) == 3)
				ok[mask] = 1;
		}
	}

	VI dp(1 << SZ, SZ + 1);
	VI to(1 << SZ, -1);
	dp[0] = 0;
	
	FOR(mask, 1, 1 << SZ)
	{
		for(int submask = mask; submask > 0; submask = (submask - 1) & mask)
		{
			if(ok[submask] && dp[mask ^ submask] + 1 < dp[mask])
			{
				dp[mask] = dp[mask ^ submask] + 1;
				to[mask] = submask;
			}
		}
	}
	int mask = (1 << SZ) - 1;
	cout << dp[mask] << "\n";
	while(mask)
	{
		int sub = to[mask];
		
		VI poses;
		FOR(i, 0, SZ)
		{
			if(sub & (1 << i))
				poses.PB(i);
		}
		FOR(i, 0, SZ(poses))
		{
			if(i)
				cout << " ";
			cout << s[poses[i]];
		}
		cout << "\n";
		
		mask ^= sub;
	}
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	init();
	
	int t;
	cin >> t;
	while(t--)
		solve();
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 3616kb

input:

2
2h 3c 4d 5d 6s Th Qc Qs Ad Tc Ts 9c 9d
2h 3h 4h 5h 6d 7s 8h 8d 8c 8s 9c Td Js

output:

4
Th Tc Ts 9c 9d
Ad
Qc Qs
2h 3c 4d 5d 6s
2
2h 3h 4h 5h 6d 7s 8s 9c Td Js
8h 8d 8c

result:

ok correct (2 test cases)

Test #2:

score: 0
Accepted
time: 157ms
memory: 3596kb

input:

63
2h 3c 4d 5d 6s Th Qc Qs Ad Tc Ts 9c 9d
2h 3h 4h 5h 6d 7s 8h 8d 8c 8s 9c Td Js
2h 2d 2c 2s 4s 4d 4c Qh Qs Qc Ts Js Ac
5d 5c 7d 5s 3s 3c Qh 3d Qc Ts 5h Js Qs
2s 3h 4d 5d 6d 7s 8s 9s Tc Jd Qc Kc As
2s 3h 4d 5d 6d 7s 8s 9s Tc Jd Qc Kc 8c
2s 3h 4d 5d 6d 7s 8s 9s Tc Jd Qc 8c 7h
3h 4d 5d 6d 7s 8s 9s Tc ...

output:

4
Th Tc Ts 9c 9d
Ad
Qc Qs
2h 3c 4d 5d 6s
2
2h 3h 4h 5h 6d 7s 8s 9c Td Js
8h 8d 8c
5
Ac
Js
Ts
2c 2s Qh Qs Qc
2h 2d 4s 4d 4c
5
5s Qh Qc 5h Qs
Js
Ts
5d 5c 3s 3c 3d
7d
1
2s 3h 4d 5d 6d 7s 8s 9s Tc Jd Qc Kc As
2
2s 3h 4d 5d 6d 7s 9s Tc Jd Qc Kc 8c
8s
2
9s Tc Jd Qc 8c 7h
2s 3h 4d 5d 6d 7s 8s
2
Tc Jd Qc 8c...

result:

ok correct (63 test cases)