AOJ 757.路边的树

路边的树
Time Limit: 1000 ms   Case Time Limit: 1000 ms   Memory Limit: 64 MB
Total Submission: 76   Submission Accepted: 10
 
Description
长度为L的路边有一排树,相邻树间隔为1米,树种在整数点,0,1,2,….,L.
现在马路上有一些区域修地铁,区域用起点和终止点表示。已知有M个区域要修地铁,区间之间可能重合的部分。现要把这些区域(包括端点处的两棵数)移走
,计算些树移走后,马路上还有多少棵树。
Input
第一行有两个整数L(1<=L<=10000)和M(1<=M<=100),L代表马路的长度, M代表区域数。L和M之间用一个空格分隔开。接下来的M行,每行包含两个不同的
整数,用一个空格隔开,表示一个区域的起始点和终止点的坐标。
Output
输出包括一行,这一行只包含一个整数,表示马路上剩余的树的数目。
Sample Input
Original Transformed
500 3
150 300
100 200
470 471
Sample Output
Original Transformed
298

用数组来模拟道路,先把所有位置种上树(TRUE),把需要砍掉的FALSE掉即可

AC代码:GitHub

 1 /*
 2 By:OhYee
 3 Github:OhYee
 4 HomePage:http://www.oyohyee.com
 5 Email:oyohyee@oyohyee.com
 6 Blog:http://www.cnblogs.com/ohyee/
 7 
 8 かしこいかわいい?
 9 エリーチカ!
10 要写出来Хорошо的代码哦~
11 */
12 
13 #include <cstdio>
14 #include <algorithm>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <iostream>
19 #include <vector>
20 #include <list>
21 #include <queue>
22 #include <stack>
23 #include <map>
24 using namespace std;
25 
26 //DEBUG MODE
27 #define debug 0
28 
29 //循环
30 #define REP(n) for(int o=0;o<n;o++)
31 
32 const int maxn = 10005;
33 bool tree[maxn];
34 
35 bool Do() {
36     int L,M;
37     if(scanf("%d%d",&L,&M)==EOF)
38         return false;
39 
40     memset(tree,true,sizeof(tree));
41 
42     REP(M) {
43         int a,b;
44         scanf("%d%d",&a,&b);
45         for(int i = a;i <= b;i++)
46             tree[i] = false;
47     }
48 
49     int cnt = 0;
50     for(int i = 0;i <= L;i++)
51         if(tree[i])
52             cnt++;
53     
54 
55     printf("%d
",cnt);
56     return true;
57 }
58 
59 int main() {
60     while(Do());
61     return 0;
62 }
原文地址:https://www.cnblogs.com/ohyee/p/5496551.html