Poj 1948 triangular pastures (DP two-dimensional 01 backpack)

Source: Internet
Author: User

Question:Click Open Link

Question:

Give n sides and make them into a triangle. What is the maximum area? All edges must be used.


Ideas:

For triangles with known perimeter, a third edge can be introduced as long as we know the length of the two sides, so we can get the state equation:
F [I] [J] [k] indicates whether the first I edge can be used to form two edges with a length of J and K.
Initialize f [0] [0] [0] = true;
F [I] [J] [k] = f [I-1] [J-len [I] [k] | f [I-1] [J] [k-len [i];
If f [I] [J] [k] = true, then whether the triangle can be formed using J, K, Sum-J-K as the three sides is calculated, if you can, use the Helen formula to calculate the area.

If you want to open a three-dimensional array, F [40] [1600] [1600] should be enabled, obviously MLE is required, so it must be reduced to two-dimensional,
However, the time complexity is 40*1600*1600. If there is no optimization, it is required by TLE.
Therefore, you need to optimize the running time by Ms ~ Between 1 million MS:

1. f [I] [J] and f [J] [I] are two identical triangles. Therefore, when recursion, j> = I
2. Each side must be half of the perimeter
3. when an I-th edge is enumerated, the first I-th edge cannot be equal to or greater than sum [I] (sum [I] = Len [1] +... + Len [I]) Length

Optimized the time to 100 MS +

Code:

#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<vector>#define SQ(x) ((x)*(x))#define MP make_pairconst int INF = 0x3f3f3f3f;const double PI = acos(-1.0);typedef long long int64;using namespace std;const int MAXN = 42;int n, m;int len[MAXN], sum[MAXN];bool f[1602][1602];inline bool isTriangle(double e[3]){    if(e[2] < e[1]){swap(e[2], e[1]);        if(e[1] < e[0]) swap(e[0], e[1]);}    return e[0]+e[1] > e[2];}inline double getArea(double e[3]){    if(!isTriangle(e)) return -1;    double p = sum[n]/2.0;    return sqrt(p*(p-e[0])*(p-e[1])*(p-e[2]));}int main(){    scanf("%d", &n);    for(int i=1; i<=n; ++i){        scanf("%d", &len[i]);        sum[i] = sum[i-1]+len[i];}    memset(f, 0, sizeof(f));    f[0][0] = true;    double ans = -1;    double e[3];for(int i=1; i<=n; ++i){for(int v1=min(sum[i],sum[n]>>1); v1>=0; --v1){for(int v2=min(sum[i]-v1,sum[n]>>1); v2>=v1; --v2)if(v1+v2<sum[n]){                e[0] = v1; e[1] =v2; e[2] = sum[n]-v1-v2;         if(v1-len[i]>= 0 && f[v1-len[i]][v2]){                    f[v1][v2] = true;}if(!f[v1][v2] && v2-len[i]>=0 && f[v1][v2-len[i]]){    f[v1][v2] = true;}if(f[v1][v2]){                    ans = max(ans, getArea(e));}} }}    if(ans < 0) puts("-1");else printf("%d\n", (int)(100*ans));    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.