Poj 3616 (DP), poj3616dp

Source: Internet
Author: User

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...

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.