第六周总结

一、知识总结
(1)滚动数组 二维dp只需两行或两列时,用[1][0]来轮换表示[j], [j-1],从而降低空间复杂度。 j1=1;dp[i][j11]借助是j1的值在1,0间反复换。 (2)省时输入 scanf输入比cin输入节省时间,但输入多种类型 时cin更方便。所以遇到输入量特别大时,如果想 用cin,需要先做处理。 //代码 int main{ cin.tie(0); ios::sync_with_stdio(0); …… …… } // 二、重要代码样例 G-G A project manager wants to determine the number of the workers needed in every month. He does know the minimal number of the workers needed in each month. When he hires or fires a worker, there will be some extra cost. Once a worker is hired, he will get the salary even if he is not working. The manager knows the costs of hiring a worker, firing a worker, and the salary of a worker. Then the manager will confront such a problem: how many workers he will hire or fire each month in order to keep the lowest total cost of the project. Input The input may contain several data sets. Each data set contains three lines. First line contains the months of the project planed to use which is no more than 12. The second line contains the cost of hiring a worker, the amount of the salary, the cost of firing a worker. The third line contains several numbers, which represent the minimal number of the workers needed each month. The input is terminated by line containing a single ‘0’. Output The output contains one line. The minimal total cost of the project. Sample Input 3 4 5 610 9 110 Sample Output 199代码:java#include<iostream> using namespace std; const int INF=0x3f3f3f3f; int dp[20][100000];int main() { int n; while(scanf("%d",&n)&&n) { int salary,fire,hire; int month[20]={0},maxn=0,sum=INF; scanf("%d%d%d",&hire,&salary,&fire); for(int i=1;i<=n;i++) { scanf("%d",&month[i]); maxn=max(maxn,month[i]); } for(int i=1;i<=n;i++) { for(int j=0;j<=maxn;j++) { dp[i][j]=INF; } } for(int i=month[1];i<=maxn;i++) dp[1][i]=(hire+salary)*i; for(int i=2;i<=n;i++) { for(int j=month[i];j<=maxn;j++) { for(int k=month[i-1];k<=maxn;k++) { if(k<=j) dp[i][j]=min(dp[i][j],dp[i-1][k]+ (j-k)*(hire+salary)+k*salary); else dp[i][j]=min(dp[i][j],dp[i-1][k]+ (k-j)*fire+j*salary); } } } for(int i=month[n];i<=maxn;i++) sum=min(sum,dp[n][i]); printf("%d\n",sum); } return 0; }J-J Now here is a matrix with letter ‘a’,‘b’,‘c’,‘w’,‘x’,‘y’,‘z’ and you can change ‘w’ to ‘a’ or ‘b’, change ‘x’ to ‘b’ or ‘c’, change ‘y’ to ‘a’ or ‘c’, and change ‘z’ to ‘a’, ‘b’ or ‘c’. After you changed it, what’s the largest submatrix with the same letters you can make? Input The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 1000) on line. Then come the elements of a matrix in row-major order on m lines each with n letters. The input ends once EOF is met. Output For each test case, output one line containing the number of elements of the largest submatrix of all same letters. Sample Input 2 4 abcw wxyz Sample Output 3 java #include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; const int maxn=1e3+10; char G[maxn][maxn]; int dp[maxn][maxn],area,n,m; void get_area() { for (int i=1; i<=n; i++) { int left[maxn],right[maxn],t; left[1]=1; right[m]=m; for (int j=2; j<=m; j++) { t=j; while (t>1&&dp[i][j]<=dp[i][t-1]) t=left[t-1]; left[j]=t; } for (int j=m-1; j>=1; j--) { t=j; while (t<m&&dp[i][j]<=dp[i][t+1]) t=right[t+1]; right[j]=t; } for (int j=1; j<=m; j++) { area=max(area,(right[j]-left[j]+1)* dp[i][j]); } }}int main(){ while (scanf("%d%d",&n,&m)!=EOF) { for (int i=1; i<=n; i++) { scanf("%s",G[i]+1); } area=0; memset (dp,0,sizeof (dp)); for (int j=1; j<=m; j++) { for (int i=n; i>=1; i--) { if (G[i][j]=='a'||G[i][j]=='w'|| G[i][j]=='y'||G[i][j]=='z') dp[i][j]=dp[i+1][j]+1; else dp[i][j]=0; } } get_area(); memset (dp,0,sizeof (dp)); for (int j=1; j<=m; j++) {for (int i=n; i>=1; i--) {if (G[i][j]=='b'||G[i][j]=='w'|| G[i][j]=='x'||G[i][j]=='z') dp[i][j]=dp[i+1][j]+1; else dp[i][j]=0; } } get_area(); memset (dp,0,sizeof (dp)); for (int j=1; j<=m; j++) { for (int i=n; i>=1; i--) { if (G[i][j]=='c'||G[i][j]=='x'|| G[i][j]=='y'||G[i][j]=='z') dp[i][j]=dp[i+1][j]+1; else dp[i][j]=0; } } get_area(); printf("%d\n",area); } return 0;}三、做题感想 vjudge有几个题,我抄的别人的状态转移方程。 在草稿纸上演算发现dp数组不一定要必须连续 赋值。像G-G题的状态转移方程有些行只有一列 被赋值,其余列都是初值0,而且存在对同一个 变量多次赋值的情况。 另一情况是动态规划中也有题目要用辅助函数, 麻烦的是像J-J一样要多次用到辅助函数。 这几天做动态规划,觉得自己在思维方面很差, 许多状态转移方程都是搜出来的,最核心的部分 自己不会,一定有后患。我应该挤时间把线性 dp的例题做一遍。ppt有较为详细的思路介绍, 有些题也是经典题目,先掌握最大字段和,分配 等基础问题才会有个好底子。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值