2015-09-21
Milking cows
The main idea is this Bessie cow milk is very diligent, a farmer has a timetable, in n time divided into m time period, each time period Bessie will always produce milk, and then have a certain benefit, and Bessie milk to rest two hours.
This is a very simple DP, because the interval is not duplicated, so we just have to line up, and then from the first time period to the last time period
State transition equation:
DP[I][J]=DP[I-1][J] j<i;
Dp[i][i]=max (Dp[i][i],dp[i][j]+list[i].eff); J<i
It's very simple, I did this problem for two hours????
Why??? Because I'm getting the wrong line!
The code starts with the 1001*1001 matrix, the time is 0ms, but the memory to use 7000 +, with the rolling array time to 16ms, but the memory becomes 132, or is worth
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 #defineMAX (a) (a) > (b)? (a):(B)5 #defineCUTOFF 206 7typedefintPosition;8typedefstructinput9 {Ten intStart_hour; One intEnd_hour; A inteff; - }list; - theLIST list[ +]; - Long Longdp1[1001]; - Long Longdp2[1001]; - + voidQuick_sort (Position, Position); - voidSwap (Position, Position); + voidInsertion_sort (Position, Position); A intMedian_of_three (Position, Position, Position); at voidSearch (Const int,Const int,Const int); - - intMainvoid) - { - intinterval, R, N, I; - in while(~SCANF ("%d%d%d", &n, &interval, &R)) - { to for(i =1; I <= interval; i++) +scanf"%d%d%d", &list[i].start_hour, &list[i].end_hour, &List[i].eff); -list[0].start_hour = Int_min; list[0].end_hour =-R; theQuick_sort (1, interval); * Search (N, interval, R); $ }Panax Notoginseng return 0; - } the + intMedian_of_three (Position left, Position Middle, Position right) A { the if(List[left].start_hour >list[middle].start_hour) + Swap (left, middle); - if(List[left].start_hour >list[right].start_hour) $ Swap (left, right); $ if(List[middle].start_hour >list[right].start_hour) - Swap (middle, right); -Swap (middle, right);//Hide Pivot Element the returnList[right].start_hour; - }Wuyi the voidSwap (Position x, Position y) - { WuList[x].start_hour ^=List[y].start_hour; -List[y].start_hour ^=List[x].start_hour; AboutList[x].start_hour ^=List[y].start_hour; $ -List[x].end_hour ^=List[y].end_hour; -List[y].end_hour ^=List[x].end_hour; -List[x].end_hour ^=List[y].end_hour; A +List[x].eff ^=List[y].eff; theList[y].eff ^=List[x].eff; -List[x].eff ^=List[y].eff; $ } the the voidInsertion_sort (Position left, Position right) the { the Position I, J; - inttmp_s, Tmp_e, Tmp_eff; in for(i = left +1; I <= right; i++) the { thetmp_s =List[i].start_hour; AboutTmp_e =List[i].end_hour; theTmp_eff =List[i].eff; the for(j = i; j > left && list[j-1].start_hour > tmp_s; j--) the { +List[j].start_hour = List[j-1].start_hour; -List[j].end_hour = List[j-1].end_hour; theList[j].eff = List[j-1].eff;Bayi } theList[j].start_hour =tmp_s; theList[j].end_hour =tmp_e; -List[j].eff =Tmp_eff; - } the } the the voidQuick_sort (Position left, Position right) the { -Position mid = (left + right)/2, I, J; the intpivot; the the if(Right-left >CUTOFF)94 { thePivot =Median_of_three (left, Mid, right); thei = left; j =Right ; the while(1)98 { About while(List[++i].start_hour <pivot); - while(List[--j].start_hour >pivot);101 if(I <j)102 Swap (i, j);103 Else Break;104 } the Swap (I, right);106Quick_sort (left, I-1);107Quick_sort (i +1, right);108 }109 ElseInsertion_sort (left, right); the }111 the voidSearch (Const intNConst intIntervalConst intR)113 { the intI, J; the Long LongAns =-1; the Long Long*now = DP2, *prev = dp1, *tmp =NULL;117 118 for(i =1; I <= interval; i++)119 { - for(j = i-1; J >=0; j--)121 {122NOW[J] =Prev[j];123 if(List[i].start_hour-list[j].end_hour >=R)124Now[i] = MAX (Now[i], now[j] +List[i].eff); theAns =MAX (ans, now[i]);126 }127TMP = Now; now = prev; Prev =tmp; - }129printf"%lld\n", ans); the}
Dp:miking time (POJ 3616)