2015 construction of the pyramid in the first game of the US preliminary round of programming, 2015 first game

Source: Internet
Author: User

2015 construction of the pyramid in the first game of the US preliminary round of programming, 2015 first game
 Time limit: 256 ms single point time limit: Ms memory limit: MB

Description

In the quadratic element, the pyramid is an isosceles right triangle with the bottom side on the X axis.

You are a construction contractor in the second world. There are now N Construction Orders, and each order has a revenue of w, that is, building this pyramid can earn a revenue of w. You can choose to build or not to build each order.

The cost of building a pyramid is the area of the pyramid. If two or more pyramids have overlapping areas, the overlapping part of the building only needs to be built once.

The total profit of building a group of pyramid is the sum of income and the cost of deduction. These orders are now given, requesting the maximum profit.

Input

The first act of the input data is an integer T, indicating the number of data groups.

The first behavior of each group of data is an integer N, indicating the number of orders.

In the next N rows, each row has three integers x, y, w, indicating an order. (X, y) indicates the vertex of the constructed pyramid, and w indicates the benefit.

Output

For each group of data, output a line "Case # X: Y", X indicates the data number (starting from 1), Y indicates the maximum profit, rounded to the second digit 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. Solutions: Use interval dp to solve the problem. This question requires the maximum benefit from these orders. First, we can know that we only need to care about the border points on the right of each triangle. In this way, a complete triangle can be contained. Therefore, state (l, r, w) is used to describe all triangles.

Next, define d (j) to indicate the maximum benefit of the range [0, j. We need to calculate the maximum boundary lim in advance, so that the variation range of j is 0 ≤ j ≤ lim. The following describes how to find the state transition equation.

(1) When j ≥ x [I]. when r is used, it indicates that the triangle I is completely included between [0, j], and the greater one that builds it and does not build it. That is, d (j) = max {d (j), d (j) + x [I]. w };

(2) When not meeting (1), but j ≥ x [I]. l, we are concerned with x [I]. the maximum benefit of r. After drawing, it is easy to know that the added benefit value is the benefit w of building the I-th pyramid minus the area of Multi-construction, that is, S (x [I]. l, x [I]. r)-S (x [I]. l, j ). The following State Transition equation is 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 neither of the current two conditions is met, the state transition equation is actually similar to (2), that is, 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 situation: only the benefits of building the I-th pyramid, so finally, take the benefits calculated above and only the larger ones that build the I-th pyramid.

When the maximum benefit value of all intervals is calculated, the answer is the maximum value among them. Note that d (j) must be initialized to an infinitely small value in advance, indicating that no computation has been performed.

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] indicates the maximum benefit of struct atom {int l, r, w in the range of [0, j]; // The left boundary of each triangle, right boundary, return} x [1100]; int n; int compare (atom k1, atom k2) {// return k1.l <k2.l;} in descending order of the left boundary ;} double cal (int k) {// Triangle Area of the bottom edge with a length of k return k * k/4.0;} double solve () {scanf ("% d ", & n); int lim = 0; // lim indicates the maximum right boundary for (int I = 1; I <= n; I ++) {int k1, k2, k3; scanf ("% d", & k1, & k2, & k3); x [I] = atom {k1-k2, k1 + k2, k3 }; lim = max (lim, k1 + k2);} sort (x + 1, x + n + 1, compare ); // follow the left boundary from small to for (int I = 0; I <= lim; I ++) dp [I] =-1e18; // initialize to an infinitely small 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 the maximum unconstructed 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 the net cost, and then w minus the net cost is the extra benefit else dp [x [I]. r] = max (dp [x [I]. r], dp [j]-cal (x [I]. r-x [I]. l) + x [I]. w); // The extra benefit is the benefit of the I-th pyramid w 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 bigger one when building the current user} double ans = 0; for (int I = 0; I <= lim; I ++) ans = max (ans, dp [I]); // return ans from all vertices;} int main () {// freopen ("t.txt", "r ", stdin); int t; scanf ("% d", & t); for (int I = 1; I <= t; I ++) {printf ("Case # % d: %. 2lf \ n ", I, solve ();} 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.