Topic link
answer
Title
Given ten skills, each skill requires three elements to release, and the three elements can be arranged arbitrarily. Players can store up to three elements in their hands. If you want to acquire new elements, you need to let go of the elements you acquired at the beginning, and you need to use one R
and three corresponding elements to release a certain skill . Assumed that the player has unlimited R
and R
does not occupy the position.
Given an array of skills, find the minimum number of elements required.
Ideas
Dynamic planning!
Each skill has three elements, and the three elements can form six permutations. Using these six permutations for dynamic planning, find the minimum number of elements required and add what you need each time you release the skill, which is what you want R
.
Pay attention to multiple sets of input for this question.
AC code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>
using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
/// mms中使用 0x3f 可以使得其初始化为INF
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ll)(1e9+7)
typedef pair<int, int> P;
int const N = 1e6 + 10;
char in[N];
char ch[20][12][10] = {
{
"QQQ", "QQQ", "QQQ", "QQQ", "QQQ", "QQQ"},
{
"QQW", "QWQ", "WQQ", "QQW", "QWQ", "WQQ"},
{
"QQE", "QEQ", "EQQ", "QQE", "QEQ", "EQQ"},
{
"WWW", "WWW", "WWW", "WWW", "WWW", "WWW"},
{
"QWW", "WQW", "WWQ", "QWW", "WWQ", "WQW"},
{
"WWE", "EWW", "WEW", "WWE", "WEW", "EWW"},
{
"EEE", "EEE", "EEE", "EEE", "EEE", "EEE"},
{
"QEE", "EQE", "EEQ", "QEE", "EEQ", "EQE"},
{
"WEE", "EWE", "EEW", "EEW", "EWE", "WEE"},
{
"QWE", "WQE", "WEQ", "QEW", "EQW", "EWQ"},
};
int dp[N][10];
map<char, int> mp;
int check(int now, int init, int pos_now, int pos_init) {
if (ch[init][pos_init][0] == ch[now][pos_now][0] && ch[init][pos_init][1] == ch[now][pos_now][1] && ch[init][pos_init][2] == ch[now][pos_now][2]) return 0;
else if (ch[init][pos_init][1] == ch[now][pos_now][0] && ch[init][pos_init][2] == ch[now][pos_now][1]) return 1;
else if (ch[init][pos_init][2] == ch[now][pos_now][0]) return 2;
else return 3;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
mp['Y'] = 0;
mp['V'] = 1;
mp['G'] = 2;
mp['C'] = 3;
mp['X'] = 4;
mp['Z'] = 5;
mp['T'] = 6;
mp['F'] = 7;
mp['D'] = 8;
mp['B'] = 9;
while (scanf("%s", in) != EOF){
int n = strlen(in);
mms(dp, 0x3f);
for (int i = 0; i < 6; i++) dp[0][i] = 3;
for (int i = 1; i < n; i++) {
for (int j = 0; j < 6; j++) {
for (int k = 0; k < 6; k++) {
dp[i][j] = min(dp[i][j], dp[i - 1][k] + check(mp[in[i]], mp[in[i - 1]], j, k));
}
}
}
int ans = dp[n-1][0];
for (int i = 0; i < 6; i++) {
ans = min(ans, dp[n - 1][i]);
}
printf("%d\n", ans + n);
}
return 0;
}