校门外的树
校门外的树
https://ac.nowcoder.com/acm/problem/16649
思路:因为给的区域有重合部分所以可以将注意从移走多少树变为剩多少树。
步骤:创建一个给定长度的数组并赋初值为0,给一个区域就用for循环将其减一,再用for循环遍历一遍为0的就是有树的。
#include "stdio.h" #include "iostream" using namespace std; int main() { int L,M,start,end,total=0; cin>>L>>M; int Tree[L+1]; int i; for(i=0;i<=L;i++){ Tree[i]=0; } for(i=0;i<M;i++){ cin>>start>>end; for(int j=start;j<=end;j++){ Tree[j]--; } } for(i=0;i<=L;i++){ if(Tree[i]==0)total++; } cout<<total; }