一二三四五 上山打老虎

NC25136-切长条

题目链接:https://ac.nowcoder.com/acm/problem/25136

题意:给定n个长条的起点和长度,计算最少需要多少个竖线可以切割长条

思路:贪心,
----①
----------②
首先简单的例子,对于局部情况,第二个横线包括第一个横线的时候可以选的竖线很多,但是最优解是第一条线的边界

-----①
-----②
对于这种情况来说,选择边界来说,局部最优解是选二线的右边界,选二线的右边界是对于二线来说最右边的边界,也最容易切割到一线,所以可以按照有边界进行排序然后遍历枚举。

#include<iostream>
#include<algorithm>

using namespace std;
struct node{
    int l,r;
}a[(int)5e4+5];
bool cmp(struct node s,struct node t){
    return s.r<t.r;
}
int main (){
    int n;
    cin>>n;
    int l,r;
    for(int i=0;i<n;i++){
        cin>>a[i].l>>a[i].r;
        a[i].r=a[i].l+a[i].r-1;
    }
    sort(a,a+n,cmp);
    int ans=1,atm=a[0].r;
    for(int i=1;i<n;i++){
        if(atm>=a[i].l&&atm<=a[i].r)continue;
        ans++;
        atm=a[i].r;
    }
    cout<<ans<<endl;
    
    return 0;
}
posted @ 2021-01-26 16:22  黒川川  阅读(96)  评论(0)    收藏  举报