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 c i 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 c i (0 ≤ c i ≤ 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 exceed 100 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.
<코드>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100010
#define LL long long
const LL INF = 1e15;
int c[maxn];
string str[maxn][2];
LL dp[maxn][2];
string reverse(string s)
{
string res = s;
int i, len = res.length();
for (i = 0; i < len / 2; ++i)
swap(res[i], res[len - 1 - i]);
return res;
}
int main()
{
int n, i;
while (scanf("%d", &n) != EOF)
{
for (i = 0; i < n; ++i)
scanf("%d", &c[i]);
for (i = 0; i < n; ++i)
{
cin >> str[i][0];
str[i][1] = reverse(str[i][0]);
}
for (i = 0; i < n; ++i)
dp[i][0] = dp[i][1] = INF;
dp[0][0] = 0; dp[0][1] = c[0];
for (i = 1; i < n; ++i)
{
if (str[i][0] >= str[i - 1][0])
dp[i][0] = dp[i - 1][0];
if (str[i][1] >= str[i - 1][0])
dp[i][1] = dp[i - 1][0] + c[i];
if (str[i][0] >= str[i - 1][1])
dp[i][0] = min(dp[i][0], dp[i - 1][1]);
if (str[i][1] >= str[i - 1][1])
dp[i][1] = min(dp[i][1], dp[i - 1][1] + c[i]);
if (dp[i][1] == INF && dp[i][0] == INF)
break;
}
if (i == n)
printf("%I64d\n", min(dp[n - 1][0], dp[n - 1][1]));
else
printf("-1\n");
}
return 0;
}
https://codeforces.com/problemset/problem/706/C
'🧩PS > 🥇Hard' 카테고리의 다른 글
[C/C++] Codeforce 602B - Approximating a Constant Range (0) | 2020.04.30 |
---|---|
[C/C++] Codeforce 607A - Chain Reaction (0) | 2020.04.30 |
[C/C++] Codeforce 327A - Flipping Game (0) | 2020.04.30 |
[C/C++] Codeforce 455A - Boredom (DP) (0) | 2020.04.30 |
[C/C++] Codeforce 189A - Cut Ribbon (DP, Brute Force) (0) | 2020.04.30 |