【动态规划】Codeforces 706C Hard problem

本文解析Codeforces竞赛中一道关于动态规划的难题,涉及字符串翻转成本最小化问题。通过动态规划方法,解决字符串序列调整至字典序的最低成本,提供完整代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

  http://codeforces.com/contest/706/problem/C

题目大意

  n(2 ≤ n ≤ 100 000)个字符串(长度不超过100000),翻转费用为Ci(<=109),求所有字符串从上到下符合字典序从小到大的最小费用。无解输出-1。

题目思路:

  【动态规划】

  每个字符串有2种状态,翻转或者不翻转,每次只与上一个字符串是否翻转有关,可以用DP。

  费用很大,要用long long。

  无解的时候我直接break了WA了好久。

 1 //
 2 //by coolxxx
 3 //
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<memory.h>
 9 #include<time.h>
10 #include<stdio.h>
11 #include<stdlib.h>
12 #include<string.h>
13 //#include<stdbool.h>
14 #include<math.h>
15 #define min(a,b) ((a)<(b)?(a):(b))
16 #define max(a,b) ((a)>(b)?(a):(b))
17 #define abs(a) ((a)>0?(a):(-(a)))
18 #define lowbit(a) (a&(-a))
19 #define sqr(a) ((a)*(a))
20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
21 #define mem(a,b) memset(a,b,sizeof(a))
22 #define eps (1e-8)
23 #define J 10000000
24 #define MAX 0x7f7f7f7f
25 #define PI 3.1415926535897
26 #define N 100004
27 using namespace std;
28 typedef long long LL;
29 int cas,cass;
30 int n,m,lll,ans;
31 LL c[N];
32 LL f[N][2];
33 int l[2];
34 char s[4][N];
35 int main()
36 {
37     #ifndef ONLINE_JUDGE
38 //    freopen("1.txt","r",stdin);
39 //    freopen("2.txt","w",stdout);
40     #endif
41     int i,j,k;
42 //    for(scanf("%d",&cas);cas;cas--)
43 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
44 //    while(~scanf("%s",s))
45     while(~scanf("%d",&n))
46     {
47         mem(f,0x7f);mark=0;
48         for(i=1;i<=n;i++)
49             scanf("%I64d",&c[i]);
50         scanf("%s",s[0]);
51         l[0]=strlen(s[0]);
52         for(i=0;i<l[0];i++)s[2][i]=s[0][l[0]-1-i];
53         f[1][0]=0;f[1][1]=c[1];
54         for(i=2,j=1;i<=n;i++,j^=1)
55         {
56             scanf("%s",s[j]);
57             l[j]=strlen(s[j]);
58             for(k=0;k<l[j];k++)s[j+2][k]=s[j][l[j]-1-k];s[j+2][k]='\000';
59             if(strcmp(s[j],s[j^1])>=0)
60                 f[i][0]=min(f[i-1][0],f[i][0]);
61             if(strcmp(s[j],s[(j^1)+2])>=0)
62                 f[i][0]=min(f[i-1][1],f[i][0]);
63             if(strcmp(s[j+2],s[j^1])>=0)
64                 f[i][1]=min(f[i-1][0]+c[i],f[i][1]);
65             if(strcmp(s[j+2],s[(j^1)+2])>=0)
66                 f[i][1]=min(f[i-1][1]+c[i],f[i][1]);
67         }
68         if(f[n][0]==f[0][0] && f[n][1]==f[0][0])puts("-1");
69         else printf("%I64d\n",min(f[n][0],f[n][1]));
70     }
71     return 0;
72 }
73 /*
74 //
75 
76 //
77 */
View Code

 

转载于:https://www.cnblogs.com/Coolxxx/p/5768931.html

### 关于Codeforces平台上的动态规划问题 在Codeforces这样的编程竞赛平台上,动态规划(Dynamic Programming, DP)是一类非常重要的算法技术。这类题目通常涉及优化子结构和重叠子问题两个特性。 #### 动态规划示例解析 考虑一个典型的DP问题,在给定条件下求解最优方案的数量或具体路径等问题。例如,在某些情况下,可能需要计算达到特定状态所需的最少步数或是最大收益等[^1]。 对于具体的例子而言,假设有一个序列`a[]`,目标是从左到右遍历此序列并决定是否选取当前元素加入集合中,最终目的是让所选元素之和尽可能大而不超过某个上限值M。这个问题可以通过定义二维数组dp[i][j]表示从前i个物品里挑选若干件放入容量为j的背包可以获得的最大价值来建模: - 如果不取第i项,则`dp[i][j]=dp[i−1][j]`; - 若选择第i项且其重量w不超过剩余空间j,则更新为`max(dp[i−1][j], dp[i−1][j-w]+v)`其中v代表该项的价值; 最后的结果保存在`dp[n][m]`处(n为总项目数量,m为目标体积)[^2]。 ```cpp #include <iostream> using namespace std; const int N = 1e3 + 5; int w[N]; // weights of items int v[N]; // values of items long long f[N][N]; void knapsack(int n, int m){ for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { f[i][j] = f[i - 1][j]; if(j >= w[i]) f[i][j] = max(f[i][j],f[i - 1][j - w[i]] + v[i]); } } } ``` 上述代码展示了如何利用记忆化搜索的方式实现简单的0/1背包问题解决方案,这同样适用于其他形式更复杂的动态规划挑战。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值