思路:典型的贪心法
就是选择不相交区间的问题,建议看紫书
代码
#include<iostream>
#include<sstream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
struct number
{
int q;
int d;
bool operator < (const number b) {
return d < b.d||(d==b.d&&q<=b.q);
}
};
vector<number> v;
void s() {
int num = 1;
int last = v[0].d;
for (int i = 1; i < v.size(); i++) {
if (v[i].d-v[i].q >= last)
{
last = v[i].d;
num++;
}
}
cout << num;
}
int main() {
int n;
cin >> n ;
while (n--) {
number m;
int a,b;
cin >> a >> b;
m.d = b;
m.q = a;
v.push_back(m);
}
sort(v.begin(), v.end());
s();
system("pause");
}