The third Shenyang University of Aeronautics and Astronautics (Dalian Maritime University Competition)---i:spoors (pretreatment)

Source: Internet
Author: User

Time Limit: 1 Sec Memory Limit: MB Description

Some of know when a snail crawls; He'll leave a spoor after him tour. Then all the you'll know that when many snail crawls, they'll leave many spoors. In this problem, you'll be given n snails ' spoors and then calculate the sum of length of their spoors. To make it harder, we assume this all the snail crawl on the same line. That was to say, when the snails ' spoor overlaps, the length of the overlap part was considered only once.


There is many test cases.

The first line was a single integer n (0<n<=1000), indicating n snails.

Following n lines, in each line there is both integer A and B (0<a, b<=1000), indicating a snail crawls from point ( 0,a) to point (0,b). I warn the length of (0,a) to (0,B) is |b-a+1|.

Proceed to End of File.


For each case, output one line with a single integer, indicating the sum length of the spoors.

Sample Input426 551 5Sample Output359

Test instructions: There is a snail, on a road to climb many times, every time there is a starting point A and the end of B, each crawl will leave a mark on the road, ask how long this road total is marked (repeat only one time).

Analysis: Pre-treatment, each time there is a starting point and end point, each time the beginning and end of the road between the mark, to the end, see how long is marked.

AC Code:

#include <cstdio> #include <iostream> #include <algorithm> #include <cstring>using namespace Std;int A[1005];int Main () {//freopen ("In.txt", "R", stdin); int n, x, Y;while (scanf ("%d", &n)!=eof) {memset (A, 0, sizeof (a)); int fo = 0;for (int i=0; i<n; i++) {scanf ("%d%d", &x, &y), if (x > Y) Swap (x, y);        X may be greater than Y if (y > fo) fo = y;for (int i=x; i<=y; i++)      a[i] = 1;             Mark}int ans = 0;for (int i=0; i<=fo; i++) {             //Check How many are labeled if (A[i]) ans + +;} printf ("%d\n", ans);}}

This method is still very common, I used to be from the tourist code in the first to see, very magical method ^_^

The third Shenyang University of Aeronautics and Astronautics (Dalian Maritime University Competition)---i:spoors (pretreatment)

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: 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.