2019CCPC Qinhuangdao I Invoker

Meaning of the questions:

It is to conjure skills, a minimum number of symbols and the like.

Ideas:

Linear dp title

After referred dp [i] [6] for the i-th pray evoke skill, ball body is a sequence of three methods minimum number of key state 0 to 5. (That is, the total arrangement of three serving a skill of 6)

Transfer the end of state violence on the enumeration of a skill, then count a few Orb is reusable, take a best value on the line.

Z1 pretreatment time for the i-th bit of skill arranged proceeds to state j th z2 skills required number of steps array dis

Write about the full array, and then write a function of structured programming, implementation is also relatively simple.

Code:

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 10;
int dis[10][10][6][6];
const int INF = 0x3f3f3f3f;
ll dp[N][6];
int get_id(char ch) {
    if (ch == 'Y')return 0;
    if (ch == 'V')return 1;
    if (ch == 'G')return 2;
    if (ch == 'C')return 3;
    if (ch == 'X')return 4;
    if (ch == 'Z')return 5;
    if (ch == 'T')return 6;
    if (ch == 'F')return 7;
    if (ch == 'D')return 8;
    if (ch == 'B')return 9;
}
string str[10] = { "QQQ","QQW","QQE","WWW","QWW","WWE","EEE","QEE","WEE","QWE" };
string get_string(int id, int num) {
    string s = str[id];
    string ans;
    int p[3] = { 0,1,2};
    int cnt = 0;
    do {
        if (cnt++ == num) {
            //ans=str[id][0]+str[id][1] + str[id][2];
            ans.push_back(str[id][p[0]]);
            ans.push_back(str[id][p[1]]);
            ans.push_back(str[id][p[2]]);
            return ans;
        }
    }while(next_permutation(p, p + 3));
}
int get_val(string s1, string s2) {
    if (s1 == s2)return 0;
    if (s1[1] == s2[0] && s1[2] == s2[1])return 1;
    if (s1[2] == s2[0])return 2;
    return 3;
}
void init(){
    string s1, s2;
    for(int i=0;i<10;i++)
        for (int j = 0; j < 10; j++) {
            for(int z1=0;z1<6;z1++)
                for (int z2 = 0; z2 < 6; z2++) {
                    s1 = get_string(i, z1); s2 = get_string(j, z2);
                    dis[i][j][z1][z2] = get_val(s1, s2);
                }
        }
}
char s[N];
int n;
int main() {
    while (~scanf("%s", &s)) {
//(hdu上面必须加多组输入)
        n = strlen(s);
        for (int i = 1; i < n; i++)
            for (int j = 0; j < 6; j++)
                dp[i][j] = INF;
        init();
        for (int i = 0; i < 6; i++)dp[0][i] = 4;
        int id1, id2;
        for (int i = 1; i < n; i++) {
            id1 = get_id(s[i - 1]); id2 = get_id(s[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] + dis[id1][id2][k][j] + 1);
                }
        }
        ll ans = dp[n - 1][0];
        for (int i = 1; i < 6; i++) {
            ans = min(ans, dp[n - 1][i]);
        }
        printf("%lld\n", years); 
    } 
    Return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/gzr2018/p/11605385.html