1203. Scientific Conference solution report
Give the start and end times of n more meetings and ask the maximum number of meetings a person can attend! The two meetings must be separated by at least one minute!
1 <= n <= 10 W 1 ≤ Ts <Te ≤ 30000.
It's also DP. Here DP seems to have done it, but it's still done with a backpack, but it obviously needs to time out. But what should I do if I see the topic type, which is obviously the dp I want to use? If O (nm) times out, use O (n) or O (m). I first used the enumeration of the number of meeting types, provided that the sorting is performed based on the end time, the end time has already been ++, so I don't need to worry about the issue of interval 1 in the future!
For () dp [nod [I]. t2] = max (dp [nod [I]. t1] + 1, dp [nod [I]. t2]); // but it is obvious that there will be many vacancies in dp! What should I do ?! I thought about using temporary data to record the previous one, and then I decided to update the current one to the previous one if it was not satisfied! But there are bugs ...... So I couldn't update the vulnerability in the most stupid way. I added a for, but it may time out. Fortunately, it didn't time out! So Hu is confused, and A is lost!
Later, I came to the conclusion report to understand that we should not enumerate the number of categories, which will lead to vacancies in dp. What should we do? Enumeration time, so there is no vacancy, but you need to convert the number of types into a time array, that is, record the cnt [j] With j as the end time, what is the latest start time ......
However, I think the best way is to use the thought of a backpack. However, this is time-out. The thought of a backpack helps me understand this question!
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
# Deprecision MAX 100010
Int n, m, k;
Int dp [30010];
Struct Node
{
Int t1, t2;
} Nod [MAX];
Bool cmp (Node n1, Node n2)
{
Return n1.t2 <n2.t2;
}
Int ans;
Int main ()
{
Memset (dp, 0, sizeof (dp ));
Scanf ("% d", & n );
For (int I = 1; I <= n; ++ I)
{
Scanf ("% d", & nod [I]. t1, & nod [I]. t2 );
Nod [I]. t2 ++;
}
Sort (nod + 1, nod + n + 1, cmp );
For (int I = 1; I <n; ++ I)
{// The enumerated type is
Dp [nod [I]. t2] = max (dp [nod [I]. t1] + 1, dp [nod [I]. t2]);
For (int j = nod [I]. t2; j <= nod [I + 1]. t2; ++ j)
{// This for can complete the dp of the intermediate vacancy; or another method is to enumerate the time rather than the type, but the type array must be converted into a time array.
Dp [j] = dp [nod [I]. t2];
}
/// If (dp [nod [I]. t2] = 1) dp [nod [I]. t2] + = cnt;
/// Cnt = dp [nod [I]. t2];
}
Dp [nod [n]. t2] = max (dp [nod [n]. t1] + 1, dp [nod [n]. t2]);
Printf ("% d \ n", dp [nod [n]. t2]); //
Return 0;
}
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
# Deprecision MAX 100010
Int n, m, k;
Int dp [30010];
Struct Node
{
Int t1, t2;
} Nod [MAX];
Bool cmp (Node n1, Node n2)
{
Return n1.t2 <n2.t2;
}
Int ans;
Int main ()
{
Memset (dp, 0, sizeof (dp ));
Scanf ("% d", & n );
For (int I = 1; I <= n; ++ I)
{
Scanf ("% d", & nod [I]. t1, & nod [I]. t2 );
Nod [I]. t2 ++;
}
Sort (nod + 1, nod + n + 1, cmp );
For (int I = 1; I <n; ++ I)
{// The enumerated type is
Dp [nod [I]. t2] = max (dp [nod [I]. t1] + 1, dp [nod [I]. t2]);
For (int j = nod [I]. t2; j <= nod [I + 1]. t2; ++ j)
{// This for can complete the dp of the intermediate vacancy; or another method is to enumerate the time rather than the type, but the type array must be converted into a time array.
Dp [j] = dp [nod [I]. t2];
}
/// If (dp [nod [I]. t2] = 1) dp [nod [I]. t2] + = cnt;
/// Cnt = dp [nod [I]. t2];
}
Dp [nod [n]. t2] = max (dp [nod [n]. t1] + 1, dp [nod [n]. t2]);
Printf ("% d \ n", dp [nod [n]. t2]); //
Return 0;
}
Baidu others' code:
[Cpp]
// Ural 1203 dynamically plan the number of intervals specified by DP to obtain the maximum number of non-Intersecting intervals.
// Recursive formula: f [I] = max (f [I-1], f [g [I]-1] + 1 );
# Include <cstdio>
# Include <string. h>
# Include <utility>
# Include <algorithm>
Using namespace std;
Const int M1 = 111111;
Typedef pair <int, int> pil;
Int N;
Int mxp;
Int f [M1]; // f [I] indicates the time ranges from 0 ~ I maximum number of reports that can be listened
Int g [M1]; // g [I] indicates the latest start time in the report ending with time I.
Pil rpt [M1];
Int main ()
{
Scanf ("% d", & N );
For (int I = 0; I! = N; ++ I)
Scanf ("% d", & rpt [I]. second, & rpt [I]. first );
Mxp = 0;
Memset (g,-1, sizeof (g ));
Sort (rpt, rpt + N );
For (int I = 0; I! = N; ++ I)
{If (I = N-1 | rpt [I]. first! = Rpt [I + 1]. first)
G [rpt [I]. first] = rpt [I]. second;
Mxp = max (mxp, rpt [I]. first );
}
F [0] = 0;
For (int I = 1; I <= mxp; ++ I) // enumeration time
{If (g [I]! =-1)
F [I] = max (f [I-1], f [g [I]-1] + 1 );
Else
F [I] = f [I-1];
}
Printf ("% d \ n", f [mxp]);
Return 0;
}