Ultraviolet A Problem 10154 Weights and Measures (weight and strength)

Source: Internet
Author: User
Tags dot net
// Weights and Measures (weight and power) // PC/Ultraviolet IDs: 111103/10154, popularity: C, success rate: average level: 3 // verdict: programming challenges-solved, ultraviolet A-accepted // submission date: 2011-10-12 // ultraviolet A run time: 0.080 S // All Rights Reserved (c) 2011, Qiu. Metaphysis # Yeah dot net // I know, up on top you are seeing great sights, // But down at the bottom, we, too, showould have rights. // We turtles can't stand it. our shells will all crack! // Besides, we need food. We are starving! "Groaned Mack. /// Yertle the turtle, dr. seuss // [Problem description] // a turtle named Mack, to avoid being cracked, has enlisted your advice as to // The order in which turtles shoshould be stacked to form Yertle the turtle's throne. // each of the 5,607 turtles ordered by Yertle has a different weight and strength. // your task is to build the largest stack of turtles possible. /// [input] // standa Rd input consists of several lines, each containing a pair of integers // separated by one or more space characters, specifying the weight and strength // of a turtle. the weight of the turtle is in grams. the strength, also in grams, // is the turtle's overall carrying capacity, including its own weight. that is, // a turtle weighing 300g with a strength of 1,000g can carry 700g of turtles // on it S back. there are at most 5,607 turtles. /// [Output] // your output is a single integer indicating the maximum number of turtles that // can be stacked without exceeding the strength of any one. /// [sample input] // 300 1000 // 1000 1200 // 200 600 // 100 101 // [sample output] // 3 ////[ solution] // start with a very simple DP question, sort the turtles in ascending order of load-bearing weight. If the load-bearing weight is the same, the turtles will be placed on the top with a light weight, and then // DP will find the maximum height. The program passed in programming challenges, but it was wa on the ultraviolet. After reading the post on the BBS, I found that the reason why I passed the test on the PC is that the test data is too weak (Khan ...). //// Analyze why the above method is not feasible if the load is the same as the weight. /// In addition to providing the weight that the tortoise can bear, the bearing weight cannot provide more information, such as the two turtles. The weight and strength are as follows: //// (1) 10 50 // (2) 100 120 // if you follow the preceding sorting method, the tortoise (1) should be placed below and (2) should be placed above, A turtle tower can be formed up to 1, but in fact // If (2) is down and (1) is above, a turtle tower with a height of 2 can be formed. Therefore, this sorting method cannot reach the optimal // sub-structure of the tortoise order, so it cannot guarantee that the optimal solution is obtained. //// If the weights are in ascending order and the weights are in the same order as the weights are in ascending order, the tortoise will also get the wrong answer, because the tortoise's load-bearing capacity is not taken into account. // weight, the inverse example is as follows: // (1) 10 1000 // (2) 20 1000 // (3) 30 40 /// after sorting, the maximum height of the turtle tower is 2, which is actually 3. You only need to put (3) on the top. /// If it is in ascending order of power, the power is the same in ascending order of weight, starting from the first turtle, set the newly added turtle to I, from 1 to // I-1 search for turtles whose total weight is less than the load-bearing weight of I, and whose height can be increased, update the total load-bearing weight of turtle I and the maximum Heap/stack height. This method can be used to obtain the correct answer for general test data, but for the following test data, but I cannot // get the correct answer (I used this method before): // (1) 101 101 // (2) 100 201 // (3) 99 300 // (4) 98 301 // (5) 5 302 // The maximum height obtained by the aforementioned algorithm is 3, which will actually (2), (3 ), (4), (5) stack it up to get a 4-bit black/turtle tower. Why is the algorithm ineffective? Because the tortoise (1) can be placed on the tortoise (2) in step 2, the tortoise // (2) has a maximum height of 2, so when dealing with the future turtles, (2) They cannot be placed on other turtles as individual turtles, but they can only be placed as a whole with (1, therefore, we need to record the minimum total weight of a turtle tower that can reach a height of K. In this way, when constructing a turtle Tower, // always select the turtle with the highest stack and the minimum total weight, it is possible to build a higher turtle tower. //// Can I sort data by force? The answer is yes. If there are two turtles, the weights and forces are W1, S1, W2, S2, // and S1 <= S2, the order is as follows: /// W1 S1 // W2 S2 // if the force is S1, the tortoise can support the weight of the tortoise, including W2, then there is S1> = (W1 + W2). Because S2> = S1 // The tortoise with the strength of S2 can always support it, but it is not necessarily the opposite, that is, S2> = (W1 + W2), not necessarily // S1> = (W1 + W2), So sorting by force will always not change the optimal structure. //// To sum up, use a two-dimensional array to record the minimum total weight when I used the first turtle to form a tower with a height of H, set to minweight // [H] [I], with the following transfer equation: // minweight [H] [I] = min {minweight [H] [I-1], minweight [h-1] [I-1] + weight [I]} // the second one is conditional, that is, minweight [h-1] [I-1] + weight [I] <= strength [I]. Similarly, // because there are many turtles, if a two-dimensional array is used, a segment error may occur due to a large array, one-dimensional scrolling array can be used instead of optimized space to avoid segment errors due to excessive memory allocation. Why can we use a one-dimensional array for optimization? This is determined by the transfer equation of the question. At the very beginning, when the number of turtles is 0 and the height is 0, the minimum total weight is of course 0, but it is impossible to stack the height from 1 to the total number of turtles. You can set the minimum total weight to a maxint value. The array elements are as follows: /// minweight [0] [0] 0 // minweight [1] [0] maxint // minweight [2] [0] maxint //...... // minweight [N] [0] maxint // assuming that the minimum total weight of the turtle tower with a height of 1 is calculated when the number of turtles is 1, according to the transfer equation, yes: // minweight [1] [1] = min {minweight [1] [0], minweight [0] [0] + weight [1]} // Of course, the second value is conditional (the condition is assumed here), which can be known from the element of the array, minweight [1] [0] already exists in the // array, and minweight [0] [0] also exists in the array, the calculated value minweight [1] [1] is stored in the same position as the row number of // minweight [1] [1], except that the column number is increased by 1. The element 0 will not be used, so you can skip the column label directly // and turn it into a one-dimensional array: /// minweight [0] minweight [1] minweight [2]... minweight [N] // 0 maxint... maxint /// only when calculating, in order not to overwrite the previous calculated value, each time it is calculated from the last element using the transfer equation. // You can use the same method to optimize the previous ultraviolet A 10069 distinct subsequences. # Include <iostream> # include <algorithm> using namespace STD; # define maxn 5610 # define maxint (1 <20) Class turtle {public: int weight; int strength; bool operator <(const turtle & Other) const {// powerful at the bottom. If (strength! = Other. Strength) return Strength <other. strength; // if the power is the same, the heavy turtle is below. Return weight <Other. weight ;};}; int main (int ac, char * AV []) {turtle turtles [maxn]; int minweight [maxn]; int nturtles = 1, weight, strength; while (CIN> weight> strength) {If (weight> strength) continue; turtles [nturtles ++] = (Turtle) {weight, strength };} // sort by power and weight. Sort (turtles + 1, turtles + nturtles); nturtles --; // assign the initial value. If it is maxint, it is impossible to form a turtle tower with H height. Minweight [0] = 0; For (int h = 1; H <= nturtles; H ++) minweight [H] = maxint; // DP find the maximum height. Int answer = 1; for (INT I = 1; I <= nturtles; I ++) for (INT H = nturtles; h> = 1; h --) {If (minweight [h-1] + turtles [I]. weight <= turtles [I]. strength) minweight [H] = min (minweight [H], minweight [h-1] + turtles [I]. weight); If (minweight [H] <maxint) Answer = max (answer, H);} cout <answer <Endl; 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.