The remaining number - cattle-off

 Has a length of integer L (1 <= L <= 10000) of the road, one can imagine the number of longitudinal axis L of a line segment, the starting point is the coordinate origin, a tree at every integer coordinate points, i.e. 0,1 , 2, ..., L total L + L + 1 has a position on the tree. Now to remove some trees, tree removal section is represented by a pair of numbers, such as 100 200 represents all removed from the tree (inclusive) 100-200. There may be M (1 <= M <= 100) intervals, there may be overlap between the intervals. The number of trees remaining after removal of the requirements of all sections of the tree now.

Enter a description:

    Two integers L (1 <= L <= 10000) and M (1 <= M <= 100). 
    Then there is an integer M groups, each with a pair of numbers.

Output Description:

    May be multiple sets of input data, each input data, outputs a number representing the number of all sections after removal of the remaining trees in the tree.
Example 1

Entry

500 3
100 200
150 300
470 471

Export

298

Problem-solving ideas

For such problems, the idea is too important, I hope later this encounter similar topics can think of this clever method:

1, create an array, all set to 1, then deleting all the numbers of all the subscript set to 0

2, and finally through the array, the array is the number of count of 0, the output can.

PS: There is a point to note about the first array and in the definition given initial time, int a [L] = {0} Some compilers is not legal

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5         int L,M,count=0;
 6         cin >> L >> M;
 7         int a[10001]={0};
 8         while(M--)
 9         {
10             int l,r;
11             cin >> l >> r;
12             for(int i = l-1;i<r;i++)
13             {
14                 a[i] = 1;
15             }
16         }
17         for(int i=0;i<L;i++)
18         {
19             if(a[i]==0) count++;
20         }
21         cout << count+1 <<endl;
22         return 0;
23 }

 

Guess you like

Origin www.cnblogs.com/jiashun/p/newcode18.html