Greedy algorithm--the number of series is very poor

Source: Internet
Author: User

The idea of greedy algorithm is to use local optimal solution to reach the final global optimal solution. Greedy algorithm use is limited, a problem can not use greed to do, often we have to do the necessary proof. The greedy algorithm strategy has no direction, that is, after the state of the current phase is determined, it is not affected by the later stage state.

Now let's start with a problem that can be used with greedy algorithms--the sequence is very poor.

Problem Description: write a sequence of n positive integers on the blackboard, and do the following: each time it erases two numbers a and B, and then adds a number a*b+1 to the series, so that until there is a number left on the blackboard, Max Max, in all the numbers that are finally obtained by this operation, The minimum is min, then the extreme difference of the sequence is defined as m=max-min.

Thinking Analysis: For example, a series of 2,4,6. According to the definition of extreme difference, we calculate the extreme difference of this series. He has three kinds of situations: (2*4+1) *6+1=55, (4*6+1) *2+1=51, (6*2+1) *4+1=53. We find that yourselves calculates two smaller numbers, which is the maximum value, and the minimum value is obtained when the yourselves calculates two large numbers. Therefore, we follow this idea to solve this problem. The problem is to find the minimum and maximum values, and then subtract them, so we need to copy the input array in reverse order. As for why it is in reverse order, the code explains it. When we find the maximum and minimum two numbers, we record their position. However, after that, these two numbers will not be used, so we overwrite them with the calculated value.

Greedy method. CPP: Defines the entry point of the console application. vs2010//absolute greed include "stdafx.h" int s1,s2;void min2 (int a[],int n) {int j;if (a[0]>a[1]) {s1=0;//s1 points to the largest number s2=1;} else {s1=1;s2=0;} for (j=2;j<=n;j++) {if (A[j]>a[s1]) {s2=s1;s1=j;;} else if (A[j]>a[s2]) {s2=j;}}} int caluatemin (int a[],int n) {while (n>1) {min2 (a,n); a[s1]=a[s1]*a[s2]+1;a[s2]=a[n];n--;} return (a[0]*a[1]+1);} void max2 (int a[],int n) {
<span style= "White-space:pre" ></span>//b array in reverse order, here is to calculate the maximum value of the time convenient point. The same method is used for calculating the maximum value and calculating the minimum value. int j2=0;if (a[n]<a[n-1]) {s1=n;//s1 points to the smallest
s2=n-1;} Else{s1=n-1;s2=n;} for (j2=n-2;j2>=0;j2--) {if (A[j2]<a[s1]) {s2=s1;s1=j2;} else if (A[j2]<a[s2]) {s2=j2;}}} int Calculatemax (int a[],int n) {while (n>1) {max2 (a,n); a[s1]=a[s1]*a[s2]+1;a[s2]=a[n];n--;} return (a[0]*a[1]+1);} int _tmain (int argc, _tchar* argv[]) {//series Extremum int a[]={1,2,3,4};//here directly initialized, interested children shoes can change their own int b[]={4,3,2,1};//b is an inverse copy array of a. int Min=caluatemin (a,3), int max=calculatemax (b,3);p rintf ("min=%d\n", Min);p rintf ("max=%d\n", Max);p rintf ("Extremum =%d", max-min); return 0;}



Finally, this code has a lot of room for optimization, which is just a starter version.

For example, the maximum minimum of two can be used to sort the heap, and this sort is mentioned in my previous blog.

Second, the two positional records can use pointers, make the code more brief, and so on.

Welcome you to discuss the message!!



Greedy algorithm--the number of series is very poor

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.