C语言--Building a New Depot
来源:互联网 发布:java redis 并发控制 编辑:程序博客网 时间:2024/06/05 11:37
题目链接:点击打开链接
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.
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.
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
61 11 33 32 13 22 20
Sample Output
The length of the fence will be 8 units.
题目大意:说直白点就是给出一系列的坐标,然后计算就水平竖直的栅栏连接,问需要栅栏多少米?
题目分析:很明显这又涉及到结构体的定义与排序,直接一个结构体搞定。
重点及注意事项:1.对结构体的定义排序
2.在计算时如何防止如1,3;2,4;2,5;之类的坐标,重复相加了,怎么办?
3.排序方法的使用(防止超时);
不多说AC代码----方法仅供产考#include <stdio.h>#include <algorithm>using namespace std;struct point{int x,y;}p[1000002];bool cmp1(point a,point b){if(a.y == b.y)return a.x < b.x;return a.y < b.y;//以y坐标为基准升序排列坐标}bool cmp2(point a,point b){if(a.x==b.x) return a.y<b.y;return a.x<b.x;//以x坐标为基准升序排列坐标}int main(){int n;while(scanf("%d",&n)!=EOF&&n!=0){for(int i=0;i<n;i++)scanf("%d%d",&p[i].x,&p[i].y);int s=0;sort(p,p+n,cmp1);for(int j=0;j<n;j+=2)s+=(p[j+1].x-p[j].x);//数据每两个为一组,避免了重复处理数据,导致答案错误sort(p,p+n,cmp2);for(int k=0;k<n;k+=2)s+=(p[k+1].y-p[k].y);//x,y的长度分开计算,与上面的不同基准排序相照应printf("The length of the fence will be %d units.\n",s); }return 0;}
附上运行截图
心得:对数据正确有效的处理能起到事半功倍的效果
0 0
- C语言--Building a New Depot
- POJ Building a New Depot
- poj 1788 Building a New Depot
- POJ 1788 Building a New Depot 笔记
- POJ 1788 Building a New Depot
- ZOJ 2157 Building a New Depot(POJ 1788)
- zoj 2157 || poj 1788 Building a New Depot
- POJ 1788 Building a New Depot(计算正多边形边长)
- ZOJ 2157|| Building a New Depot(纵向排序 横向排序
- POJ 1788 Building a New Depot(水~)
- Building a New Framework
- Building Android for a new Mobile Device
- poj 3269 Building A New Barn
- 1696: [Usaco2007 Feb]Building A New Barn新牛舍
- BZOJ 1696: [Usaco2007 Feb]Building A New Barn新牛舍
- bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍
- Intel's Haswell Architecture Analyzed: Building a New PC and a New Intel
- 【C++】new A和new A()的区别详解
- HTML查看器
- POJ 3422 Kaka's Matrix Travels(费用流)
- omap 系列寄存器的PULL_UP,PULL_DOWN
- XScrollBar - Scroll bar like Windows Media Player's
- Spring 学习之 IOC
- C语言--Building a New Depot
- 顺序表
- CentOS和Ubuntu设置静态IP
- stucts2访问web元素(jsp和servlet的九大隐式对象)
- hdu1542(扫描线求面积并)
- dpkg: error processing package git-daemon-sysvinit (--remove):
- lightoj-1006
- glPushMatrix()和glPopmatirx()
- 第一篇