码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2665 Trees

时间:2015-06-02 19:34:13      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10075   Accepted: 6622

Description

The road off the east gate of Peking University used to be decorated with a lot of trees. However, because of the construction of a subway, a lot of them are cut down or moved away. Now please help to count how many trees are left.

Let‘s only consider one side of the road. Assume that trees were planted every 1m (meter) from the beginning of the road. Now some sections of the road are assigned for subway station, crossover or other buildings, so trees in those sections will be moved away or cut down. Your job is to give the number of trees left.

For example, the road is 300m long and trees are planted every 1m from the beginning of the road (0m). That‘s to say that there used to be 301 trees on the road. Now the section from 100m to 200m is assigned for subway station, so 101 trees need to be moved away and only 200 trees are left.

Input

There are several test cases in the input. Each case starts with an integer L (1 <= L < 2000000000) representing the length of the road and M (1 <= M <= 5000) representing the number of sections that are assigned for other use.

The following M lines each describes a section. A line is in such format:

Start End

Here Start and End (0 <= Start <= End <= L) are both non-negative integers representing the start point and the end point of the section. It is confirmed that these sections do not overlap with each other.

A case with L = 0 and M = 0 ends the input.

Output

Output the number of trees left in one line for each test case.

Sample Input

300 1
100 200
500 2
100 200
201 300
0 0

Sample Output

200
300

类似于usaco Training -> Section 1.2 Milking Cows
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define REP(i, s, n) for(int i = s; i <= n; i ++)
#define REP_(i, s, n) for(int i = n; i >= s; i --)
#define MAX_N 5000 + 10

using namespace std;

int n, L;
int a[MAX_N], b[MAX_N];

int main(){
    while(scanf("%d%d", &L, &n) != EOF){
        if(L == 0 && n == 0) break;
        REP(i, 1, n) scanf("%d%d", &a[i], &b[i]);
        sort(a + 1, a + n + 1); sort(b + 1, b + n + 1);
        
        L ++;
        int node = 1, l = a[1], r = b[1], sum = 0;
        while(node < n){
            if(a[node + 1] <= b[node]) node ++, r = b[node];
            else{
                sum += r - l + 1; 
                node ++; l = a[node]; r = b[node];
            }
        }
        sum += r - l + 1;
        
        printf("%d\n", L - sum);
    }
    return 0;
}    

 



POJ 2665 Trees

标签:

原文地址:http://www.cnblogs.com/ALXPCUN/p/4546961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!