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;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值