This summer is not AC (C language realization)

Problem Description

"Not AC this summer?"
"Yes."
"So what are you doing?"
"Look at the World Cup, idiot!"
"@ # $% ^ & *% ..."

Indeed, the World Cup is here, the fans' festival It is also coming, it is estimated that many ACMers will also leave the computer and go to the TV.
As a fan, you must want to watch as many complete games as possible. Of course, as a good young man in the new era, you will definitely watch some other programs, such as news broadcast (never forget to care about national events), very 6 + 7, super Girls, and Wang Xiaoya's "Happy Dictionary", etc., assuming that you already know the broadcast schedule of all the TV programs you like to watch, will you make reasonable arrangements? (The goal is to watch as many full programs as possible)

 

 

Input

The input data contains multiple test instances. The first line of each test instance has only an integer n (n <= 100), which indicates the total number of programs you like to watch, then n lines of data, each line includes two data Ti_s, Ti_e (1 <= i <= n) represents the start and end time of the ith program respectively. To simplify the problem, each time is represented by a positive integer. n = 0 means the input is over and no processing is done.

 

 

Output

For each test instance, the number of TV programs that can be completely viewed is output, and the output of each test instance occupies one line.

 

 

Sample Input

 

12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0

 

 

Sample Output

 

5

 

 

Author

lcy

 

 

Source

ACM final exam (2006/06/07)

#include "stdio.h"

struct node {
	int t1;
	int t2;
};

int main() {
	struct node a[105], temp;
	int n, cnt = 1, i, k;						
	while (scanf("%d", &n) != EOF && n != 0) {
		for (i = 0; i < n; i++) {
			scanf("%d %d", &a[i].t1, &a[i].t2);
		}
		for (i = 0; i < n-1; i++) {				//冒泡排序   先按结束时间排序,在结束时间相等时,再按开始时间晚的排序 
			for (k = 0; k < n - 1; k++) { 		//想让在一定时间内看更多的节目,就需要看节目时间最短的节目 
				if (a[k].t2 > a[k+1].t2) {
					temp = a[k];
					a[k] = a[k+1];
					a[k+1] = temp;
				}
				if (a[k].t2 == a[k+1].t2) {
					if (a[k].t1 < a[k+1].t1) {
						temp = a[k];
						a[k] = a[k+1];
						a[k+1] = temp;
					}
				}
			}
		}
		int t = a[0].t2;
		for (i = 1; i < n; i++) { 			//下一个节目开始时间要在上一个节目结束时 
			if (a[i].t1 >= t) {
				cnt++;
				t = a[i].t2;
			}
		}
		printf("%d\n", cnt);
		cnt = 1;
	}

	return 0;
}

 

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
5
0

--------------------------------
Process exited after 1.603 seconds with return value 0
请按任意键继续. . .

 

Topic: Watch the most programs in a limited time. At the beginning, there was no comprehensive consideration, but the time of the program that appeared first was marked with another array. If there is interspersed in the next time period, the program cannot be completely watched. Given WA, the problem is that the optimal solution is not considered, which is the greedy algorithm.

Algorithm: Greedy algorithm. Sort by end time first, if there is the same end time, continue to sort by the start time late (that is, the shortest duration first)

Published 32 original articles · praised 0 · visits 475

Guess you like

Origin blog.csdn.net/geshifansheng_7/article/details/105066913