POJ 2665 Trees(我的水题之路——移树,POJ100题啦!)
2012-02-12 17:09
405 查看
Trees
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
Sample Output
Source
Beijing 2005 Preliminary
一个L(L<2 * 10^9)长度的公路,上面每隔一米有一棵树,即这个公路上有L+1棵树。之后有M个区间的树要移除,区间为[start,end],所以每次会移除end-start+1棵树。问最后还剩下多少棵树。
用一个unsigned int储存总数,然后每次对于区间减去需要移除的数量,最后求值。
注意点:
1)unsigned int为32位,最大值为4294967296,可以存储的下。
代码(1AC, POJ100题啦 \(^o^)/):
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8212 | Accepted: 5461 |
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 1 100 200 500 2 100 200 201 300 0 0
Sample Output
200 300
Source
Beijing 2005 Preliminary
一个L(L<2 * 10^9)长度的公路,上面每隔一米有一棵树,即这个公路上有L+1棵树。之后有M个区间的树要移除,区间为[start,end],所以每次会移除end-start+1棵树。问最后还剩下多少棵树。
用一个unsigned int储存总数,然后每次对于区间减去需要移除的数量,最后求值。
注意点:
1)unsigned int为32位,最大值为4294967296,可以存储的下。
代码(1AC, POJ100题啦 \(^o^)/):
#include <cstdio> #include <cstdlib> #include <cstring> 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; }
相关文章推荐
- POJ 2665 Trees(水题)
- POJ 2665 水题
- POJ 2665 Trees
- POJ 1298 The Hardest Problem Ever(我的水题之路——解密,水题)
- POJ 1517 u Calculate e(我的水题之路——特殊格式,打表)
- POJ 1543 Perfect Cubes(我的水题之路——四重暴力水)
- POJ 2070 Filling Out the Team(我的水题之路——double比较)
- POJ 2136 Vertical Histogram(我的水题之路——字符统计表)
- POJ 2390 Bank Interest(我的水题之路——double和floa计算差别)
- POJ 2538 WERTYU(我的水题之路——键盘错位)
- POJ 2636 Electrical Outlets(我的水题之路——水,电器接头)
- POJ 1316 Self Numbers(我的水题之路——筛法)
- POJ 1552 Double(我的水题之路——二重循环正逆比较)
- POJ 2551 Ones(我的水题之路——重点,末尾有几个1)
- POJ 2656 Unhappy Jinjin(我的水题之路——不开心的学习日)
- POJ 3030 Nasty Hacks(我的水题之路——比较大小)
- POJ 3158 Kickdown(我的水题之路——齿轮盒子,读题失败)
- POJ 2128 Highways(我的水题之路——最小两条公路之和)
- POJ 2459 Feed Accounting(我的水题之路——英文题啊!!!)
- POJ 3183 Stump Removal(我的水题之路——高峰烧火,在线判断)