C语言排序(7)___修栅栏

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.

题目大意:

每组数据第一排给出一个整数n;

接下来的n排表示n个木桩的坐标.在木桩之间修栅栏,栅栏只剩东西南北方向,栅栏为单位长度.

问要几个栅栏才能连接完所有木桩形成一个环.


样例1:

#include <stdio.h>
int a[2][200000];            //定义一个二维数组,分别对应放坐标的x,y
int com(int m,int n,int p,int k)         //定义比较函数,用于后面快排函数做参数,p,k表示当前在排x还是y
{
	int book=0;               
	if((a[p][m]>a[p][n])||(a[p][m]==a[p][n]&&a[k][m]>a[k][n])) .
		book=1;
	return book;
}
void quicksort(int left,int right,int p)  //定义快排对坐标排序,p=0表示当前对x排序,p=1表示当前对y排序
{
	int i,j,m,k;
	if(p==1)
		k=0;
	else
		k=1;
	i=left,j=right;
	if(i>j)
		return;
	while(i!=j)
	{
		while(i<j&&com(j,left,p,k))
			j--;
		while(i<j&&com(left,i,p,k))
			i++;
		if(i<j)
		{
			m=a[p][j],a[p][j]=a[p][i],a[p][i]=m;
			m=a[k][j],a[k][j]=a[k][i],a[k][i]=m;
		}
	}
	m=a[p][i],a[p][i]=a[p][left],a[p][left]=m;
    m=a[k][i],a[k][i]=a[k][left],a[k][left]=m;
	quicksort(left,i-1,p);
	quicksort(i+1,right,p);
}

int main()
{
	int n,i,sum;
	while(scanf("%d",&n)&&n)
	{
		sum=0;
		for(i=0;i<n;i++)
			scanf("%d%d",&a[0][i],&a[1][i]);//存储坐标
		quicksort(0,n-1,0);       //对x排序
		for(i=0;i<n;i+=2)         
			sum+=a[1][i+1]-a[1][i];    <span style="font-family: Arial, Helvetica, sans-serif;">//累加计算y差值</span>
		quicksort(0,n-1,1);      //对y排序
		for(i=0;i<n;i+=2)
			sum+=a[0][i+1]-a[0][i];   //累加计算x差值
		printf("The length of the fence will be %d units.\n",sum);
	} 

	return 0;
}


样例二:

#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
struct weizhi             //定义存储坐标的结构体
{
	int x;
	int y;
}a[200000];
bool cmp1(weizhi a,weizhi b)   //定义对x排序sort函数所需参数的比较函数.
{
	if(a.x==b.x)
		return a.y<b.y;  //当x相同的时候对y升序
	return a.x<b.x;         //对x升序
}
bool cmp2(weizhi a,weizhi b)   //定义对y排序sort函数所需参数的比较函数
{
	if(a.y==b.y)
		return a.x<b.x;   //当y相同的时候对x升序
	return a.y<b.y;       //对y升序
}
int main()
{
	long int n;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)break;
		int i;
		for(i=0;i<n;i++)
		    scanf("%d %d",&a[i].x,&a[i].y);
		int sum=0;
		sort(a,a+n,cmp1);
		for(i=0;i<n;i+=2)
		{
			sum+=a[i+1].y-a[i].y;
		}
		sort(a,a+n,cmp2);
		for(i=0;i<n;i+=2)
		{
			sum+=a[i+1].x-a[i].x;
		}
		printf("The length of the fence will be %d units.\n",sum);
	}
	return 0;
}


本题坑点:

对x排序的时候,当x相同时,y必须升序,同理对y排序的时候,当y相同时,x必须升序.

原因如下:

        ——
——|      |——
|                       |

——————         这种情况在第二排会出现四个点y值相同,那么不对x排序的话无法计算两边的栅栏长度!

你可能感兴趣的:(C语言)