链接:https://ac.nowcoder.com/acm/problem/25136
来源:牛客网
题目描述
给定如图所示的若干个长条。你可以在某一行的任意两个数之间作一条竖线,从而把这个长条切开,并可能切开其他长条。问至少要切几刀才能把每一根长条都切开。样例如图需要切两刀。
注意:输入文件每行的第一个数表示开始的位置,而第二个数表示长度。
输入描述:
Line 1: A single integer, N(2 <= N <= 32000)
Lines 2…N+1: Each line contains two space-separated positive integers that describe a leash. The first is the location of the leash’s stake; the second is the length of the leash.(1 <= length <= 1e7)
输出描述:
Line 1: A single integer that is the minimum number of cuts so that each leash is cut at least once.
示例1
输入
7
2 4
4 7
3 3
5 3
9 4
1 5
7 3
输出
2
题意:略。
题记:略。
#include<bits/stdc++.h>
using namespace std;
const int N=32010;
struct Node{
int l,r;
}node[N];
bool cmp(Node a,Node b){
if(a.r==b.r) return a.l<b.l;
return a.r<b.r;
}
int main(){
int n;
cin>>n;
int num,s;
for(int i=0;i<n;i++){
cin>>num>>s;
node[i].l=num,node[i].r=num+s;
}
sort(node,node+n,cmp);
int ans=0,x=0;
for(int i=0;i<n;i++)
if(node[i].l>=x){
x=node[i].r;
ans++;
}
cout<<ans<<endl;
return 0;
}