1003 jige series story-Temporary Work Plan
Time Limit: 1.0 seconds memory limit: 32768 K
As the saying goes, it is difficult for a hero to pay a penny. After years of high school, jige has already understood this truth. Therefore, it has become a habit to store personal funds for one year since the New Year, however, since college, he has been embarrassed to ask for money from adults. He can only put his only hope on his own. However, due to the special nature of the time period and the factors of his own ability, he can only find some fragmented jobs. He wants to know how to arrange his vacation to get the most salary.
It is known that jige has a total of M days of vacation, and the number of each day ranges from 1 to m, a total of n jobs can be done, each job knows the start time s, end Time e and corresponding salary c. the start time and end time of each job are in the unit of day (number of days). the end time of each job must be the end time to get the total salary C, and there cannot be jobs with overlapping time. For example, a job that starts from 1st days and ends from 2nd days cannot start with 2nd days, and a job that ends from 4th days is selected together, because 2nd days Ji Ge can only work in one place.
Now, gigo wants to know how to get the maximum salary within M days of the holiday (day m + 1 gigo must return to school, M days later ).
Input
The first row is the number of data groups T;
The first row of each group of data is two positive integers: the holiday time m and the number of work that can be done N;
The next n rows have three positive integers describing the start time s, end time e, and total salary C of the corresponding N jobs.
[Technical Specification]
1 <= T <= 1000
9 <m <= 100
0 <n <= 1000
S <= 100, E <= 100, S <= E
C <= 10000
Output
For each group of data, output the highest salary available to jige.
Sampleinput
1
10 5
1 5 100
3 10 10
5 10 100
1 4 2
6 12 266
Sampleoutput
102
I thought it was greedy, but I analyzed it carefully and found it was DP.
Use DP [I] to record the maximum revenue of the previous I days. Then DP [m] is the final solution. Detailed analysis, read the code
# Include <cstdio> # include <algorithm> # include <cstring> # include <vector> using namespace STD; const int n = 110; vector <int> S [N]; // It is used to store the start time of each end time. According to the input, an end time may correspond to several start times, and several wage vectors <int> C [N]; // used to store the salary corresponding to each end time. According to the input, each end time may correspond to different work and salary int n, m, DP [N]; // DP [I] is used to store int X, Y, Z, T; int main () {scanf ("% d ", & T); While (t --) {scanf ("% d", & N, & M); For (INT I = 0; I <= N; ++ I) {s [I]. clear (); C [I]. clear (); DP [I] = 0; // Initialization is 0} For (INT I = 0; I <m; ++ I) {scanf ("% d", & X, & Y, & Z); If (x> Y) continue; If (x <1) continue; if (x> N | Y> N) continue; s [Y]. push_back (x); C [Y]. push_back (z);} For (INT I = 1; I <= N; ++ I) {DP [I] = DP [I-1]; // copy DP [I] to DP [I-1], this step cannot be saved because if day I is not the end date of any work, then the amount of money it can earn is not changed, equal to DP [I-1] I, and s [I] is empty won't enter the next loop for (Int J = 0; j <s [I]. size (); ++ J) DP [I] = max (DP [I], DP [s [I] [J]-1] + C [I] [J]); // If I is the end date of a job, then we should choose this job and do not select this job that earned more; then it must be d [I] to compare, rather than D [I-1], this should pay attention, after each loop, the value of DP [I] may change. If you select this day, the money earned is the total salary of the previous day of the job plus the salary of the job // printf ("% d \ n", I, DP [I]);} printf ("% d \ n", DP [N]);}