Title Description
A school outside the gate length L of the road with a row of trees, the interval between each two adjacent trees are 1 m. We can put the road number as a shaft, one end of the road at a position number 0 of the shaft, the other end position L; each integer points on the number line , i.e., 0,1,2, ..., L, are a tree species .
Because there are some areas on the road to be used to build the subway. These areas represent the start and end points with number axis thereof. Coordinate any known starting and ending points of a region are integers, there may be overlap between the partial regions. Now, we these regions tree (including two trees at the end regions) removed. Your task is the calculation of these trees are removed, how many trees there are on the road.
Input Format
The first line has two integers L (1≤L≤10000) and M (1≤M≤100) , representative length L of the road, the number M represents the region between L and M are separated by a space.
Next M lines contains two different integers, separated by a space, coordinates indicating the starting and ending points of a region.
Output Format
An integer indicating the number of remaining tree on the road.
Sample input and output
Input # 1
500 3 150 300 100 200 470 471
Output # 1
298
Description / Tips
NOIP2005 universal set of second title
For 20 \% 2 0 % of the data, there is no overlap between the partial region;
For other data, where there is overlap between the regions.
Thinking:
In fact, this is an entry-level questions, but at the time of submission of test points is always some trouble, reason is not careful enough, big enough to open the array. I used to define the size of the array to a start taking into account the M groups of data, and the subject is given range . 1 ≤ M ≤ . 1 0 0, it will be open to the array 100. But in the first test run, I found it impossible to output. Later I discovered that I used on behalf of an array of trees too small. Because the subject is given . 1 ≤ L ≤ . 1 0 0 0 0, so I'll change the size of the array, open to 10,000. Would have thought it, the results of the test is running, although you can give the answer, the test data can also be passed, but still can not be submitted when AC. Carefully to see it again the subject, found that although the topic to the L range is 1 ≤ L ≤ 1 0 0 0 0, however, the road endpoints are planting trees, which means that in fact if L represents the number of trees to use , then L should be a range of 1 ≤ L ≤ 1 0 0 01 fishes. After modification, the final result would be the AC.
Complete code:
#include<stdio.h> #define N 10001 int main() { int L, m, I, J, SUM = 0 , A [N], B [N], C [N] = { 0 }; // C [N] array is used to represent the trees, the initial values are zero, It has not yet been removed Scanf ( " % D% D " , & L, & m); // enter the road length and number of regions for (I = 0 ; I <m; I ++ ) Scanf ( " % D% D " , & A [I], B & [I]); // input a starting point of each data for (I = 0 ; I <m; I ++ ) { if(a[i]>b[i]) { for(j=b[i];j<=a[i];j++) c [j] = 1 ; } if(a[i]<=b[i]) { for(j=a[i];j<=b[i];j++) c [j] = 1 ; } } // due to uncertainty whether strict accordance with the format of the input start and end points, it must first determine the size. // trees marker is within the region. 1 for (I = 0 ; I <= L; I ++ ) IF (C [I] == 0 ) SUM = SUM + . 1 ; // Analyzing, trees are not labeled c [i] = 0, the total number of + 1'd the printf ( " % D " , SUM); return 0 ; }
Topic links: https://www.luogu.com.cn/problem/P1047