1214 line segment overwrite non-struct, 1214 Line Segment
1214 line segment coverage
Time Limit: 1 s space limit: 128000 KB title level: Gold QuestionDescription
Description
Given N (0 <N <100) line segments on the X axis, each line segment is determined by its two endpoints a_ I and B _ I. I = ,...... N. These coordinates are integers in the range (-999,999. Some line segments overlap or overwrite each other. Write a program to remove as few line segments as possible from the given line segments, so that there is no internal public point between the remaining line segments. The so-called internal public point refers to a point that belongs to two line segments at the same time and is at least within one line segment (that is, excluding the endpoint ).
Input description
Input Description
The first line of the input is an integer N. Next there are N rows, each line has two spaces separated integers, representing the coordinates of the two endpoints of a line segment.
Output description
Output Description
The first line of the output is an integer that represents the maximum number of remaining line segments.
Sample Input
Sample Input
3
6 3
1 3
2 5
Sample output
Sample Output
2
Greedy solution: first, adjust the line segment endpoint to the left endpoint less than or equal to the right endpoint. Second, sort the line segments from small to large Based on the right endpoint. Third, scan them again, the first one that you encounter and the current max don't want to make is the best choice.
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 int a[10001]; 5 int b[10001]; 6 int main() 7 { 8 int n; 9 cin>>n;10 for(int i=1;i<=n;i++)11 {12 cin>>a[i]>>b[i];13 a[i]=a[i]+300;14 b[i]=b[i]+300;15 if(a[i]>b[i])swap(a[i],b[i]); 16 }17 for(int i=1;i<=n;i++)18 {19 for(int j=1;j<n;j++)20 {21 if(b[j]>b[j+1])22 {23 swap(b[j],b[j+1]);24 swap(a[j],a[j+1]);25 }26 }27 28 }29 int ans=0;30 int maxn=-1;31 for(int i=1;i<=n;i++)32 {33 if(a[i]>=maxn)34 {35 ans++;36 maxn=b[i];37 }38 }39 cout<<ans;40 return 0;41 }