Challenge 2.3 (POJ 3616 milking time) __ Challenge Program Design scrubbing

Source: Internet
Author: User

The main effect of the topic:

The problem is that a farmer is milking a cow in order to get the maximum amount of milk produced. Given the n hours of the day, and then the m time period can be milked, every time the milking, the cows will rest r hours, milking every period of

Efficiency is efficiency_i. The largest amount of milking is obtained.

Ideas for solving problems:

This problem, think about greed should be able to, for all the coordinates, find the first end of the point, and then do not meet the overlap in the case to calculate the maximum milk, but this is sometimes the contradiction between greed and DP,

Because I'm looking for the first milking stage, and then the same way to find the next one, it will cause n such a phase and less than a certain value, so this greedy algorithm can not complete the optimal solution of the problem.

Then it comes to the idea of using a DP, defined state Dp[i] to represent the maximum amount of milk that is obtained from the previous phase.

State transition equation: dp[i] = dp[i-1]+a[j].efficiency;

Well, with this equation, just like solving other DP problems, the final output is OK.

Code:

# include<cstdio> # include<iostream> # include<algorithm> # include<cstring> # include< string> # include<cmath> # include<queue> # include<stack> # include<set> # Include<map

> Using namespace std;
# define INF 999999999 # define MAX 1000+4 int Dp[max];

int n,m,r;
    struct Node {int sta;
    int en;
int eff;


}a[max];

int CMP (const node & x,const node & y) {return x.sta < Y.sta;}
            int main (void) {while (cin>>n>>m>>r) {for (int i = 0;i < m;i++) {
            cin>>a[i].sta>>a[i].en>>a[i].eff;
        A[i].en + = r;

        Sort (a,a+m,cmp);
        for (int i = 0;i < m;i++) Dp[i] = A[i].eff; for (int i = 0;i < m;i++) {for (int j = 0;j < i;j++) {if (a[j ].en <= A[i].sta) {Dp[i] = max (dp[i],dp[j]+a[i].eff);
        '} int ans = 0;
        for (int i = 0;i < m;i++) {ans = max (ans,dp[i]);
    } cout<<ans<<endl;
return 0;
 }


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.