SPOJ QUEST5 Nail Them
来源:互联网 发布:php 获取上传文件类型 编辑:程序博客网 时间:2024/06/03 00:33
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 integern (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 thekth table.The output on the kth line must be an integerik, the minimum number of nails required.
Sample Input
Input:
2
3
1 5
3 5
2 4
2
1 4
4 5
Output:
1
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;}
- SPOJ QUEST5 Nail Them
- Nail Them+SPOJ+贪心算法
- SPOJ Make them equal !
- SPOJ - MKEQUAL E - Make them equal ! 水题
- [SPOJ INS14G Kill them All]数学、排列组合
- HOJ 2690 Nail II
- HOJ 2692 Nail IV
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- Swift(1):常量与变量
- 必须得对自己狠一些
- Oracle正则表达式用法
- Spring中的@Transactional配置
- org.hibernate.NonUniqueObjectException
- SPOJ QUEST5 Nail Them
- AngularJS移动开发中的坑汇总
- 群联PS2251-03东芝MLC量产成功教程(东芝16G)
- Java:int 转 long
- IOS UIImagePickerController
- Restricted Boltzmann Machine 数学推倒
- SPOJ 1026 Favorite Dice
- 【分治法】线性时间选择算法
- textView添加超链接(两种方式)