2015 Programming beauty first game B build pyramid

Source: Internet
Author: User

Time limit:4000msSingle Point time limit:2000msMemory Limit:256MB
Describe

In two-dimensional, the pyramid is a isosceles right triangle on the x-axis of the bottom.

You are a construction contractor in the two-time world. There are now N construction orders, each with a yield of W, that is to build this pyramid to gain the benefits of W. You can choose to build or not build on each order.

The cost of building a pyramid is the area of the pyramid, and if two or more pyramids have overlapping areas, the overlapping parts of the pyramid are constructed only once.

The total profit of building a set of pyramids is the sum of the proceeds minus the cost. These orders are now given and the maximum profit is requested.

Input

The first behavior of the input data is an integer t, which represents the number of data groups.

The first behavior of each set of data is an integer n, which represents the number of orders.

Next n lines, three integers per line x, y, W, represent an order. (x, y) represents the vertex of the constructed pyramid, and W represents the benefit.

Output

For each set of data output one line "case #X: Y", X represents the data number (starting at 1), Y represents the maximum profit, rounded to two digits after the decimal point.

Data range

1≤t≤20

0≤W≤107

Small Data

1≤n≤20

0≤x, y≤20

Big Data

1≤n≤1000

0≤x, y≤1000


Sample input
322 2 36 2 531 1 12 2 33 3 531 1 12 2 33 3 6
Sample output
Case #1:1.00Case #2:0.00Case #3:1.00

2. Problem-Solving ideas: The problem using interval DP solution. The main requirement of these orders is the maximum benefit. The first thing to know is that we only need to care about the right side of each triangle. This is to include a complete triangle. Therefore, for all triangles, use the state (L,R,W) to describe it.

Next, define D (j) to represent the maximum value of the gain on the interval [0,j]. In advance we want to count the maximum boundary Lim, so that the J change range is 0≤j≤lim. Consider how to find the state transition equation.

(1) When J≥X[I].R, it is said that the first triangle is completely included in the [0,j] between, take the construction of it and do not build it proceeds of the larger. i.e. d (j) =max{d (J), D (j) +X[I].W};

(2) When not satisfied (1) But when the J≥X[I].L, we are concerned about the maximum value of X[I].R, after drawing it is easy to know that the value added is to build the income of the pyramid I minus the number of construction area, that is, S (X[I].L,X[I].R)-S (x[i].l,j). The following state transfer equations are obtained: D (X[I].R) =max{d (X[I].R), D (j) +x[i].w-s (X[I].L,X[I].R) +s (X[I].L,X[I].R)};

(3) When the current two are not satisfied, the state transfer equation is in fact similar to (2), i.e. D (X[I].R) =max{d (X[I].R), D (j) +x[i].w-s (X[I].L,X[I].R)};

Finally, do not forget a special case: the proceeds from the construction of the first pyramid, and therefore the above-mentioned gains and the larger of the income of the first pyramid.

When the maximum value of all the intervals is calculated, the answer is the maximum of them. Note that D (j) is initialized to infinity in advance, indicating that no calculations have been made.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std;double dp[2100];//dp[j] = [0,j] The maximum profit within the range struct Atom{int L, R, w;//the left edge of each triangle, the right edge, the yield}x[1100];int n;int compare (Atom K1, Atom K2) {//follow the left border from small to large sort return k1.l& LT;K2.L;} Double cal (int k) {/////length k at the bottom of the Triangle area return k*k/4.0;} Double Solve () {scanf ("%d", &n), int lim = 0;//lim represents the largest right boundary for (int i = 1; I <= n; i++) {int K1, K2, K3; scanf ("%d%d%d ", &k1, &k2, &AMP;K3); X[i] = atom{k1-k2, k1 + k2, K3}; Lim = Max (lim, K1 + K2);} Sort (x + 1, x + n + 1, compare);//follow left border from small to for (int i = 0; I <= Lim; i++) Dp[i] = -1e18;//Initialize to infinity for (int i = 1; i < = N; i++) {for (int j = Lim; J >= 0; j--) if (j>= x[i].r) Dp[j] = max (Dp[j], dp[j] + X[I].W);//When J is greater than or equal to the right boundary, take the construction I pyramid and do not build the maximum value else if (J >= x[i].l) DP[X[I].R] = max (DP [X[I].R], Dp[j]-cal (X[I].R-X[I].L) + cal (J-X[I].L) + X[I].W);//Calculate net cost, then W minus net cost is more out of revenue else DP[X[I].R] = max (DP[X[I].R ], Dp[j]-cal (X[I].R-X[I].L) + X[I].W);//The extra gain is the gain of the first pyramid minus its area DP[X[I].R] = max (DP[X[I].R), x[i].w-cal (x[i].r-x[i ].L));//finally take the larger one to build yourself}double ans = 0;for (int i = 0; I <= Lim; i++) ans = max (ans, dp[i]);//Take the largest of all ranges return ans; int main () {//freopen ("T.txt", "R", stdin); int t; scanf ("%d", &t), for (int i = 1; I <= t; i++) {printf ("Case #%d:%.2 Lf\n ", I, Solve ());} return 0;}

2015 Programming beauty first game B build pyramid

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.