http://ac.jobdu.com/problem.php?cid=1040&pid=12
- 题目描述:
-
有一个长度为整数L(1<=L<=10000)的马路,可以想象成数轴上长度为L的一个线段,起点是坐标原点,在每个整数坐标点有一棵树,即在0,1,2,...,L共L+1个位置上有L+1棵树。
现在要移走一些树,移走的树的区间用一对数字表示,如 100 200表示移走从100到200之间(包括端点)所有的树。
可能有M(1<=M<=100)个区间,区间之间可能有重叠。现在要求移走所有区间的树之后剩下的树的个数。
- 输入:
-
两个整数L(1<=L<=10000)和M(1<=M<=100)。
接下来有M组整数,每组有一对数字。
- 输出:
-
可能有多组输入数据,对于每组输入数据,输出一个数,表示移走所有区间的树之后剩下的树的个数。
- 样例输入:
-
500 3 100 200 150 300 470 471
- 样例输出:
-
298
// 题目13:剩下的树.cpp: 主项目文件。 #include "stdafx.h" #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <string> #include <set> #include <map> #include <queue> #include <stack> using namespace std; const int N=10003; int hashTable[N]; int main() { //freopen("F:\\test.txt","r",stdin); //freopen("F:\\output.txt","w",stdout); int n,m; while(cin>>n>>m) { memset(hashTable,0,sizeof(hashTable)); for(int i=0;i<m;i++) { int tmp1,tmp2; cin>>tmp1>>tmp2; for(int j=tmp1;j<=tmp2;j++) hashTable[j]=true; } int cnt=0; for(int i=0;i<=n;i++) if(!hashTable[i]) cnt++; cout<<cnt<<endl; } return 0; }