Sort by time (beginning to end), then DP (i) = Max (DP (i), DP (j) + 1) (J < i && festival J end time before festival I start time) answer = MAX ( DP (i)) (1 <= i <= N)
--------------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#define REP (i, n) for (int i = 0; i < n; i++)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std;const INT MAXN = 10000 + 5;struct Data {int L, R;void Read () {scanf ("%d%d", &l, &r);r + = l-1;}BOOL Operator < (const data &RHS) Const {return R < RHS.R;}};data a[MAXN];int dp[MAXN];int main () {//freopen ("test.in", "R", stdin);int n;cin >> N;Rep (i, N)a[i]. Read ();sort (A, a + N);Rep (i, N) {dp[i] =1;Rep (J, i) if (a[J].R < a[i].l)dp[i] = max (dp[i], dp[j] + 1);}int ans = 0;Rep (i, N)ans = max (ans, dp[i]);cout << ans << "\ n";return 0;}
--------------------------------------------------------------------------------
1664: [Usaco2006 open]county Fair events participate in festival celebrations Time Limit:5 Sec Memory Limit:MB
Submit:262 Solved:190
[Submit] [Status] [Discuss] Description
Farmer John had returned to the county Fair so he can attend the special events (concerts, rodeos, cooking shows, etc.). He wants to attend as many of the N (1 <= n <=) special events as he possibly can. He ' s rented a bicycle so he can speed from one event to the next in absolutely no time @ All (0 time units to go from one Event to the next!). Given a list of events FJ might wish to attend, with their start times (1 <= T <= 100,000) and their durati ONS (1 <= L <= 100,000), determine the maximum number of events that FJ can attend. FJ never leaves an event early.
There are n festivals where each festival has a start time and a duration. The cows want to attend as many festivals as possible and ask how many they can participate. Note that the transfer speed of the cow is very fast and does not take any time.
Input
* Line 1: A single integer, N.
* Lines 2..n+1:each Line contains-space-separated integers, T and L, that describe a event that FJ might attend.
Output
* Line 1: A single integer which is the maximum number of events FJ can attend.
Sample Input 7
1 6
8 6
14 5
19 2
1 8
18 3
10 6
INPUT DETAILS:
Graphic picture of the schedule:
11111111112
12345678901234567890---------This is the timeline.
--------------------
111111 2222223333344
55555555 777777 666
In this figure 1 represents the first festival starting from 1, which lasts 6 hours until 6.
Sample Output 4
OUTPUT DETAILS:
FJ can do no better than to attend events 1, 2, 3, and 4.HINT
Source
Silver
Bzoj 1664: [Usaco2006 open]county Fair events participate in festival celebrations (DP)