QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#511091#3408. Help BubuPetroTarnavskyi#AC ✓252ms5452kbC++201.6kb2024-08-09 16:08:022024-08-09 16:08:03

Judging History

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

  • [2024-08-09 16:08:03]
  • 评测
  • 测评结果:AC
  • 用时:252ms
  • 内存:5452kb
  • [2024-08-09 16:08:02]
  • 提交

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;

const int MN = 25;
const int N = 107;
const int P = 8;
const int INF = 1e9;

template<typename T>
void updMin(T& a, T b)
{
	a = min(a, b);
}

int n, k;
int dp[N][P + 1][1 << P], ndp[N][P + 1][1 << P];

void solve()
{
	VI a(n);
	int full = 0;
	for (int& ai : a)
	{
		cin >> ai;
		ai -= MN;
		full |= 1 << ai;
	}
	FOR(j, 0, N)
		FOR(last, 0, P + 1)
			FOR(mask, 0, 1 << P)
				dp[j][last][mask] = INF;
	dp[0][P][full] = 0;
	FOR(i, 0, n)
	{
		FOR(j, 0, N)
			FOR(last, 0, P + 1)
				FOR(mask, 0, 1 << P)
					ndp[j][last][mask] = INF;
		FOR(j, 0, i + 1)
		{
			FOR(last, 0, P + 1)
			{
				FOR(mask, 0, 1 << P)
				{
					updMin(ndp[j][a[i]][mask & ~(1 << a[i])], dp[j][last][mask] + (last != a[i]));
					updMin(ndp[j + 1][last][mask], dp[j][last][mask]);
				}
			}
		}
		memcpy(dp, ndp, sizeof dp);
	}
	int ans = INF;
	FOR(j, 0, k + 1)
		FOR(last, 0, P + 1)
			FOR(mask, 0, 1 << P)
				updMin(ans, dp[j][last][mask] + __builtin_popcount(mask));
	cout << ans << "\n\n";
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	for (int tc = 1; ; tc++)
	{
		cin >> n >> k;
		if (n == 0 && k == 0)
			break;
		cout << "Case " << tc << ": ";
		solve();
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 252ms
memory: 5452kb

input:

5 1
25 26 27 28 29
52 29
26 27 31 28 30 27 26 25 32 26 30 32 29 25 27 32 25 27 32 29 31 28 30 29 30 30 27 25 30 25 26 32 28 27 26 32 26 30 28 30 26 26 25 25 28 27 27 26 30 28 27 30
95 58
26 31 31 28 28 26 30 30 32 29 32 31 27 25 30 28 31 32 28 25 30 25 30 29 26 25 26 26 27 28 31 32 26 29 28 25 31 26...

output:

Case 1: 5

Case 2: 9

Case 3: 11

Case 4: 8

Case 5: 9

Case 6: 4

Case 7: 22

Case 8: 8

Case 9: 8

Case 10: 39

Case 11: 8

Case 12: 21

Case 13: 9

Case 14: 23

Case 15: 13

Case 16: 23

Case 17: 9

Case 18: 14

Case 19: 8

Case 20: 36


result:

ok 39 lines