最后一周训练赛第一题

A - Problem A

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

Submit Status Practice SPOJ QUEST5

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

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

这里先将别人的代码贴出来,供参考:

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

最后一周训练赛第一题

时间: 2024-08-06 04:32:55

最后一周训练赛第一题的相关文章

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

早晨训练赛第一场 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 

HDU 4864 Task (贪心+STL多集(二分)+邻接表存储)(杭电多校训练赛第一场1004)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4864 解题报告:有n台机器用来完成m个任务,每个任务有一个难度值和一个需要完成的时间,每台机器有一个可以工作的最长时间和一个可以完成的任务的难度的最大值, 一台机器能完成一个任务的条件是这台机器的最长工作时间和能完成任务的难度值必须都大于等于这个任务,而且一台机器最多完成一个任务,假设一个任务的时间为t,难度值为x,那么完成这个任务可以赚到的钱 money = 500 * t + 2 * x; 现在

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

acm集训训练赛B题【排序+模拟】

一.原题 Description Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of ndistinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you

区赛第一题讲解+基础算法——桶排序与快速排序

截止到上篇随笔,我们已经学完了c++中所有的基础语句,这意味着,noip普及组的第一题你已经可以拿满分了.为了纪念这个伟大的时刻,今天要上的这道题,是刚刚考完的海淀区区赛第一题. 题目描述: 已知RFdragon有n个杯子,每个杯子的容积都是无限大,里面都装有1L水.由于RFdragon的杯子实在太多了,他决定扔掉一些杯子,使剩下的杯子不超过k个.RFdragon每次可以把两个装水体积相同的杯子中的水倒在其中一个杯子里,然后扔掉另一个杯子.有时候,RFdragon无论如何也不能使剩下的杯子不超过

中国(北方)大学生程序设计训练赛(第一周)

比赛链接 D题是个二分,每次check复杂度为O(n),类似于xdu_1068,只是一个是求积,一个是求商 #include<bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LF; const LF eps=1e-8; int a[100005],bb[100005]; LF b[100005]; LL A,B; LL K; LL rankof(LF x) { LL ans = 0,no

2016 省赛第一题

Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input 输入包含不超过 30 组数据. 每组数据包含两个整数 n,m (1≤n,m≤109). Output 对于每组数据,输出一个整数表示满足条件的数量. Sample Input 32 63 2016 2016 1000000000 1000000000 Sample Output 1 30576 752314689550

ECJTU ACM18级队员寒假训练赛 D题 CodeForces 1033A

#include<stdio.h>#include<queue>#include<string.h>#include<math.h>using namespace std; int n,qx,qy,kx,ky,wx,wy; int dx[8]={-1,-1,-1,0,0,1,1,1}; int dy[8]={1,0,-1,1,-1,1,0,-1}; // 方向数组 bool vis[1010][1010]; //标记数组 struct Node{ int x