Hdu1160 & 1087

Source: Internet
Author: User

I haven't written DP for a long time (about a year), so I can identify it and didn't make enough effort.

In the process of thinking, I was always alert that I could not read the problem-solving report. As a result, it was hard to complete independently. Although it was a simple DP, the gains were not small ~~~~~~

Super
Jumping! Jumping! Jumping!

The largest sum in a one-dimensional space can be a discontinuous sequence, and the largest sum is output.

When deriving the dynamic equation of DP, you only need to find the maximum value of the sum smaller than the current value plus the current value, that is, the largest sum of the current value, record the greatest sum in the entire search process.

Note that the max value is out of the int range.

The DP equation is:

DP [I] = num [I] + max (DP [I-1, I-2... 0]);

Do the problem always wa, the reason is, DP equation error, max (DP [I-1, I-2... 0]), mistakenly written the closest to the current num DP. I did not notice that the closest is not necessarily the biggest.

IfReturns the sum of the maximum continuous sequences.(Negative), output start point, end point. You only need to record and modify the begin value (and when it is less than 0) and end value (and greater than the current maximum and time.

Fatmouse's
Speed

In this example, weight strictly increments and speed strictly decreases the mice sequence.

Analysis, you can use sort to sort speed or weight (record the number of mice) and then calculate the monotonic increasing subsequence for another attribute. And output the sequence and its length.

Find the maximum value of weight or speed of each mice smaller than itself, and update DP to the sequence number of the current mice. Record the largest order number in the entire DP process.

After DP is complete, search for a sequence in reverse order.

Code of 1160:

# Include <iostream> using namespace STD; int num [1001]; long DP [1001]; int main () {int N, I; while (CIN> N & N) {long MX; CIN> num [1]; DP [0] = 0; DP [1] = num [1]; MX = DP [1]; for (I = 2; I <= N; I ++) // DP process {CIN> num [I]; long long Mn = 0; For (Int J = I-1; j> 0; j --) {If (Num [I]> num [J]) {If (DP [J]> Mn) Mn = DP [J] ;}} DP [I] = num [I] + Mn; If (MX <DP [I]) MX = DP [I];} cout <MX <Endl;} return 0 ;}

Code of 1087:

# Include <iostream> # include <algorithm> using namespace STD; struct info {int W; int s; int F;} mice [1001]; bool compare (struct info, struct info B) {if (. s> B. s) return 1; else if (. S = B. s) & (. W <B. w) return 1; else return 0;} int max (int A, int B) {return A> B? A: B;} int main () {int M, N; int K = 1; while (CIN> m> N) {mice [K]. W = m; mice [K]. S = N; mice [K]. F = K ++;} Sort (mice, mice + k, compare); int I, j; int Len = 0; mice [0]. S = 0; for (I = 1; I <K-1; I ++) {mice [I]. S = 0; For (j = 0; j <I; j ++) {If (mice [I]. w> mice [J]. w) mice [I]. S = max (mice [I]. s, mice [J]. S + 1);} If (LEN <mice [I]. s) Len = mice [I]. s;} cout <Len + 1 <Endl; int flag = K-1; // mark the position of the current maximum value int num [1001]; // forward from the back, find from big to small, and then output in reverse order for (I = Len; I> = 0; I --) {for (j = Flag-1; j> = 0; j --) if (mice [J]. S = I) {num [I] = J; flag = J; break ;}}for (I = 0; I <= Len; I ++) // output {cout in sequence <mice [num [I]. F <Endl;} return 0 ;}

Gains:

During DP, observe the relationship between the final result of DP and the first n items, find the dynamic transfer equation, and solve the problem.

Happy birthday and 3.8

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.