poj 1948 Triangular Pastures(01背包+暴力)
来源:互联网 发布:python玫瑰花代码 编辑:程序博客网 时间:2024/06/06 02:37
题目链接:poj 1948 Triangular Pastures
题目大意:给出若干个木棍的长度,要求用这些木棍组成一个三角形,求最大面积,不能组成输出-1.
解题思路:dp[i][j]表示长度为i和j的边是否能同时被组成,用01背包去计算出所有的可组成情况,另一条边就用s(和)-i-j;然后就是枚举i和j,维护最大值。
#include <stdio.h>#include <string.h>#include <math.h>const int N = 805;int n, s, dp[N][N];void add (int l) {for (int i = 800; i >= 0; i--) {for (int j = 800; j >= 0; j--) {if (dp[i][j]) {if (i + l < N)dp[i+l][j] = 1;if (j + l < N)dp[i][j+l] = 1;}}}}void init () {s = 0;memset(dp, 0, sizeof(dp));dp[0][0] = 1;int l;for (int i = 0; i < n; i++) {scanf("%d", &l);s += l;add(l);}}int area (double x, double y, double z) {double p = (x*x - y*y + z*z)/(2*z);double h = sqrt(x*x - p*p);return h*z/2 * 100;}void solve () {int ans = -1;for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {if (dp[i][j] == 0) continue;int x = s - i - j;if (i + j <= x || i + x <= j || j + x <= i) continue;int tmp = area(i, j, x);if (tmp > ans)ans = tmp;}}printf("%d\n", ans);}int main () {while (scanf("%d", &n) == 1 && n) {init ();solve ();}return 0;}
1 0
- poj 1948 Triangular Pastures(01背包+暴力)
- poj 1948 Triangular Pastures 01背包
- poj 1948 Triangular Pastures(二维01背包)
- poj 1948 Triangular Pastures 二维01背包
- POJ 1948 Triangular Pastures(01背包)
- poj 1948 Triangular Pastures 二维01背包
- POJ 1948 Triangular Pastures(二维背包)
- 【POJ 1948】Triangular Pastures(dp—二维01背包)
- poj 1948 Triangular Pastures (dp 二维01背包)
- POJ 1948 (Triangular Pastures)二维背包 DP/DFS
- poj 1948 Triangular Pastures (二维背包dp)
- POJ 1948 TRIANGULAR PASTURES(背包问题)
- poj 1948 Triangular Pastures 二维背包
- 0-1背包-poj-1948-Triangular Pastures
- poj 1948 Triangular Pastures
- poj 1948 Triangular Pastures
- POJ 1948 Triangular Pastures
- poj 1948 Triangular Pastures
- 想考研的成考毕业生
- 进程与线程的区别
- 将博客搬至CSDN
- 10.2 firebug 调试 js文件
- PHP文件上传类
- poj 1948 Triangular Pastures(01背包+暴力)
- Effective C++——》条款13:以对象管理资源
- 点亮LED
- 如何做到每天写代码?
- 图像的边缘提取
- Pat(Advanced Level)Practice--1069(The Black Hole of Numbers)
- 优秀Unix管理员的七个习惯
- Thinking——写在前面的话
- python中的多进程