Spoors
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.
Input
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.
Output
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)