Greedy Algorithm (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 algorithms generate a global optimal solution through local optimal selection. What's different from dynamic planning is that it solves every sub-problem in top-down mode. 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. It can be said that this topic is a replica of the issue of activity arrangement. It only talks about how to arrange the most activities, 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. Some programs start very early, but may be accepted at the latest. In this case, the maximum value cannot be obtained. Therefore, the sorting is based on the end time. There is no doubt that the one at the top can be viewed. Then, based on this, it will be traversed from the back, 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 continue traversing from the program, find the first program j and make 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: [cpp] # 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. [Cpp] # 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", & 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) {www.2cto.com 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.