HDU 2829 Lawrence(DP+四边形不等式优化)
来源:互联网 发布:画立绘用什么软件 编辑:程序博客网 时间:2024/05/16 16:56
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2829
思路:大神地址:http://www.tuicool.com/articles/nMzAzi6
四边形不等式:http://wenku.baidu.com/view/be418243a8956bec0975e3bf.html
待写代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cstring>#include <climits>#include <cmath>#include <cctype>const int inf = 0x3f3f3f3f;//1061109567typedef long long LL;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;LL dp[1010][1010],cost[1010][1010];int a[1010];int s[1010][1010];int main(){ int n,m; while(scanf("%d%d",&n,&m)) { if(m + n == 0) break; for(int i=1; i<=n; i++) scanf("%d",&a[i]); /*memset(cost,0,sizeof(cost)); for(int i=1; i<=n; i++) { for(int j=i+1; j<=n; j++) { cost[i][j] = cost[i][j-1]; for(int k=i; k<=j; k++) { cost[i][j] += a[j] * a[k]; } } }*/ for(int i=1; i<=n; i++) { LL sum = 0; cost[i][i] = 0; for(int j=i+1; j<=n; j++) { sum += a[j-1]; cost[i][j] = cost[i][j-1] + sum * a[j]; } } for(int i=1; i<=n; i++) { dp[0][i] = cost[1][i]; s[0][i] = 0; s[i][n+1] = n; } for(int i=1; i<=m; i++) { for(int j=n; j>=1; j--)//这层循环是倒着来着,和01背包一个道理 { dp[i][j] = cost[i][j]; for(int k=s[i-1][j]; k<=s[i][j+1]; k++) { dp[i][j] = min(dp[i][j],dp[i-1][k]+cost[k+1][j]); s[i][j] = k; } } } printf("%I64d\n",dp[m][n]); } return 0;}
超时代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cstring>#include <climits>#include <cmath>#include <cctype>const int inf = 0x3f3f3f3f;//1061109567typedef long long LL;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;LL dp[1010][1010],cost[1010][1010];int a[1010];int main(){ int n,m; while(scanf("%d%d",&n,&m)) { if(m + n == 0) break; for(int i=1; i<=n; i++) scanf("%d",&a[i]); /*memset(cost,0,sizeof(cost)); for(int i=1; i<=n; i++) { for(int j=i+1; j<=n; j++) { cost[i][j] = cost[i][j-1]; for(int k=i; k<=j; k++) { cost[i][j] += a[j] * a[k]; } } }*/ for(int i=1; i<=n; i++) { LL sum = 0; cost[i][i] = 0; for(int j=i+1; j<=n; j++) { sum += a[j-1]; cost[i][j] = cost[i][j-1] + sum * a[j]; } } for(int i=1; i<=n; i++) { dp[0][i] = cost[1][i]; } for(int i=1; i<=m; i++) { for(int j=n; j>=1; j--)//这层循环是倒着来着,和01背包一个道理 { dp[i][j] = cost[i][j]; for(int k=i; k<j; k++) { dp[i][j] = min(dp[i][j],dp[i-1][k]+cost[k+1][j]); } } } printf("%I64d\n",dp[m][n]); } return 0;}
0 0
- HDU 2829 Lawrence【dp四边形不等式优化】
- HDU 2829 Lawrence(DP+四边形不等式优化)
- hdu 2829 Lawrence (四边形不等式优化DP)
- HDU 2829 Lawrence(四边形不等式优化DP)
- HDU-2829 Lawrence 【斜率优化DP】【四边形不等式优化】
- hdoj 2829 Lawrence 四边形不等式优化dp
- 【斜率/四边形不等式DP】【hdu 2829】Lawrence
- HDU 2829 Lawrence(四边形优化dp/斜率优化dp)
- Lawrence - HDU 2829斜率优化,四边形不等式优化
- dp专辑 U - Lawrence [ 四边形不等式优化]
- Hdu 2829 Lawrence(dp+四边形优化或斜率优化)
- hdu 2829 Lawrence 四边形优化 dp
- dp四边形优化 Hdu 2829 Lawrence 题解
- HDU 2829 Lawrence(动态规划-四边形不等式)
- hdu 2829 dp+四边形不等式优化
- HDU - 2829 Lawrence(四边形优化)
- hdu 2829 Lawrence (dp斜率优化||四边形优化)
- HDU-2829 Lawrence 四边形优化DP 斜率优化
- SQL server 内存数据库
- opencv中轮廓内部的像素点怎么提取
- MAC编译android studio的JNI .h文件
- Android App卡顿问题分析
- 使用 NSURLSession 开发一个支持后台下载和断点续传的下载工具
- HDU 2829 Lawrence(DP+四边形不等式优化)
- SoapUI测试WSDL文件
- poj3259(最短路)
- Android Cursor自动更新的实现和原理
- Linux——环境变量&flags
- POJ2396 Budget
- HDU 4283 You Are the One 区间DP
- bzoj1355(KMP)
- IOS8中SWIFT 弹出框的显示