训练赛(1---5)A

 

一、题目

 

Description

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.

Sample Input

Input: 


1 5 
3 5 
2 4 

1 4 
4 5

Output: 

1

 
 
二、程序代码
1、正确版(从左端点排序)
 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define INF  0x3f3f3f
#define size 100020

 struct node
{
    int a,b;
}t[size];

int cmp(const node i,const node j)
{
    return i.a<j.a;
}

int main()
{
    int n,m;
    cin>>n;
    while(n--)
    {
        cin >> m;
        for(int i =0;i < m;i++)
        {
            cin>> t[i].a>>t[i].b;
        }
        sort(t,t+m,cmp);
        //cout<< t[0].a<< t[1].a<< t[2].a;
        int x=INF,y=INF,k=1;
        for(int i =0;i<m;i++)
        {
            if(y<t[i].a){
                k++;
                y = t[i].b;
            }
            x= t[i].a;
            if(y>t[i].b)y = t[i].b;

        }
        cout<< k<< endl;
    }
    return 0;
}

 

2、正确版从右端点排序

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10100;
struct Table{
    int l,r;
    bool operator < (const Table &rhs) const{
        return r < rhs.r;
    }
}table[maxn];

int main(){
    int kase,n;
    scanf("%d",&kase);
    while(kase--){
        scanf("%d",&n);
        for(int i = 0;i < n;i++){
            scanf("%d%d",&table[i].l,&table[i].r);
        }
        sort(table,table+n);
        int ans = 0,r = -1;
        for(int i = 0;i < n;i++){
            if(r < table[i].l){
                ans++;
                r = table[i].r;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

3、我当时WA的程序(从左端点排序)

 

#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

struct plank
{
    int a,b;
} p[10015];

bool cmp(struct plank p1,struct plank p2)
{
    if(p1.a<p2.a) return true;
    if(p1.a==p2.a&&p1.b<=p2.b) return true;
    return false;
}

int main()
{
    int t,m = 0,n,k = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d",&p[i].a,&p[i].b);
        }
        m=0;
        sort(p,p+n,cmp);
        for(int i = 0; i < n; i++)
        {
            if(p[i].a > p[m].b)
            {
                m = i;
                k++;
            }
        }
        cout<<k<<endl;
    }
    return 0;
}

 

三、分析错因

 

我忽略了一种情况,就是左端点的值比上一个大,右端点的值又比上一个小。

看上两种正确的程序,会发现,他们都是更新左右端点的值,而我的程序是去更新了整个木块,容易去忽略上一种情况。

转载于:https://www.cnblogs.com/fightfor/p/3905064.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值