Codeforces-706C Hard problem
来源:互联网 发布:分治算法 编辑:程序博客网 时间:2024/06/05 18:40
题目大意:
给你n个字符串,让你把它排序成字典序,但是任何两个字符串之间不能交换,只能对每个字符串进行翻转操作,并且第i个字符串翻转的时候,会消耗c[i]的能量,然后问你,将这些字符串排成字典序的时候产生的最小能量。如果不能排成字典序则输出-1
解题思路:
明显是个DP,对于每个字符串只有两个选择,翻转和不翻转,搜索的话是2^100000复杂度明显搞笑。亦可赛艇
DP做,就是与前面一个字符串进行比较
可以设dp[i][j]表示第j个字符串为i状态时所消耗的最小能量。i只为0和1
那么状态转移方程就很明显了。不知道状态转移的可以自己小推一下。
代码:
#include <cmath>#include <string>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;//#define DEBUG 1const int maxn = 100005;const long long INF = 1e16;string tmp, str[2][maxn];long long c[maxn], dp[2][maxn];int main() {int n;cin >> n;for (int i = 0; i < n; ++i) cin >> c[i];for (int i = 0; i < n; ++i) {cin >> str[0][i];tmp = str[0][i];reverse(tmp.begin(), tmp.end());str[1][i] = tmp;}dp[0][0] = 0; dp[1][0] = c[0];for (int i = 1; i < n; ++i) {int flag = 0;dp[0][i] = dp[1][i] = INF;if (str[0][i] >= str[0][i - 1] && dp[0][i-1] != -1) {dp[0][i] = dp[0][i - 1];flag = 1;}if (str[0][i] >= str[1][i - 1] && dp[1][i-1] != -1) {dp[0][i] = min(dp[0][i], dp[1][i-1]);flag = 1;}if (!flag) dp[0][i] = -1;flag = 0;if (str[1][i] >= str[0][i - 1] && dp[0][i - 1] != -1) {dp[1][i] = dp[0][i - 1] + c[i];flag = 1;}if (str[1][i] >= str[1][i - 1] && dp[1][i - 1] != -1) {dp[1][i] = min(dp[1][i], dp[1][i-1] + c[i]);flag = 1;}if (!flag) dp[1][i] = -1;}if (dp[0][n - 1] == -1 && dp[1][n - 1] == -1) {cout << "-1" << endl;} else if (dp[0][n - 1] == -1) {cout << dp[1][n - 1] << endl;} else if (dp[1][n - 1] == -1) {cout << dp[0][n - 1] << endl;} else {cout << min(dp[0][n - 1], dp[1][n - 1]) << endl;}#ifdef DEBUGsystem("pause");#endif // DEBUGreturn 0;}
0 0
- Codeforces-706C Hard problem
- CodeForces 706C Hard problem
- 【Codeforces 706C . Hard problem】
- codeforces 706C Hard problem
- Codeforces 706C Hard problem
- Codeforces 706C Hard problem(dp+字符串)
- codeforces 706 C. Hard problem #367 div2
- 【动态规划】Codeforces 706C Hard problem
- Codeforces 706C Hard problem (DP)
- Codeforces 706C Hard problem (DP)
- CodeForces 706C Hard problem【dp】
- CodeForces 706 C. Hard problem(dp)
- codeforces-367# C. Hard problem
- 706C. Hard problem
- CodeForces 706C-Hard problem(基本的DP)
- CodeForces 688C-NP-Hard Problem(dfs)
- CodeForces-688C.NP-Hard Problem
- Codeforces Round #367 C. Hard problem
- 压缩图片
- HDU 5781 ATM Mechine(概率dp)
- win 10 安装Ubuntu和CentOS 感悟
- 旅行_纪中2547_枚举
- 动态规划学习(1)-数字三角形问题
- Codeforces-706C Hard problem
- 2071 Max Num
- Spring 事务
- Hibernate中的五种数据检索方式
- SpringMvc自动任务调度之task实现
- 欢迎使用CSDN-markdown编辑器
- httpUrlConnection 实现向手机号发送验证码
- VS2010编译mod_wsgi-py35-VC10.so(python35和Apache24)
- android -- 蓝牙 bluetooth (一) 入门