bzoj2146 Construct

2146: Construct

Time Limit: 10 Sec  Memory Limit: 259 MB

Submit: 126  Solved: 52

[Submit][Status][Discuss]

Description

随着改革开放的深入推进…… 小T家要拆迁了…… 当对未来生活充满美好憧憬的小T看到拆迁协议书的时候,小T从一位大好的社会主义青年变成了绝望的钉子户。 由于小T的家位于市中心,拆迁工作又难以进行,有关部门决定先把小T家用围栏围起来,以免影响市容。考虑到要建设资源节约型社会,他们希望所用的围栏长度越短越好,由于市中心寸土寸金,在围栏长度最短的情况下,围出的多边形面积越小越好。 为了简化问题,我们约定,小T的家是一个多边形,并且每条边与坐标轴平行,围栏构成的也是多边形,每条边与坐标轴平行。

Input

在第一行列出整数n——多边形的顶点的数量。在以下n行中的每一行都有两个整数——沿逆时针方向走过这个多边形顺次所经过的顶点的坐标。边界的任意三个连续顶点不在一条直线上。多边形的边界不含自交和自切。

Output

输出两行,第一行为围栏最短的长度,第二行为长度最短的前提下,最小的面积。

Sample Input

8

0 0

9 0

9 9

6 9

6 3

3 3

3 6

0 6

Sample Output

36

63

【数据范围】

对于100%的数据4≤n≤100000,坐标的绝对值不超过109 。

凸包思路好题

搬运PoPoQQQ大爷的题解:

http://blog.csdn.net/popoqqq/article/details/43920135

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define maxn 100005
#define inf 1000000005
using namespace std;
int n,top;
ll max_x,min_x,max_y,min_y,ans;
struct P{ll x,y;}p[maxn],s[maxn];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
inline bool cmp(P a,P b)
{
	return a.x==b.x?a.y<b.y:a.x<b.x;
}
inline void solve()
{
	int i;
	for(i=1;i<=n;i++)
	{
		if (top&&p[i].x==s[top].x) top--;
		if (!top||p[i].y>=s[top].y) s[++top]=p[i];
		if (p[i].y==max_y) break;
	}
	for(i++;i<=n;i++)
	{
		while (p[i].y>s[top].y) top--;
		s[++top]=p[i];
	}
	F(i,1,top-1) ans+=min(s[i].y,s[i+1].y)*(s[i+1].x-s[i].x);
	top=0;
	for(i=1;i<=n;i++)
	{
		if (!top||p[i].y<=s[top].y) s[++top]=p[i];
		if (p[i].y==min_y) break;
	}
	for(i++;i<=n;i++)
	{
		while (p[i].y<s[top].y) top--;
		s[++top]=p[i];
	}
	F(i,1,top-1) ans-=max(s[i].y,s[i+1].y)*(s[i+1].x-s[i].x);
}
int main()
{
	n=read();
	max_x=max_y=-inf;min_x=min_y=inf;
	F(i,1,n)
	{
		p[i].x=read();p[i].y=read();
		max_x=max(max_x,p[i].x);min_x=min(min_x,p[i].x);
		max_y=max(max_y,p[i].y);min_y=min(min_y,p[i].y);
	}
	sort(p+1,p+n+1,cmp);
	solve();
	printf("%lld\n%lld\n",(max_x-min_x+max_y-min_y)*2,ans);
	return 0;
}
时间: 2024-10-06 13:02:50

bzoj2146 Construct的相关文章

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

492. 构造矩形 Construct the Rectangle

For a web developer, it is very important to know how to design a web page's size. So, given a specific rectangular web page's area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements: 1

105. Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. ============== 基本功: 利用前序和中序构建二叉树 , === code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : va

106. Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. ======== 利用:中序+后序遍历 ==== code: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod

【leetocde】 105. Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. Subscribe to see which companies asked this question 递归就可以了. #include<algorithm> using namespace std; /** * Defi

C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 这个题目是给你一棵树的中序遍历和后序遍历,让你将这棵树表示出来.其中可以假设在树中没有重复的元素. 当做完这个题之后,建议去做做第105题,跟这道题类似. 分析:这个解法的基本思想是:我们有两个数组,分别是IN和POST.后

函数构造前面的关键字的区别是什么?public construct private static

1. public  construct  private  static  public function index(){ ... }

leetcode之Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 给出树的前序和中序遍历序列,构造二叉树,假设树唯一存在. 剑指offer面试题6,重建二叉树

LeetCode 106:Construct Binary Tree from Postorder and Inorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. 给定一个二叉树的后序和中序遍历,重建这棵二叉树. 此题和LeetCode105 根据前序和中序重建二叉树类似. 所谓后序遍历,即先访问根的左.右子树,然后再访问根节点.这样根节点在二叉树后序遍历的最后一个个元素. 所谓中序遍历,即使先访问左子树,然后访问根节点,,再访问又子树,这样根节点位于中序遍历中间位置,左边为左子树的节点,右边为又子树的节点