Codeforces Round #367 (Div. 2)-C - Hard problem-DP
来源:互联网 发布:kali linux安装输入法 编辑:程序博客网 时间:2024/06/02 06:48
题意:给字符串n个,
能否通过逆序某些字符串,使得最后整体字典序递增。
每个字符串翻转需要花费,输出最小花费使得 整体字典序递增
dp[i][0]表示第i个字符串选择不翻转的花费
dp[i]【1】表示要翻转的花费
简单dp一下就好了。。最后答案判断是否超过IN
if (ss[i]>=ss[i-1]) dp[i][0]=min(dp[i][0],dp[i-1][0]); if (rev_ss[i]>=ss[i-1]) dp[i][1]=min(dp[i][1],c[i]+dp[i-1][0]); if (ss[i]>=rev_ss[i-1]) dp[i][0]=min(dp[i][0],dp[i-1][1; if (rev_ss[i]>=rev_ss[i-1]) dp[i][1]=min(dp[i][1],c[i]+dp[i-1][1]);
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <map>#include <set>#include <vector>#include <iostream>using namespace std;const double pi=acos(-1.0);double eps=0.000001;typedef long long ll;string ss[100123];string rev_ss[100123];ll c[100123];ll dp[100123][2];char tmp[100123];char tt[100123];int ok[100123][2];int main(){ int n; cin>>n; for (int i=1; i<=n; i++) scanf("%lld",&c[i]); for (int i=1; i<=n; i++) { scanf("%s",tmp); ss[i]=tmp; int len=strlen(tmp); for (int j=0; j<len; j++) tt[j]=tmp[len-j-1]; tt[len]=0; rev_ss[i]=tt; } for (int i=1; i<=n; i++) dp[i][0]=dp[i][1]=1e16; dp[0][0]=dp[0][1]=0; ok[0][1] =ok[0][0]=1; for (int i=1; i<=n; i++) { if (ok[i-1][0]) { if (ss[i]>=ss[i-1]) dp[i][0]=min(dp[i][0],dp[i-1][0]),ok[i][0]=1; if (rev_ss[i]>=ss[i-1]) dp[i][1]=min(dp[i][1],c[i]+dp[i-1][0]),ok[i][1]=1; } if (ok[i-1][1]) { if (ss[i]>=rev_ss[i-1]) dp[i][0]=min(dp[i][0],dp[i-1][1]),ok[i][0]=1; if (rev_ss[i]>=rev_ss[i-1]) dp[i][1]=min(dp[i][1],c[i]+dp[i-1][1]),ok[i][1]=1; } } if (ok[n][0]==0&& ok[n][1]==0) { printf("-1\n"); return 0; } printf("%lld\n",min(dp[n][0],dp[n][1])); return 0;}
0 0
- Codeforces Round #367 (Div. 2)-C - Hard problem-DP
- 【Codeforces Round 367 (Div 2) C】【简单DP】Hard problem
- Codeforces Round #367 (Div. 2) C. Hard problem (DP)
- Codeforces Round #367 (Div. 2) C. Hard problem (dp)
- Codeforces Round #367 (Div. 2) C Hard problem(DP)
- Codeforces Round #367 (Div. 2) C. Hard problem (dp)
- Codeforces Round #367 (Div. 2) C. Hard problem(dp)
- Codeforces Round #367 (Div. 2) C. Hard problem (DP)
- Codeforces Round #367 (Div. 2) C Hard problem(dp)
- Codeforces Round #367 (Div. 2) C. Hard problem 朴素dp、线性dp
- Codeforces Round #367 (Div. 2)Hard problem DP
- DP——Hard problem (Codeforces Round #367 (Div. 2) C)
- Codeforces Round #367 (Div. 2) C. Hard problem(简单DP)
- Codeforces Round #367 (Div. 2) C. Hard problem
- Codeforces Round #367 (Div. 2) C. Hard problem
- Codeforces Round #367 (Div. 2)C. Hard problem
- Codeforces Round #367 (Div. 2) [C] Hard Problem
- Codeforces Round #367 (Div. 2) C. Hard problem
- iOS图像处理(5)填充模式
- hdu 4283 You Are the One(区间DP)
- 在CentOS系统上格式化逻辑分区的方法
- [leetcode] 96. Unique Binary Search Trees
- Codeforces Round #367 (Div. 2)-Vasiliy's Multiset-最大异或和-trie
- Codeforces Round #367 (Div. 2)-C - Hard problem-DP
- c语言栈实现括号匹配
- js 性能优化总结
- [leetcode] 382. Linked List Random Node 解题报告
- [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown
- OI算法的时空复杂度及算法模板
- Linux写时拷贝技术(copy-on-write)
- JAVA中I/O的基础知识
- read copy update