Greedy algorithm
The so-called greedy algorithm means that when solving a problem, always make the best choice in the present. In other words, not from the overall optimal consideration, what he made is only in a sense of the local optimal solution.
The greedy algorithm can not get the whole optimal solution for all problems, but it can produce the whole optimal solution or the approximate solution of the whole optimal solution for many problems with a wide range. The basic idea of greedy algorithm is as follows:
1. Establish a mathematical model to describe the problem.
2. The problem of solving is divided into several sub issues.
3. To solve each problem, we get the local optimal solution of the sub problem.
4. The solution of the problem of the butt problem is synthesized by the local optimal solution of the original solution.
The process of implementing the algorithm:
Starting from an initial solution of the problem;
While being able to move toward a given total goal
Do to find a solution element of the feasible solution;
A feasible solution that is problematic by the combination of all solution elements;
#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<n;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]);
}
Examples
Topic Description:
In the graduation season, many large companies come to school recruitment, job fairs scattered at different times, Xiaoming want to know how many job fairs they can participate in the most complete (a job fair can not be interrupted or left).
Input:
The first line n, there are n job fairs, and the next n rows are two integers per line representing the starting and ending times, represented by the number of hours from the first day of the job Fair 0 o'clock.
n <= 1000.
Output:
The maximum number of job fairs to attend.
Sample input:
3
9 10
10 20
8 15
Sample output:
2
Activity selection Issues
Overview
This problem is the scheduling of several competing job fairs, all of which require the use of a public resource (xiaoming) in an exclusive manner. The goal of scheduling is to find a single largest set of mutually compatible activities. Here is a collection of n activities that need to use a resource (xiaoming) S={a1,a2,..., an}. The resource can only be occupied by one activity at a time. Each activity Ai has start time Si and end time fi, and 0<=si<fi< infinity. Once selected, the active AI occupies an interval [si,fi]. If the interval [Si,fi] and [sj,fj] do not overlap, call active AI and AJ compatible. The problem with activity selection is to select a maximum set of children that is composed of compatible questions.
Arrange all activities in ascending order of end time
Theorem
For any sij problem, set AM is the activity with the earliest end time in Sij:
Fm=min{fk:ak belongs to Sij}
So
1) Active AM is used in a subset of the maximum compatible activities of the SIJ
2 The child problem sim is empty, so selecting am will make the child issue SMJ as the only possible non-empty child problem
AC Code
#include <stdio.h> #include <stdlib.h> #include <string.h>
struct 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
& nbsp; language:c
result:accepted
time:10 Ms
memory:904 kb
****************************************************************/