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;
}