CF#367(Div2)C. Hard problem (DP)

    xiaoxiao2024-04-19  8

    C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

    Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.

    Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).

    To reverse the i-th string Vasiliy has to spent ci units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.

    String A is lexicographically smaller than string B if it is shorter than B (|A| < |B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.

    For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.

    Input

    The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of strings.

    The second line contains n integers ci (0 ≤ ci ≤ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string.

    Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed100 000.

    Output

    If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print  - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.

    Examples input 2 1 2 ba ac output 1 input 3 1 3 1 aa ba ac output 1 input 2 5 5 bbb aaa output -1 input 2 3 3 aaa aa output -1 Note

    In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.

    In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is  - 1.

    In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is  - 1.

    题目大意:给n个字符串,你可以将这个字符串进行逆序操作或者不操作,对第i串进行逆序操作要花费ci,问要求n个串按字典序排列最少花费要多少,若不能形成字典序则输出-1.

    解题思路:DP,只要考虑相邻两个串的情况就好了,有四种情况。

    /* *********************************************** ┆ ┏┓   ┏┓ ┆ ┆┏┛┻━━━┛┻┓ ┆ ┆┃       ┃ ┆ ┆┃   ━   ┃ ┆ ┆┃ ┳┛ ┗┳ ┃ ┆ ┆┃       ┃ ┆ ┆┃   ┻   ┃ ┆ ┆┗━┓ 马 ┏━┛ ┆ ┆  ┃ 勒 ┃  ┆       ┆  ┃ 戈 ┗━━━┓ ┆ ┆  ┃ 壁     ┣┓┆ ┆  ┃ 的草泥马  ┏┛┆ ┆  ┗┓┓┏━┳┓┏┛ ┆ ┆   ┃┫┫ ┃┫┫ ┆ ┆   ┗┻┛ ┗┻┛ ┆ ************************************************ */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <climits> using namespace std; #define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++) #define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--) #define pb push_back #define mp make_pair const int inf_int = 2e9; const long long inf_ll = 2e18; #define inf_add 0x3f3f3f3f #define mod 1000000007 #define LL long long #define ULL unsigned long long #define MS0(X) memset((X), 0, sizeof((X))) #define SelfType int SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);} SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;} #define Sd(X) int (X); scanf("%d", &X) #define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;} //#pragma comment(linker, "/STACK:102400000,102400000") string str[100005][2]; LL c[100005],dp[100005][2]; int main() { int n; cin>>n; for(int i=1;i<=n;i++) cin>>c[i]; for(int i=1;i<=n;i++) { cin>>str[i][0]; str[i][1]=str[i][0]; reverse(str[i][1].begin(),str[i][1].end()); } for(int i=1;i<=n;i++) { dp[i][0]=dp[i][1]=inf_ll; for(int j=0;j<2;j++) for(int k=0;k<2;k++) if(str[i][j]>=str[i-1][k]) dp[i][j]=min(dp[i][j],dp[i-1][k]+j*c[i]); } LL res=min(dp[n][0],dp[n][1]); if(res<inf_ll)cout<<res<<endl; else cout<<-1<<endl; return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1288142.html
    最新回复(0)