Tree out of school, out of school

Source: Internet
Author: User

Tree out of school, out of school

Problem description

There is a row of trees on the road with a length of L outside the gate of a school. The interval between every two adjacent trees is 1 meter. We can regard the road as a number axis, where one end of the road is 0 and the other is L. Each integer point on the number axis is 0, 1, 2 ,......, l. There is a tree. There are some areas on the road to build a subway. These regions are expressed by their starting and ending points on the number axis. It is known that the coordinates of the starting and ending points of any region are integers, and the areas may overlap. Now we need to remove the trees (including the two trees at the region endpoint) in these regions. Your task is to calculate the number of trees on the road after all these trees are removed.

Input data

The first line of the input has two integers, L (1 <= L <= 10000) and M (1 <= M <= 100). L represents the length of the road, M indicates the number of regions. L and M are separated by a space. Each row of the next M line contains two different integers separated by a space, representing the coordinates of the start and end points of a region.

The output must contain one row. This row only contains one integer, indicating the number of remaining trees on the road. Input example

500 3

150 300

100 200

470 471

Sample output 298

 

Solution Analysis:

First, the first reaction is to find the set, create a two-dimensional array, and then get stuck in the merged set.

However, if you look at the constrains of the question, you will find that the maximum value of L is not large. Therefore, a simple method is derived. The initial value is 1. The range of each section is set to 0. If multiple sections are set to 0, this will not be affected. Therefore, the c ++ solution for the final question is as follows:

#include <iostream>#include <vector>#include <numeric>using namespace std;int compare(vector<int>a,vector<int>b){    return a[0]<b[0];}int main() {    int l,m;    cin>>l>>m;    int *trees=new int[l+1];    //fill the array with 1    fill_n(trees,l+1,1);    int start,end;    for(int i=0;i<m;i++)    {        cin>>start>>end;        for(int j=start;j<=end;j++)        {            trees[j]=0;        }    }    //sum of trees    cout<<accumulate(trees,trees+l+1,0);    delete []trees;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.