POJ 1788 Building a New Depot 解题报告

Description

Advanced Cargo Movement, Ltd. is successfully expanding. In order to meet new demands on truck maintenance, the management of the company decided to build a new truck depot. A suitable lot for building a depot was purchased and a construction company ``Masonry and Fences for Future, Ltd.‘‘ was hired to build a depot.

The area of the depot will be enclosed by a fence. The fence is supposed to enclose a connected area of a lot and each part of the fence follows North-South or East-West direction. At each place where the fence changes its direction, there is a single post. The posts are only at points where the fence changes the direction, i.e., there are no unnecessary posts. When MFF workers have built all of the posts, they lost the plan of a depot being built. At this moment, they asked you for a help.

Given the coordinates of all the posts, your task is to compute the length of the fence.

Input

The input consists of several blocks. The first line of each block contains a single number P, 1 <= P <= 100 000. P is the number of posts which have been built. Each of the following P lines contains two integers X and Y, 0 <= X, Y <= 10 000, which represent coordinates of the posts in MFF internal units (which no one else is able to understand). No two posts have the same coordinates.

Each block is followed by a single empty line and the input is terminated by a line containing a single number 0.

Output

Output contains a single line for each block. The line should contain the text "The length of the fence will be L units.", where L is replaced by an actual length of the fence. You can assume that the fence can always be built.

Sample Input

6
1 1
1 3
3 3
2 1
3 2
2 2

0

Sample Output

The length of the fence will be 8 units.

题目大意:就是数字是柱子的坐标,没两根柱子之间有栅栏  求栅栏的长度。。。解题思路:两次排序  第一次按X排,第二次按y排,,,,然后分别把差值加起来

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<math.h>
 4 using namespace std;
 5 struct weizhi{
 6     int x;
 7     int y;
 8 }a[200000];
 9 bool cmp1(weizhi a,weizhi b)
10 {
11     if(a.x==b.x)
12         return a.y<b.y;
13     return a.x<b.x;
14 }
15 bool cmp2(weizhi a,weizhi b)
16 {
17     if(a.y==b.y)
18         return a.x<b.x;
19     return a.y<b.y;
20 }
21 int main()
22 {
23     long int n;
24     while(scanf("%d",&n)!=EOF)
25     {
26         if(n==0)break;
27         int i;
28         for(i=0;i<n;i++)
29         {
30             scanf("%d %d",&a[i].x,&a[i].y);
31         }
32         int sum=0;
33         sort(a,a+n,cmp1);
34         for(i=0;i<n;i+=2)
35         {
36             sum+=a[i+1].y-a[i].y;
37         }
38         sort(a,a+n,cmp2);
39         for(i=0;i<n;i+=2)
40         {
41             sum+=a[i+1].x-a[i].x;
42         }
43         printf("The length of the fence will be %d units.\n",sum);
44     }
45     return 0;
46 }

时间: 2024-10-17 20:20:51

POJ 1788 Building a New Depot 解题报告的相关文章

最小生成树,POJ和HDU几道题目的解题报告(基于自己写的模板)

首先POJ题目: 链接:1251 Jungle Roads 题目大意:纯求最小生成树,结果为最小权值边的和.采用邻接表 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <queue> 5 using namespace std; 6 7 #define maxn 30 //最大顶点个数 8 int n; //顶点数,边数 9 10 struct arcn

poj 1094 Sorting It All Out 解题报告

题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具体的字母顺序不能确定但至少不矛盾.这些关系均是这样的一种形式: 字母1 < 字母2 这道题目属于图论上的拓扑排序,由于要知道读入第几行可以确定具体的顺序,所以每次读入都需要进行拓扑排序来检验,这时每个点的入度就需要存储起来,所以就有了代码中memcpy 的使用了. 拓扑排序的思路很容易理解,但写起来

POJ 3126(BFS_E题)解题报告

题目链接:http://poj.org/problem?id=3126 -------------------------------------------------------- 题意:给定两个数,将初始数,每次变换一位而且必须保证变换后的数为素数,要求从初始数到目标数所需要的最小操作次数. 思路:首先是多组测试数据,所以先打个10000以内的素数表,防止每次都去判断.然后就是bfs的想法,每次分别改变个位.十位.百位.千位的值,然后按层次进行遍历,首先到达的情况是最小值的情况. 代码:

POJ 2774 字符串哈希_解题报告

题目:  http://poj.org/problem?id=2774 A - Long Long Message The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway ticke

poj 1087.A Plug for UNIX 解题报告

网络流,关键在建图 建图思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化). 2.层次出现断层,无法得到新流(重要). 时间复杂度(m*n^2) */ #include <iostream> #include <cstdio> #include <cstring> #include <map> #define ms(a,b) mem

POJ 2258(DFS_D题)解题报告

题目链接:https://vjudge.net/problem/22345/origin -------------------------------------------------------------------- 题意:求图中所有路径中最长的路径. 思路:DFS,注意:其中结点可以重复遍历,而边不可以重复遍历.所以应当在深度遍历上做一定修改,将对结点的遍历改为对边的遍历. 代码: #include<cstdio> #include<cstring> #include&

POJ 1958 Strange Towers of Hanoi 解题报告

Strange Towers of Hanoi 大体意思是要求\(n\)盘4的的hanoi tower问题. 总所周知,\(n\)盘3塔有递推公式\(d[i]=dp[i-1]*2+1\) 令\(f[i]\)为4塔转移步骤. \(f[i]=min(f[i],f[k]*2+d[i-k])\) 即先以4塔以上面的\(k\),再以3塔移\(i-k\),最后以4塔移动回去. 可以推广到\(n\)盘\(m\)塔 2018.5.26 原文地址:https://www.cnblogs.com/ppprseter

poj 3020 Antenna Placement 解题报告

题目链接:http://poj.org/problem?id=3020 题目意思:首先,请忽略那幅有可能误导他人成分的截图(可能我悟性差,反正有一点点误导我了). 给出一幅 h * w 的图,  “ * ” 表示 point of interest,“ o ” 忽略之.你可以对 " * " (假设这个 “* ”的坐标是 (i, j))画圈,每个圈只能把它四周的某一个点括住(或者是上面(i-1, j) or 下面(i+1, j) or 左边(i, j-1)  or 右边(i, j+1))

poj 1469 COURSES 解题报告

题目链接:http://poj.org/problem?id=1469 题目意思:略 for 循环中遍历的对象要特别注意,究竟是遍历课程数P 还是 学生数N,不要搞混! 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 300 + 5; 7 int match[maxn], map[maxn][maxn];