Meaning of the questions:
With n closed interval [a_i, b_i] number line. Take as few points, such that have at least one point (point may be different sections containing the same) in each interval
Input:
The first line of an integer N (N <= 100)
of 2 ~ N + 1 lines, each two integers a, b (a, b < = 100)
Output:
An integer representing the number of selected points
Example:
Input
2
1 5
4 6
Output
1
Input
3
1 3
2 5
4 6
Output
2
Ideas:
To select the minimum point, so little within each interval. Therefore, the selected point in a plurality of sections should as far as possible. Press the right end to all sections in ascending order, select the right end of the interval, after each selection, the interval point is located entirely removed, start again from the first choice is not the point of the interval until all sections are a little present.
There have been errors of:
When sorting section, the first section of the ascending order starting, the starting point of the end of the same section ascending again, still using the above greedy strategy, this time interval is too long when a first section leads to the second starting point, although a range larger than the first, but the end is smaller than the first interval, the point is not selected at this time the second section and the second section to remove causes an error.
Code:
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
bool cmp(pair<int,int>&a,pair<int,int>&b)
{
if(a.second!=b.second)
return a.second<b.second;
else
return a.first>=b.first;
}
int main(int argc, char** argv) {
int n;
scanf("%d",&n);
vector<pair<int,int>> a;
for(int i=0;i<n;i++)
{
pair<int,int>temp;
cin>>temp.first>>temp.second;
a.push_back(temp);
}
sort(a.begin(),a.end(),cmp);
int count=0;
vector<pair<int,int>>::iterator it=a.begin();
while(!a.empty())
{
int side=it->second;
while(it->first<=side&&!a.empty())
{
a.erase(it);
}
count++;
}
cout<<count;
return 0;
}