2019CCPC秦皇島 I-Invoker(簡單dp)

題幹:

In dota2, there is a hero named Invoker. He has 3 basic skills in the game, which are Quas, Wex and Exort. Once he launches a basic skill, he will gain the corresponding element, where Quas gives “Q”, Wex gives “W” and Exort gives “E”.
Invoker can’t have more than 3 elements simultaneously. If he launches a basic skill when he already owns 3 elements, he will get the corresponding element and lose the element he gained the earliest.
As can be seen, there are 10 unordered combinations of 3 elements in 3 types, each represents a special skill, which are as follows:
• Cold Snap: unordered element combination “QQQ”, denoted by “Y”
• Ghost Walk: unordered element combination “QQW”, denoted by “V”
• Ice Wall: unordered element combination “QQE”, denoted by “G”
• EMP: unordered element combination “WWW”, denoted by “C”
• Tornado: unordered element combination “QWW”, denoted by “X”
• Alacrity: unordered element combination “WWE”, denoted by “Z”
• Sun Strike: unordered element combination “EEE”, denoted by “T”
• Forge Spirit: unordered element combination “QEE”, denoted by “F”
• Chaos Meteor: unordered element combination “WEE”, denoted by “D”
• Deafening Blast: unordered element combination “QWE”, denoted by “B”

When Invoker owns 3 elements, he can launch the invoking skill, denoted by “R”, to gain the special skill according to the elements he currently owns. After invoking, the elements won’t disappear, and the chronological order of the 3 elements won’t change.
Now given a sequence of special skills, you want to invoke them one by one with using the minimum number of basic skills(Q,W,E) and invoking skill®. Print the minimum number in a single line.
At the beginning, Invoker owns no elements. And you should re-invoke the special skills even if you have already invoked the same skills just now.
Input
Input a single line containing a string s (1 ≤ |s| ≤ 100 000) that only contains uppercase letters in {B, C, D, F, G, T, V, X, Y, Z}, denoting the sequence of special skills.
Output
Output a single line containing a positive integer, denoting the minimum number of skills to launch.

Sample Input:
XDTBVV
Sample Output:
15
One possible scheme is QWWREERERWQRQRR.

思路:

"YVG…"代表不同的技能,每個技能有對應的按鍵,按鍵的順序任意,每次需要按下R才能發動技能,發動完技能已有的按鍵不會消失,每次最多存三個按鍵(R不算)。
求最少的按鍵次數。
因爲按鍵可以保留,那麼我們儘量讓本次的按鍵對下次技能的按鍵保留的最多。
但是由於技能之間的按鍵順序可以任意,所以我們dp按鍵組合。
因爲一個技能有三個鍵,所以可以產生六種排序方式(完全相同的也算上)。
dp[i][j]表示第i個技能使用第j種排序方式的按鍵數,這樣我們可以得出方程dp[i][j]=min(dp[i][j],dp[i-1][k]+(第i-1個技能使用第j種排序方式和第i個技能使用第k種排序方式的差值))。
注意需要多組輸入!

#include <bits/stdc++.h>
using namespace std;
char d[10][6][4]={  //補全排序
 {"QQQ","QQQ","QQQ","QQQ","QQQ","QQQ"},
 {"QQW","QWQ","WQQ","WQQ","WQQ","WQQ"},
 {"QQE","QEQ","EQQ","EQQ","EQQ","EQQ"},
 {"WWW","WWW","WWW","WWW","WWW","WWW"},
 {"QWW","WQW","WWQ","WWQ","WWQ","WWQ"},
 {"WWE","WEW","EWW","EWW","EWW","EWW"},
 {"EEE","EEE","EEE","EEE","EEE","EEE"},
 {"QEE","EQE","EEQ","EEQ","EEQ","EEQ"},
 {"WEE","EWE","EEW","EEW","EEW","EEW"},
 {"QWE","QEW","EQW","EWQ","WEQ","WQE"},
};
map<char,int>m;
char ch[100010];
int p[1000100],dp[1000100][6];
int check(int a,int b,int x,int y){//獲得差值
	if(d[a][x][0]==d[b][y][0]&&d[a][x][1]==d[b][y][1]&&d[a][x][2]==d[b][y][2])
		return 0;
	else if(d[a][x][1]==d[b][y][0]&&d[a][x][2]==d[b][y][1])	
		return 1;
	else if(d[a][x][2]==d[b][y][0])
		return 2;
	else 
		return 3;
}
int main()
{
	m['X']=0;m['V']=1;m['G']=2;
	m['C']=3;m['X']=4;m['Z']=5;
	m['T']=6;m['F']=7;m['D']=8;m['B']=9;
	int ans=0,sum=0;
	while(scanf("%s",ch)!=EOF){
		int l=strlen(ch);
		ans=0,sum=0;
		p[sum++]=m[ch[0]];
		for(int i=1;i<l;i++){
			if(m[ch[i]]==p[sum-1]) //相同的技能只需要按R
				ans++;
			else
				p[sum++]=m[ch[i]];
		}
		ans+=sum; //R的數量
		memset(dp,0x3f,sizeof(dp));
		dp[0][0]=3;dp[0][1]=3;dp[0][2]=3;dp[0][3]=3;dp[0][4]=3;dp[0][5]=3;
		for(int i=1;i<sum;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(p[i-1],p[i],k,j));
				}
			}
		}
		int mi=dp[sum-1][0];
		for(int i=0;i<6;i++)
			mi=min(mi,dp[sum-1][i]);
		printf("%d\n",ans+mi);
	}
	
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章