#90 C

Source: Internet
Author: User

Problem link http://codeforces.com/contest/119/problem/C

A classic DP problem, even though I knew this I still spent much time on figuring out the solution.

As solving most DP problem, the difficult point is to find out the state transition equation.

Before working on the state transition equation, I need to realize what is the optimum condition, and the problem has

Told me, the maximal possible amount of homework (poor students .)

At first glance, I have found the first State: Pick the MTH course at nth day.

But this is far from the complete state of DP, the key is how many homework of all courses shoshould issue at N-1th day?

Thanks to this forward thinking, I couldn't find the answer for about 2 hours.

The amount of homework which course I can issue is range from a [I] to B [I], and the problem has given me this data.

By backward thinking, for every possible course m in nth Day (some course can never be set to nth Day ),

I need to know what is the maximal amount of homework when course M issue every possible amount of homework (A [I] to B [I], the problem mentions that B [I]-A [I] <= 100, so enumeration is acceptable)

For example, I need to know the total amount of homework when course m in day N issues x homework, if that value is maximal, then I will pick these combination (day n, course M, issue X ). according to the constraint given by the problem, there must be X-K or X/K homework issued at N-1 day by some other courses.

The above example is the key to find the state transition equation. (But I have spent much time on expressing such example, stupid ..)

DP [N] [m] [x] = max {every other courses m' | DP [n-1] [m'] [x-K], DP [n-1] [m'] [x/K]} + x

Other constraint condition just some filters to state transition equation, no big deal.

# Include <stdio. h>
# Include <stdlib. h>

# Define Max_n 51
# Define Max_m 51
# Define # Max_len 101

Typedef Struct Path_info_st {
Int M;
Long Long HT;
} Path_info_st, * path_info_t;

Long Long A [max_m], B [max_m];
Int C [max_m], L [max_m];
Long Long DP [max_n] [max_m] [max_len];
Long Long Help1 [max_m] [max_len];
Long Long Help2 [max_m] [max_len];
Path_info_st path_data [max_n] [max_m] [max_len];
Path_info_t path [max_n];
Bool Can_pick [max_n] [max_m];
Bool Can_pick2 [max_n] [max_m] [max_len];
Int N, m, K;

Int Main (){
Long Long I, J, K, R, p, q, T, T1, T2, T3, T4, T5, T6;
Scanf ( " % D " , & N, & M, & K );
For (I = 0 ; I <m; I ++)
Scanf ( " % I64d % i64d % d " , & (A [I]), & (B [I]), & (c [I]);
For (I = 0 ; I <m; I ++)
L [I] = ( Int ) (B [I]-A [I]);

For (I = 0 ; I <m; I ++ ){
T = L [I];
T1 = A [I];

For (J = 0 ; J <= T; j ++ ){
T2 = t1 + J;
Help1 [I] [J] = T2/K;
}

For (J = 0 ; J <= T; j ++ ){
T2 = t1 + J;
Help2 [I] [J] = t2-K;
}
}

For (I = 0 ; I <m; I ++ ){
T1 = A [I];
T = L [I];
For (J = 0 ; J <= T; j ++)
DP [ 0 ] [I] [J] = t1 + J;

Can_pick [ 0 ] [I] = True ;
}

For (I = 1 ; I <n; I ++ ){
For (J = 0 ; J <m; j ++ ){
T = L [J];
T1 = A [J];
For (K = 0 ; K <m; k ++ ){
If (C [k] <C [J] & can_pick [I- 1 ] [K]) {
T3 = A [k];
T4 = B [k];
For (R = 0 ; R <= T; r ++ ){
T2 = t1 + R;
P = help1 [J] [r];
Q = help2 [J] [r];
If (P> = T3 & P <= T4 & (t2% K) = 0 ){
T5 = DP [I] [J] [r];
T6 = DP [I- 1 ] [K] [p-t3];
If (T6> 0 &&( 0 = T5 | T5 <t2 + T6 )){ // T6> 0 means there is a path to I-1 day set subject K and issues total T6 home task
DP [I] [J] [r] = T2 + T6;
Path_data [I] [J] [r]. M = K;
Path_data [I] [J] [r]. ht = p-t3;
Can_pick [I] [J] = True ;
}
}
If (Q> = T3 & q <= T4 ){
T5 = DP [I] [J] [r];
T6 = DP [I- 1 ] [K] [q-t3];
If (T6> 0 &&( 0 = T5 | T5 <t2 + T6 )){ // T6> 0 means there is a path to I-1 day set subject K and issues total T6 home task
DP [I] [J] [r] = T2 + T6;
Path_data [I] [J] [r]. M = K;
Path_data [I] [J] [r]. ht = q-t3;
Can_pick [I] [J] = True ;
}
}
}
}
}
}
}

K = 0 ;
For (I = 0 ; I <m; I ++ ){
T = L [I];
For (J = 0 ; J <= T; j ++ ){
T2 = DP [n- 1 ] [I] [J];
If (K <t2 ){
K = t2; T3 = I; T4 = J;
}
}
}

If ( 0 = K ){
Printf ( " No \ n " );
Return 0 ;
}

K = 0 , I =1 ;
Printf ( " Yes \ n " );
If (N> 1 ){
Path_info_t Pi = & (path_data [n- 1 ] [T3] [T4]);
For (I = 1 ; I <n- 1 ; I ++ ){
Path [k ++] = PI;
Pi = & (path_data [n-I-1 ] [Pi-> m] [Pi-> HT]);
}
Path [k ++] = PI;

For (I = k- 1 ; I> = 0 ; I --)
Printf ( " % D % i64d \ n " ,( Int ) (Path [I]-> M + 1 ), (Path [I]-> HT + A [path [I]-> m]);
}
Printf (" % D % i64d \ n " ,( Int ) (T3 + 1 ), (T4 + A [T3]);

Return 0 ;
}

The most disgusting part of my code is recording the Path Info, and use it for Backward Tracing.

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.