POJ3069 POJ2586 Problem Solving report (similar greedy algorithm)

Source: Internet
Author: User

"POJ 3069" (2586 see below)

The original title is here:http://poj.org/problem?id=3069

Main Topic :

there are n points in a line. The distance from point I is XI. Select several plus tags from these points. Requirements: For each point, there must be a marked point (including itself) within the range of R. At least how many points must be marked to satisfy the requirement.

Input: N, R, and the distance from the origin of N points (① does not necessarily follow the order, so it is necessary to sort ② to overlap).

Output: The minimum number of points that are marked.

Problem Solving Ideas:

  From the far left, the marked point must be on the right side of the first point (or itself) within the range of R. To make the whole mark the least point, you should mark the point at the far right of the R range (if none is marked itself). As far as possible to the right radiation.

Similarly, for a labeled point, its right beyond the first point of the range R repeats the above operation until it radiates to all points.

Code:

#include <iostream>#include<algorithm>using namespacestd;intMain () {intR//Range    intN//Number of points    intx[1001];//Record Location     while(1) {cin>> R >>N; if(R = =-1&& N = =-1)//Exit             Break;  for(inti =0; i < N; i++) {cin>>X[i]; } sort (x, x, i);//sort the positions inside.        intn =0, ans =0;  while(n<N) {ints = x[n++];//S is the leftmost point that is not radiated to the left.             while(N < n&&x[n] <= s +R) n++;//Traverse to the first point of the R range beyond s            intp = x[n-1];//P is the point on the new marker             while(N < n&&x[n] <= p +R) n++;//go to the farthest point of the new markerans++; } cout<< ans <<Endl; }    return 0;}

"POJ 2586"

The original title is here: http://poj.org/problem?id=306

Main topic:

< strong> > It's a strange company, it's not profitable every month . Span style= "color: #ff0000;" >s Otherwise a loss D . What's even more interesting is that the company's data are in trouble, just remember that within 12 months of this year, any continuous Five months inside overall are all losses. . Ask what the maximum profit is possible (requires output) .

Input: s,d;

output: If the profit output has the potential maximum profit, if the loss output deficit.

Problem Solving Ideas:

  And 30,691, starting from the far left, each group of five, the amount of 12 months can only be two groups of two. This allows the month of loss in the group as far as possible to the back row, try to radiate!

such as: The Red deeper place is selected as the loss of the month, so that the widest range of radiation

Below, it is necessary to determine how many months of loss, know S and D to determine the number of loss months is not difficult, note, if s≥4*d, must be 12 months all losses. Another point, when 3*d<=2*s and s<4 * d, just have not considered 11, December also began to consider, at this time 11 and December will have a loss month, or choose the last 5 months will not meet test instructions.

Code:

#include <iostream>#include<algorithm>using namespacestd;intProfitintSintd) {    if(D >4*s)return-2*d+Ten*R; Else if(2* d >3*s)return-4*d+8*s; Else if(3* d >2*s)return-6*d+6*s; Else if(4* d >s)return-9*d+3*s; Else        return-1;}intMain () {intS, D;  while(Cin >> S >>d) {intsum =profit (s, d); if(Sum >=0) {cout<< sum <<Endl; }        Else{cout<<"deficit"<<Endl; }    }    return 0;}

  

POJ3069 POJ2586 Problem Solving report (similar greedy algorithm)

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.