• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

serendipityx

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

切长条

 

题目描述

给定如图所示的若干个长条。你可以在某一行的任意两个数之间作一条竖线,从而把这个长条切开,并可能切开其他长条。问至少要切几刀才能把每一根长条都切开。样例如图需要切两刀。

注意:输入文件每行的第一个数表示开始的位置,而第二个数表示长度。

输入描述:

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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=32005;
struct node
{
    int a;
    int b;
}arr[maxn];

bool cmp(node x,node y)
{
    if(x.b==y.b)
        return x.a<y.a;
    else
        return x.b<y.b;
}

int main()
{
    int n,i;
    cin>>n;
    for(i=0;i<n;i++)
    {
        //cin>>arr[i].a>>arr[i].b;
        scanf("%d %d",&arr[i].a,&arr[i].b);
        arr[i].b=arr[i].a+arr[i].b;
    }
    sort(arr,arr+n,cmp);
    int ans=0,m=0;
    for(i=0;i<n;i++)
    {
        if(arr[i].a>=m)
        {
            ans++;
            m=arr[i].b;
        }
    }
    cout<<ans<<endl;
    return 0;
}

 

 

posted on 2020-05-25 00:11  Adventurer-  阅读(148)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3