Nail Them+SPOJ+贪心算法

Nail Them

Problem code: QUEST5


To get to the treasure, Jones must complete one more task. He comes across a table, where there are a number of wooden planks lying along the length of the table. He notices that the width of the table is exactly equal to the width of every plank on it. The planks are so heavy that they cannot be manually moved in any way. Some of these wooden planks are overlapping. Jones has a hammer and the Gods grant him infinite nails. The planks have to be joined to the table with nails such that every plank is connected to the table through at least one nail. The nails are of sufficient length, and have to be hammered vertically into the table. One or more planks can be joined to the table through a single nail provided they have a common overlap. Find out the minimum number of nails he needs to nail all planks to the table.

Planks

Input

  • The first line of the input is a positive integer t <= 20, denoting the number of tables.
  • The descriptions of the table follow one after the other.
  • Table description:
    • The first line of the description of the kth table contains a positive integer n (n <= 10010), the number of planks on it.
    • This is followed by n lines containing the description of the planks.
    • The description of each plank is a pair of integers a and b (0 <= a <= b <= 10000010), denoting the distance of the left end and right end of the plank from the left end of the table.

Output

The output must contain t lines , the kth line corresponding to the kth table. The output on the kth line must be an integer ik, the minimum number of nails required.

Example

Input: 


1 5 
3 5 
2 4 

1 4 
4 5

Output: 

1

解决方案:此题为简单的贪心算法,贪心思路:先将每块木板按左坐标从小到大排序,然后在第一块木板上钉钉子,且尽可能使其余的板子固定在第一块板上,直到不能再钉为止,然后再选下一块没有钉的木板重复上面的步骤即可。

code:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{

    int left,right;
} E[10015];
bool cmp(node a,node b)
{

    if(a.left!=b.left)
    {
        return a.left<b.left;
    }
    else
    {
        return a.right<b.right;
    }

}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&E[i].left,&E[i].right);
        }
        sort(E,E+n,cmp);
        int cnt=1;
        int l=E[0].left,r=E[0].right;
        for(int i=1; i<n; i++)
        {

            if(E[i].left>=l&&E[i].left<=r)
            {
               // cout<<l<<" "<<r<<endl;
                l=E[i].left;
                r=min(E[i].right,r);
            }
            else
            {
                cnt++;
                l=E[i].left;
                r=E[i].right;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值