Poj 3616 (DP), poj3616dp
Question:
Given m intervals and the start, end, and weight m intervals of each interval overlap.
You must select several intervals from these intervals so that the weights and maximum values of these selected intervals are
Requirements: 1. The selected range does not overlap. 2. The interval between the two selected ranges must be at least r.
Ideas:
The DP state in the linear structure is defined as: d [I], which indicates the maximum weight of the I range selected from the previous selection to the I interval.
Transfer:
D [I] = max (d [j] + w [I]); where d [j] should meet end [j] + r <= start [I];
Code:
const int maxn = 1005;int n,m,r;int d[maxn];struct interval{ int s,e,w; bool operator < (const interval ei) const{ return s < ei.s; }}itv[maxn];void init(){ for(int i = 1; i <= m; i++){ scanf("%d%d%d",&itv[i].s,&itv[i].e,&itv[i].w); }}void solve(){ sort(itv+1, itv+1+m); memset(d, 0, sizeof(d)); for(int i = 1; i <= m; i++){ d[i] = max(d[i], itv[i].w); for(int j = 1; j <= i; j++){ if(itv[j].e + r <= itv[i].s){ d[i] = max(d[i], d[j]+itv[i].w); } } } cout << *max_element(d+1, d+1+m) << endl;;}int main(){ while(scanf("%d%d%d",&n,&m,&r) != EOF){ init(); solve(); } return 0;}
The brain should be awake. This simple DP should be able to easily identify its model. The problem of simple linear planning and selection is very simple.
That is, status transfer is neither easy to think about nor difficult.
The most important thing is that I was not able to accurately determine the direction of thinking for this question. At first, I guess it was DP, but I was not completely sure. So I was greedy for a long time and finally felt that there was no way to be greedy.
It's complicated...