码迷,mamicode.com
首页 > 其他好文 > 详细

最后一周训练赛第一题

时间:2014-08-11 17:40:52      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   使用   os   io   

A - Problem A
Time Limit:2000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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.

 

bubuko.com,布布扣

 

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

 首先看见这道题的第一感觉真的是无法下手,除了用最笨的开数组的方法,其他的什么都想不到。但是在将解的过程中说道通过一个右端排序就可以很轻松的将问题解决,自己更是听蒙了。后来自己看见了代码,细细的研究了一会,顿时是豁然开朗。陶叔说这道题的解法之所以想得到是一种经验问题,但是看代码时我真的感觉这个代码是完完全全为这道题而写的,想不到在其他的地方还能够怎么使用这个代码。待会还会看看剩下的一些类似的例题来巩固自己吧。
这里先将别人的代码贴出来,供参考:
#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;
}

 同样是最简单的贪心题,下面有一个几乎一样的实例,希望以后能够将这样一类问题好好的处理好——(建议是在使用贪心的过程中遇到超时等等情况,要求使用二分进行尝试)

hdu2037:点击这里

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;
const int maxn = 24;
struct TV{
    int l,r;
    bool operator < (const TV &rhs) const{
        return r < rhs.r;
    }
}table[maxn];
int main()
{
    int n;
    while(cin>>n&&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;
                //cout<<"table[i].l"<<table[i].l<<"table[i].r"<<table[i].r<<endl;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

最后一周训练赛第一题,布布扣,bubuko.com

最后一周训练赛第一题

标签:des   style   blog   http   color   使用   os   io   

原文地址:http://www.cnblogs.com/tianxia2s/p/3905011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!