Introduction to algorithms (14): greedy algorithms (1): Activity arrangement problems

Source: Internet
Author: User

For many optimization problems, dynamic planning is a little useless. Sometimes we can use greedy algorithms instead. Greedy Calculation

The method is to generate a global optimal solution through the local optimal selection. What's different from dynamic planning is that it solves each

A subproblem. The issue of activity arrangement can be said to be an entry to greedy algorithms.


When I saw this problem, I first thought of a question I encountered in hangdian ACM when I was a freshman. This summer I didn't talk about AC. This question can be said

The goal is to review the issue of activity arrangement. It is just about how to arrange the most activity, and about the most visible TV programs. But the essence is the same.

. Next, I will explain this question without AC this summer.


When I saw this question, I thought about sorting and then I tried to find it. The start time is sorted by the start time. The result shows that this is meaningless. Because some

The program started very early, but it may be accepted at the latest. In this case, the maximum value cannot be obtained. So it is sorted by the end time, without a doubt,

The one at the top can be viewed. Then, based on this, traverse from the back to find the first program whose start time is later than the end time,

Mark the subscript I of the program in the array, set k = I, and then continue to traverse from the program to find the first program j

A [j]. start> = a [k]. end. Then set k = j to continue traversing. Continue until all programs are traversed. The implementation code is as follows:

# Include <stdio. h> struct Node {int s; int e ;}; int main () {int n; int I, j, k; int count; Node a [100], temp; while (scanf ("% d", & n), n) {for (I = 0; I <n; I ++) {scanf ("% d ", & a [I]. s, & a [I]. e) ;}for (I = 0; I <n-1; I ++) {for (j = 0; j <n-I-1; j ++) {if (a [j]. e> a [j + 1]. e) {temp = a [j]; a [j] = a [j + 1]; a [j + 1] = temp ;}} count = 1; k = 0; for (I = 1; I <n; I ++) {if (a [I]. s> = a [k]. e) {k = I; // mark the selected program count ++;} printf ("% d \ n", count);} return 0 ;}

Today, I read a book to learn that this uses the greedy idea. The following code is written based on the recursive form provided in the book, which means the same.

#include<stdio.h>struct Node{int s;int e;};int count;void fun (Node a[], int i, int n){int m = i + 1;while ((m <= n) && (a[i].e > a[m].s))m++;if (m <= n){count++;fun (a, m, n);}}int main(){int n;int i, j;Node a[100], temp;while (scanf("%d", &n), n){for (i = 0; i < n; i++){scanf("%d%d", &a[i].s, &a[i].e);}for (i = 0; i < n - 1; i++){for (j = 0; j < n - i - 1; j++){if (a[j].e > a[j + 1].e){temp = a[j];a[j] = a[j + 1];a[j + 1] = temp;}}}count = 1;fun(a, 0, n-1);printf("%d\n", count);}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.