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

  • 方法

由于每个点都是在坐标点处并且在篱笆的拐弯处,所以任意横坐标或者纵坐标上的点都是偶数个的。

水平方向:
根据y值排序,当y相等时根据x排序。排序后遍历,当y相等时,两个x值相减即可得到长度
垂直方向:
根据x值排序,当x相等时根据y排序。排序后遍历,当x相等时,两个y值相减即可得到长度

将上述所有的长度相加即周长。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAXN = 100010;
struct Node
{int x,y;
}arr[MAXN];bool compareX(Node &a,Node &b)
{if(a.x!=b.x){return a.x<b.x;}return a.y < b.y;
}bool compareY(Node &a,Node &b)
{if(a.y!=b.y){return a.y<b.y;}return a.x < b.x;
}int main()
{int n;while(scanf("%d",&n) && n!=0){int num = 0;for(int i=0;i<n;i++){scanf("%d%d",&arr[i].x,&arr[i].y);}sort(arr,arr+n,compareX);for(int i=0;i<n;i+=2){num += arr[i+1].y - arr[i].y;}sort(arr,arr+n,compareY);for(int i=0;i<n;i+=2){num += arr[i+1].x - arr[i].x;}printf("The length of the fence will be %d units.\n",num);}return 0;
}

【POJ 1788 --- Building a New Depot】相关推荐

  1. 【POJ No. 1577 / UVA No. 1525】落叶 Falling Leaves

    [POJ No. 1577 / UVA No. 1525]落叶 Falling Leaves POJ题目地址 [题意] 一棵字母二叉树如下图所示. 一棵字母二叉树可以是两者之一: ①空树: ②有一个根 ...

  2. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  3. BZOJ 2287 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. &q ...

  4. 【POJ 3026】Borg Maze

    [POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值最小(最小生成树 BFS+最小生成 ...

  5. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

  6. 【POJ 2485】 Highways

    [POJ 2485] Highways 最小生成树模板 Prim #includeusing namespace std;int mp[501][501]; int dis[501]; bool vi ...

  7. 2287. 【POJ Challenge】消失之物(数组递推\分治优化背包)

    2287. [POJ Challenge]消失之物 这题的思想和P4564 [CTSC2018]假面优化的思想一样,应该反过来说,假面那个题应该是借鉴这题的思路. 显然不能枚举每个物品消失O(n)O( ...

  8. bzoj2287【POJ Challenge】消失之物 缺一01背包

    bzoj2287[POJ Challenge]消失之物 缺一01背包 链接 bzoj 思路 分治solve(l,r,arr)表示缺少物品\([l,r]\)的dp数组arr. 然后solve(l,mid ...

  9. 【POJ - 1364】King(差分约束判无解)

    题干: Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ` ...

最新文章

  1. ios开发返回按钮消失_iOS开发之自定义导航栏返回按钮右滑返回手势失效的解决...
  2. 不会连PPPoE协议都不会配吧?
  3. golang模板语法
  4. MISC-BUUCTF-9题-九连环-佛系青年等
  5. AT3950-[AGC022E]Median Replace【贪心,dp】
  6. sqlmap绕过d盾_Waf功能、分类与绕过
  7. mysql 学习笔记09字符串相关函数
  8. Ubuntu 中如何使用 PPA 安装应用程序
  9. SCP-bzoj-1000
  10. 自己电脑当linux服务器,在Linux下轻松搭建自己的DNS服务器
  11. Mocha BSM产品亮点——SNMP Trap的支持
  12. MySQL数据教程(一)数据库概念,超详细安装和配置数据库,数据库可视化界面介绍
  13. 软件项目管理案例教程笔记
  14. [新增:鸵鸟]软件开发团队的脓包:皇帝的新装、口号党、鸵鸟、废话迷
  15. jsp连接mysql 菜鸟_在JSP中访问数据库大全
  16. 面试:第十四章:大厂中高级java程序员面试题
  17. 生物信息学_序列比对(二)替换计分矩阵
  18. 斐波那契数列(Fibonacci) - 这就是算法吗?爱了爱了
  19. ***腾讯云直播(含微信小程序直播)研究资料汇总-原创
  20. (包成功)Windows 11安卓子系统安装apk运行安卓App

热门文章

  1. Android 时光轴 -记录生活
  2. es多个字段排序_如何解决 ES 复杂聚合排序问题(嵌套桶排序)?
  3. 计算机网络存在的漏洞,常见的计算机网络安全漏洞有哪些
  4. 云控系统机房搭建为什么用到网络交换机?
  5. html手机端页面meta,移动端网页meta设置和响应式
  6. python 文件备份
  7. 解一元二次方程lisp_lisp语言编写lisp程序解二元一次方程组和一元二次方程组应该怎么做​...
  8. 云计算实验2 Spark分布式内存计算框架配置及编程案例
  9. 基于Conv3D实现三维立体MNIST数据集分类
  10. USTC English Club Note20211222