poj 1948二维01背包
来源:互联网 发布:淘宝商品被投诉假货 编辑:程序博客网 时间:2024/06/04 19:18
题意:给出不多于40个小棍的长度,求出用所有小棍组成的三角形的最大面积。
思路:三角形3边求面积,海伦公式:p=(a+b+c)/2;S=p*(p-a)*(p-b)*(p-c);因为最大周长为1600 则三角形的边长小于周长一半800;
则可以用背包思想dp[i][j]代表能组成的两条边i和j。dp为true代表小棍能组成这两条边,第三条边也能求出,最后遍历一次dp数组求最大面积。
代码:
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#define MAXN 815#define INF 1e8using namespace std;bool dp[801][801];int a[44];int n,sum,p;int main(){ while(scanf("%d",&n)!=EOF) { sum=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } memset(dp,0,sizeof(dp)); dp[0][0]=1; p=sum/2; for(int i=0;i<n;i++) { for(int j=p;j>=0;j--) { for(int k=j;k>=0;k--) { if((j>=a[i]&&dp[k][j-a[i]])||(k>=a[i]&&dp[k-a[i]][j])) dp[k][j]=1; } } } int ans=-1; for(int i=p;i>=1;i--) { for(int j=i;j>=1;j--) { if(dp[j][i]) { int c=sum-i-j; if(i + j > c && i + c > j && j + c > i) { double px=(i+j+c)*1.0/2; int temp=(int)(sqrt(px*(px-i)*(px-j)*(px-c))*100); if(ans<temp) ans=temp; } } } } if(ans) printf("%d\n",ans); else printf("-1\n"); } return 0;}
- poj 1948二维01背包
- poj 1948 二维01背包
- poj 1948 二维01背包
- poj 1948 二维背包
- poj 1948 Triangular Pastures(二维01背包)
- poj 1948 Triangular Pastures 二维01背包
- poj 1948 Triangular Pastures 二维01背包
- POJ -- 1948 二维背包问题
- poj 2576二维01背包
- poj 1948 Triangular Pastures (dp 二维01背包)
- 【POJ 1948】Triangular Pastures(dp—二维01背包)
- poj 1948 Triangular Pastures 二维背包
- POJ 1948 Triangular Pastures(二维背包)
- poj 2576 简单 二维背包
- poj 1837 Balance 二维背包
- poj 1948 Triangular Pastures(二维0/1背包)
- POJ 1948 Triangular Pastures 二维 0-1背包
- POJ 1948 (Triangular Pastures)二维背包 DP/DFS
- centos664位版本lamp配置第一步:安装配置mysql5.6.x
- HANA学习资料,视频教程!
- 向上吧,骚年
- SVN操作
- 堆,栈,堆栈的区别
- poj 1948二维01背包
- UINavigationController使用详解
- ElasticSearch优化
- 【HDOJ】1048 -> The Hardest Problem Ever
- iPhone程序连接到真机调试(注册了APP ID及购买了IDP开发者授权的前提下)
- xcode4.2/4.3 配置svn
- 窗口的枚举和查找
- POJ1088—滑雪 记忆化搜索
- JAVA中String与StringBuffer的区别