HDU 4293 Groups (12-year Chengdu network competition-DP)

Source: Internet
Author: User

Link: Click here ~~
Question:
There are n people, which can be divided into several groups at will. Then each person provides a message indicating the number of people in the group and the number of people in the group. The maximum number of information records is not conflicted.
Solution:
We can regard n people as a range of [1, n].
If the information of each person is a and B, the information indicates that the range S of the group is [a + 1, n-B].
If some people are in a group, and only when their information represents the same range.
When you believe what one of them says, you will surely believe what another person says, so we can combine these same intervals and record the values.
The problem is transformed into several intervals with a valid value distributed in the interval [1, n]. How can I select some non-Intersecting intervals to maximize the sum of weights.

My approach is to first sort by the right endpoint of the interval, and then set dp [I] to indicate that when the I-th interval is selected, the interval [1, S [I]. b] The maximum sum of values that can be obtained.
Dp [I] = num [I] + max {dp [j]} (j <I and S [j]. B <S [I]. ).

Finally, find the largest dp [I] as the solution.
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
Using namespace std;
 
# Define N 505
 
Struct Int
{
Int a, B;
Int (){}
Int (int _ a, int _ B): a (_ a), B (_ B ){}
Bool operator <(const Int & s) const
{
Return B <s. B | B = s. B & a <s.;
}
} S [N];
 
Int num [N] [N], dp [N];
 
Int main ()
{
Int n, a, B;
While (~ Scanf ("% d", & n ))
{
Int m = 0;
Memset (num, 0, sizeof (num ));
Memset (dp, 0, sizeof (dp ));
For (int I = 0; I <n; I ++)
{
Scanf ("% d", & a, & B );
If (a + B> n-1 | num [a + 1] [n-B] = n-B-)
Continue;
If (! Num [a + 1] [n-B])
S [m ++] = Int (a + 1, n-B );
++ Num [a + 1] [n-B];
}
Sort (S, S + m );
Int ans = 0;
For (int I = 0; I <m; I ++)
{
Int mmax = 0;
For (int j = 0; j <I; j ++)
If (S [j]. B <S [I].)
Mmax = max (mmax, dp [j]);
Dp [I] = mmax + num [S [I]. a] [S [I]. B];
Ans = max (ans, dp [I]);
}
Printf ("% d \ n", 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.