codeup watching TV (C ++)

Title Description

Summer vacation, Xiao Ming finally happy watching TV. But Xiao Ming favorite programs too much, he wants to see as much of the full program.
Now he has put his broadcast schedule favorite TV show to you, you can help him arrange it?

Entry

Test input comprising a plurality of sets of data. The first line of each input is an integer n (n <= 100), the total number of preferred program Xiaoming FIG.
Next n lines, each line of input two integers si and ei (1 <= i <= n), denotes the i th program start and end times, in order to simplify the problem, each time with a positive integer.
When n = 0, the input end.

Export

For each number of input and output can see the complete television program.

Sample input  Copy

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 Copy

5

Problem-solving ideas: This is an interval greedy problem, first of all the program period are arranged in the best case (that is, first scheduled start time of the latest program, if the same start time, end time is scheduled for the first program purpose yes. to ensure that the last program viewed, the program is left in front of the longest; shortest time interval also select a program, such that there are more programs assigned to more); then a first time period as a starting point (a first time period must be optional), traversing the remaining period, a current program period end time <= last period when the program start time, the number of programs can be scheduled by 1, until all programs been traversed. (Vertical sequence program is distinguished by time period after the sorting by time on the time axis is not distinguishable)

code show as below:

#include<bits/stdc++.h>
using namespace std;
struct show{
	int start;
	int end;
};
bool cmp(show a,show b){  // 从开始时间排序 
	if(a.start != b.start) return a.start > b.start;  // 若开始时间不同,开始时间晚的排在最前面 (给前面的节目留下更多的时间) 
    else return a.end < b.end;  // 若开始时间相同,结束时间最早的去(即开始到结束时间间隔最短的)排在前面
}
int main(){
	int n;
	while(scanf("%d",&n),n){
		show showes[n];
		for(int i = 0; i < n; i++){
			scanf("%d %d",&showes[i].start,&showes[i].end);
		}
		sort(showes,showes + n,cmp);
		// 排序好的第一个节目是一定可以选择的
		int ans = 1;  // 已经有一个节目可以选择
		int lastStart = showes[0].start;   
		for(int i = 1; i < n; i++){   // 从第二个节目开始判断是否能完整观看 
			if(showes[i].end <= lastStart){   // 当第一个节目的开始时间 大于等于 下一个节目的结束时间时,符合条件 
				ans++;
				lastStart = showes[i].start;
			} 
		}  
		printf("%d\n",ans);
	}
} 

 Such may sort: ascending order according to the end time, the end time is early in the front row (left to the back of the program more time), and then in descending order according to the start time, the start time is later than the discharge in front (to ensure the shortest time interval); the end time of a program start time is determined less than or equal to the next program, if they meet the condition, the result is incremented by one.

code show as below:

#include<bits/stdc++.h> 
using namespace std;
struct show{
	int start;
	int end;
};
bool cmp(show a,show b){
	if(a.end != b.end) return a.end < b.end;  // 升序 
	else return a.start > b.start;   // 降序 
}
int main(){
	int n;
	while(scanf("%d",&n),n){
		show showes[n];
		for(int i = 0; i < n; i++){
			scanf("%d %d",&showes[i].start,&showes[i].end);
		}
		sort(showes,showes + n,cmp);
		// 排序好的第一个节目是一定可以选择的
		int ans = 1;  // 已经有一个节目可以选择
		int lastEnd = showes[0].end;   
		for(int i = 1; i < n; i++){   // 从第二个节目开始判断是否能完整观看 
			if(showes[i].start >= lastEnd){
				ans++;
				lastEnd = showes[i].end;
			}
		}  
		printf("%d\n",ans);
	}
} 

 

Published 32 original articles · won praise 2 · Views 1612

Guess you like

Origin blog.csdn.net/qq_38969094/article/details/104369104