Greedy Algorithm: Detailed Implementation of the C language and detailed Greedy Algorithm

Source: Internet
Author: User

Greedy Algorithm: Detailed Implementation of the C language and detailed Greedy Algorithm
Greedy Algorithm

Greedy algorithms mean that when solving a problem, they always make the best choice for the moment. That is to say, without considering the overall optimization, what he makes is only a local optimal solution in a certain sense.

The greedy algorithm does not obtain the overall optimal solution for all problems, but it can produce the overall optimal solution or the approximate solution of the overall optimal solution for many problems with a wide range. The basic idea of greedy algorithms is as follows:

1. Create a mathematical model to describe the problem.

2. Divide the problem to several subproblems.

3. solve each subproblem and obtain the local optimal solution of the subproblem.

4. Merge the local optimal solution of the subproblem into a solution of the original solution.

The process of implementing this algorithm:

Starting from an initial solution to the problem;

While can take a step forward towards a given overall goal

Do is used to find a solution element of the feasible solution;

Combine all solution elements into a feasible solution to the problem;

#include "stdio.h"void main(){int act[11][3]={{1,1,4},{2,3,5},{3,0,6},{4,5,7},{6,5,9},{7,6,10},{8,8,11},{9,8,12},{10,2,13},{11,12,14}};greedy(act,11);getch();}int greedy(int *act,int n){int i,j,no;j=0;printf("Selected activities:/n");no=0;printf("Act.%2d: Start time %3d, finish time %3d/n", act[no],act[no+1],act[no+2]);for(i=1;i{no=i*3;if(act[no+1]>=act[j*3+2]){j=i;printf("Act.%2d: Start time %3d, finish time %3d/n", act[no],act[no+1],act[no+2]);}}}

Example

Description:

In the graduation season, many large companies come to the school for recruitment. Job Fairs are scattered in different time periods, xiaoming wants to know how many job fairs he can complete (he cannot interrupt or leave when attending a job fair ).

Input:

The first line n has n job fairs. The next n rows have two integers representing the start and end times, represented by the hours starting from on the first day of the job fair.

N <= 1000.

Output:

The maximum number of job fairs.

Sample input:

3

9 10

10 20

8 15

Sample output:

2

Activity Selection Problems

Overview

This problem is the scheduling of several competing job fairs. They all require the use of a public resource (James) in an exclusive manner ). The goal of scheduling is to find the largest set of mutually compatible activities. Here is a collection of n activities that need to use a certain resource (James) S = {a1, a2 ,..., an }. this resource can only be occupied by one activity at a time. Each activity ai has a start time si and end time fi, and 0 <= si

Sort all activities in ascending order of the End Time

Theorem

For any non-empty subproblem Sij, set am to the activity with the earliest End Time in Sij:

Fm = min {fk: ak belongs to Sij}

So,

1) activity am is used in a maximum compatible activity subset of Sij.

2) The sub-problem Sim is empty, so selecting am will make the sub-problem Smj the only sub-problem that may not be empty.

Ac code

#include#include#includestruct join{int begin;int end;};int compare(const void *a, const void *b);int main(){int i, n, k;struct join joins[1001], temp[1001];while(scanf("%d", &n) != EOF){for(i = 0; i < n; i ++){scanf("%d %d", &joins[i].begin, &joins[i].end);}qsort(joins, n, sizeof(joins[0]), compare);k = 0;temp[k] = joins[0];for(i = 1; i < n; i ++){if(joins[i].begin >= temp[k].end)temp[++ k] = joins[i];}printf("%d\n", k + 1);}return 0;}int compare(const void *a, const void *b){const struct join *p = a;const struct join *q = b;return p->end - q->end;}

/*************************************** ***********************

Problem: 1463

User: wangzhengyi

Language: C

Result: Accepted

MS Time: 10

Memory: 904 kb

**************************************** ************************/

Related Article

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.