博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2665 Trees(我的水题之路——移树,POJ100题啦!)
阅读量:4069 次
发布时间:2019-05-25

本文共 2276 字,大约阅读时间需要 7 分钟。

Trees
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8212   Accepted: 5461

Description

The road off the east gate of Peking University used to be decorated with a lot of trees. However, because of the construction of a subway, a lot of them are cut down or moved away. Now please help to count how many trees are left. 
Let's only consider one side of the road. Assume that trees were planted every 1m (meter) from the beginning of the road. Now some sections of the road are assigned for subway station, crossover or other buildings, so trees in those sections will be moved away or cut down. Your job is to give the number of trees left. 
For example, the road is 300m long and trees are planted every 1m from the beginning of the road (0m). That's to say that there used to be 301 trees on the road. Now the section from 100m to 200m is assigned for subway station, so 101 trees need to be moved away and only 200 trees are left.

Input

There are several test cases in the input. Each case starts with an integer L (1 <= L < 2000000000) representing the length of the road and M (1 <= M <= 5000) representing the number of sections that are assigned for other use. 
The following M lines each describes a section. A line is in such format: 
Start End 
Here Start and End (0 <= Start <= End <= L) are both non-negative integers representing the start point and the end point of the section. It is confirmed that these sections do not overlap with each other. 
A case with L = 0 and M = 0 ends the input.

Output

Output the number of trees left in one line for each test case.

Sample Input

300 1100 200500 2100 200201 3000 0

Sample Output

200300

Source

一个L(L<2 * 10^9)长度的公路,上面每隔一米有一棵树,即这个公路上有L+1棵树。之后有M个区间的树要移除,区间为[start,end],所以每次会移除end-start+1棵树。问最后还剩下多少棵树。
用一个unsigned int储存总数,然后每次对于区间减去需要移除的数量,最后求值。
注意点:
1)unsigned int为32位,最大值为4294967296,可以存储的下。
代码(1AC, POJ100题啦 \(^o^)/):
#include 
#include
#include
int main(void){ unsigned int trees; int i, j; int M; unsigned int start, end; while (scanf("%u%d", &trees, &M), trees != 0 || M != 0){ trees += 1; for (i = 0; i < M ; i++){ scanf("%u%u", &start, &end); trees -= end - start + 1; } printf("%u\n", trees); } return 0;}

转载地址:http://zloji.baihongyu.com/

你可能感兴趣的文章
SQL语句(六) 自主存取控制
查看>>
《计算机网络》第五章 运输层 ——TCP和UDP 可靠传输原理 TCP流量控制 拥塞控制 连接管理
查看>>
堆排序完整版,含注释
查看>>
二叉树深度优先遍历和广度优先遍历
查看>>
生产者消费者模型,循环队列实现
查看>>
PostgreSQL代码分析,查询优化部分,process_duplicate_ors
查看>>
PostgreSQL代码分析,查询优化部分,canonicalize_qual
查看>>
PostgreSQL代码分析,查询优化部分,pull_ands()和pull_ors()
查看>>
ORACLE权限管理调研笔记
查看>>
移进规约冲突一例
查看>>
IA32时钟周期的一些内容
查看>>
SM2椭圆曲线公钥密码算法
查看>>
获得github工程中的一个文件夹的方法
查看>>
《PostgreSQL技术内幕:查询优化深度探索》养成记
查看>>
PostgreSQL查询优化器详解之逻辑优化篇
查看>>
STM32中assert_param的使用
查看>>
C语言中的 (void*)0 与 (void)0
查看>>
vu 是什么
查看>>
io口的作用
查看>>
IO口的作用
查看>>