【UVA】11400-Lighting System Design(动态规划)
来源:互联网 发布:博客园 吉祥网络 编辑:程序博客网 时间:2024/06/03 19:19
这道题感觉状态式不是很好推。。。 WA了好几次是因为排序的时候出问题了。
这道题出在线性结构里了,先说一下最长上升子序列吧。
dp[i]代表了以array[i]结尾的时候,最长子序列长度。
推导的时候,以起点递增的顺序进行推导。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<map>#include<stack>#include<queue>#include<set>#include<ctime>#include<cmath>#include<string>#include<iomanip>#include<climits>#include<cctype>#include<deque>#include<list>#include<sstream>#include<vector>#include<cstdlib>using namespace std;#define _PI acos(-1.0)#define INF (1 << 20)typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> pill;/*======================================最长上升子序列问题dp[i]代表了以i结尾的序列最长子序列长度======================================*/#define MAXD 10000 + 10int dp[MAXD];int main(){ int array[MAXD]; int n; while(scanf("%d",&n) != EOF){ memset(dp,0,sizeof(dp)); for(int i = 1 ; i <= n ; i++) scanf("%d",&array[i]); dp[1] = 1; for(int i = 1 ; i < n ; i++) for(int j = i + 1 ; j <= n ; j ++){ if(array[j] > array[i]) dp[j] = max(dp[j],dp[i] + 1); else dp[j] = max(dp[j],dp[i]); } printf("%d\n",dp[n]); } return 0;}
第二个就是最长的公共子序列长度。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<map>#include<stack>#include<queue>#include<set>#include<ctime>#include<cmath>#include<string>#include<iomanip>#include<climits>#include<cctype>#include<deque>#include<list>#include<sstream>#include<vector>#include<cstdlib>using namespace std;#define _PI acos(-1.0)#define INF (1 << 20)typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> pill;/*======================================最长公公子序列问题======================================*/#define MAXD 1000 + 10int dp[MAXD][MAXD];int main(){ char str1[MAXD]; char str2[MAXD]; while(scanf("%s",str1 + 1) != EOF){ scanf("%s",str2 + 1); int L1 = strlen(str1 + 1) + 1; int L2 = strlen(str2 + 1) + 1; memset(dp,0,sizeof(dp)); for(int i = 1 ; i <= L1 ; i++) for(int j = 1 ; j <= L2 ; j++){ if(str1[i] == str2[j]){ dp[i][j] = max(dp[i][j],dp[i - 1][j - 1] + 1); } else{ dp[i][j] = max(dp[i - 1][j],dp[i][j - 1]); } } printf("%d\n",dp[L1 - 1][L2 - 1]); } return 0;}
这道题的话,也属于线性推导,d[i]代表了前i种灯泡的最优方略。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<map>#include<stack>#include<queue>#include<set>#include<ctime>#include<cmath>#include<string>#include<iomanip>#include<climits>#include<cctype>#include<deque>#include<list>#include<sstream>#include<vector>#include<cstdlib>using namespace std;#define _PI acos(-1.0)#define INF (1 << 20)typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> pill;/*======================================*/#define MAXD 100 + 10int n,m;int mat[MAXD][MAXD];int dp[MAXD][MAXD]; /*结点i,j到达最后一行最小的距离*/void init(){ for(int i = 0 ; i < n ;i++) dp[m - 1][i] = mat[i][m - 1]; return ;}void DP(){ init(); for(int i = m - 2 ; i >= 0; i--) for(int j = 0 ; j < n ; j++){ int x = dp[i + 1][j % n]; int y = dp[i + 1][(j + 1) % n]; int z = dp[i + 1][(j - 1 + n) % n]; dp[i][j] = min(min(x,y),min(y,z)) + mat[j][i]; } int ans = INF; int pos ; for(int i = 0 ; i < n ; i++){ if(ans > dp[0][i]){ ans = dp[0][i]; pos = i; } } /*根据求出的解打印距离*/ int head = pos; printf("%d",pos + 1); for(int i = 1 ; i < m ; i++){ int row[] = {pos - 1,pos + 1,pos}; if(row[0] < 0) row[0] += n; if(row[1] >= n) row[1] %= n; sort(row,row + 3); for(int j = 0 ; j < 3 ; j++){ if(dp[i][row[j]] == dp[i - 1][pos] - mat[pos][i - 1]){ pos = row[j]; break; } } printf(" %d",pos + 1); } printf("\n%d\n",ans);}int main(){ while(scanf("%d%d",&n,&m) != EOF){ for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < m ; j++) scanf("%d",&mat[i][j]); DP(); } return 0;}
0 0
- 【UVA】11400-Lighting System Design(动态规划)
- UVa - 11400 - Lighting System Design(线性动态规划)
- [中等] UVa OJ 11400 Lighting system design 动态规划
- uva 11400 - Lighting System Design(动态规划 最长上升子序列问题变型)
- uva 11400 Lighting System Design (dp)
- UVa 11400 - Lighting System Design(DP)
- UVa 11400 - Lighting System Design(DP)
- UVa 11400:Lighting System Design(DP)
- uva 11400 Lighting System Design(dp~)
- UVA 11400 Lighting System Design
- UVA 11400 Lighting System Design
- uva 11400 Lighting System Design
- uva 11400 Lighting System Design
- UVa - 11400 - Lighting System Design
- UVA - 11400 Lighting System Design
- uva 11400 - Lighting System Design
- uva 11400 Lighting System Design
- uva 11400 Lighting System Design
- 你所不知道的地震APP
- CKEditor和UEditor使用比较
- hdu C++模版题目(优先队列)
- Linux远程下载文件的两种方法之 ftp命令和scp命令
- Linux wext和nl80211接口简介
- 【UVA】11400-Lighting System Design(动态规划)
- Java中的ReentrantLock和synchronized两种锁定机制的对比
- 猜数字游戏
- hd 2027 统计元音
- Swiftly语言学习1
- 最奇特的编程语言特征
- 设计模式——原型设计模式
- seo优化反向链接关于上网站权重和排名要素的反映综合
- Android缓存处理