训练赛(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.

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;
}

三、分析错因

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

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

训练赛(1---5)A

时间: 2024-10-10 07:22:17

训练赛(1---5)A的相关文章

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分相同,第一部分与第二部分对称. 现在给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法,求出以第i个点为中心的回文串长度,记录到数组p中 要满足题目所要求的内容,需要使得两个相邻的回文串,共享中间的一部分,也就是说,左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也是一样. 因为我们已经记录下来以

HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&amp;#39;s problem(manacher+二分/枚举)

pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序列,求出该序列中N序列的最大长度. 思路: 来自官方题解:修正了一些题解错别字(误 先用求回文串的Manacher算法.求出以第i个点为中心的回文串长度.记录到数组p中 要满足题目所要求的内容.须要使得两个相邻的回文串,共享中间的一部分,也就是说.左边的回文串长度的一半,要大于等于共享部分的长度,右边回文串也

最后一周第二天训练赛之第二题

试题: B - B Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice SPOJ ICODER Description Mathews uses a brand new 16-bit instruction processor. (Yeah i am being sarcastic!). It has one register (say R) and it su

Dream_Chaser队训练赛第一场 I题

Dream_Chaser队训练赛第一场 I题 题目来自2012成都区域赛 I - Count Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4472 Description Prof. Tigris is the head of an archaeological team who is currently in charge of a

2017后期 第 1 场训练赛

题目依次为 NKOJ 上 P3496 P4236 P3774 P2407 1.数三角形 方法很多, 比如推出三边 x y z 的限制关系, 然后加加减减得到计算式子 不过也可以用观察法, 暴力计算出 n 为 1 至 13 对应的结果为: 0 0 0 1 3 7 13 22 34 50 70 95 125 相邻两数差为: 0 0 1 2 4 6 9 12 16 20 25 30 这些相邻两数相邻差又为: 0 1 1 2 2 3 3 4 4 5 5 找到规律了, 如果结果第 i 项为第 i - 1

早晨训练赛第一场 B题 哈希

早晨训练赛第一场 B题 B - Trees in a Row Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 402B Description The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≤ i 

zzuli训练赛_05_13-K

题意: 是输入N,2<=N<=100000,求N的质因子个数. 样例输入12 5 30 样例输出2 1 3 提示 12 = 2 * 2 * 3 5 = 5 30 = 2 * 3 * 5 解题思路: 就写个判断质数的函数,循环判断就可以,不过不是质数就加,是不同的质数. 具体代码: #include<iostream> #include<cmath> #include<cstring> using namespace std; bool temp[100000

校队训练赛,同时也是HDU4497(数论:素数分解+组合数学)

一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=4497 二.思路 将满足条件的一组x,z,y都除以G,得到x‘,y',z',满足条件gcd(x',y',x') = 1,同时lcm(x',y',x') = G/L.特判,当G%L != 0 时,无解.然后素数分解G/L,假设G/L = p1^t1 * p2^t2 *````* pn^tn.满足上面条件的x,y,z一定为这样的形式.x' = p1^i1 * p2^i2 *```* pn^in.y' =

训练赛(1---5)D

一.题目 Description Sometimes you have to try fighting even though you know that your enemy is very powerful than you. Your hero with initial health H is about to fight against a venomous enemy who has a poisonous value of P. The enemy's poison deals i*

acm集训训练赛A题【签到题】

一.题目 Description After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of nhorizontal and m vertical sticks. An intersection point is any point on the grid which is formed by t