poj 1050 To the Max(DP)做法很多
来源:互联网 发布:numpy攻略 源码 编辑:程序博客网 时间:2024/04/30 02:34
题意: 给你一个n*n的正方形,求这个正方形除自己外的子矩阵所有点的和最大的那个。
做法一:我自己想的嘿嘿嘿!
代码:
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 101;int dp[MAXN][MAXN][MAXN], g[MAXN][MAXN];int main() { int i, j, x, y, k, res = -2000000, n; scanf("%d", &n); for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) scanf("%d", &g[i][j]); for(i = 1; i <= n; i++)//预处理,求出横向长度为1的所有矩阵; for(y = 1; y <= n; y++) for(k = 1; k <= n-y+1; k++) { dp[y][k][i] = dp[y-1][k][i]+g[k+y-1][i]; res = max(res, dp[y][k][i]); } //dp[i][j][k]表示纵向长度为i,起点在(j, k)的所有矩阵中最大的一个; for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) { //枚举每个点 for(y = 1; y <= n-i+1; y++) { //枚举纵向长度 int tmp = dp[y][i][j]; for(x = j+1; x <= n; x++) { //枚举横向长度 if (i == 1 && j == 1 && y == n && x == n) continue; tmp += dp[y][i][x]; dp[y][i][j] = max(dp[y][i][j], tmp); } res = max(res, dp[y][i][j]); } } printf("%d\n", res); return 0;}
// 3000k 125ms
做法二:看的discuss里的:
确实比我想的叼,做的取舍很到位, 做dp题优化内存并不只有滚动数组;
代码+注释:
#include <stdio.h>#include <stdlib.h>int main(){int c,res=0,k;int d[100][100];int s[101],a[100];while(scanf("%d",&c)==1){for(int i=0;i<c;i++){for(int j=0;j<c;j++){scanf("%d",&d[i][j]);}}for(int i=0;i<c;i++){for(int j=(i+1);j<c;j++) //枚举横向长度的种类,也就是从纵坐标为i的点到为j的点;{ //预处理归零s[0]=0;for(int l=0;l<100;l++){a[l]=0;s[l]=0;}for(k=0;k<c;k++)// 枚举纵坐标{for(int m=i;m<=j;m++)// 枚举i到j的横坐标a[k]+=d[m][k];if(s[k]>=0) //如果这上行的和大于零,就给他加到这行;s[k+1]=s[k]+a[k];else //否则上一行是来添麻烦的s[k+1]=a[k];}for(int i=0;i<k;i++){if(res<s[i])res=s[i];}}}printf("%d",res);}}
0 0
- poj 1050 To the Max(DP)做法很多
- poj - 1050 - To the Max(dp)
- POJ 1050 to the max(dp)
- poj 1050 To the Max (dp)
- 【POJ 1050】To the Max(dp)
- POJ 1050 To the Max(DP)
- POJ 1050 To the Max DP
- POJ 1050 To the Max 经典DP
- POJ 1050 To the Max DP
- poj 1050To the Max dp+暴力
- DP训练 POJ-1050 To the Max
- To the Max - POJ 1050 dp
- POJ 1050 To the Max DP题解
- POJ 1050 - To the Max(DP)
- poj-1050-To the Max【DP】
- POJ-1050-To the Max-dp
- [DP]poj 1050 To the Max
- POJ 1050 To the Max【DP】
- L1-019. 谁先倒--Java-pat-天梯赛
- oracle创建用户 赋权
- BZOJ 1798 [Ahoi2009]Seq 维护序列——线段树
- jquery获取与设置元素的宽度与高度
- robotframework 使用自己的python库
- poj 1050 To the Max(DP)做法很多
- android studio生成签名导打包的方法
- Zurmo(十一)Relation之static::OWNED和NOOWNED
- struts2 简介及使用步骤
- iOS-OC-基本控件之UIPageControl
- 从源码解析LinkedList集合
- iOS开发中的并发、串行队列,同步、异步任务
- 三次样条差值
- linked-list-cycle-ii (链表判环 并返回交点)