Uva5211/POJ1873 The Fortified Forest 凸包

LINK

题意:给出点集,每个点有个价值v和长度l,问把其中几个点取掉,用这几个点的长度能把剩下的点围住,要求剩下的点价值和最大,拿掉的点最少且剩余长度最长。

思路:1999WF中的水题。考虑到其点的数量最多只有15个,那么可以使用暴力枚举所有取点情况,二进制压缩状态,预处理出该状态下的价值,同时记录该状态拥有的点,并按价值排序。按价值枚举状态,并对拥有的这些点求凸包,check是否合法,找到一组跳出即可。然而POJ似乎没有SPJ,同样的代码POJ会超时,UVA60ms,可以在常数上优化,不预处理,实时判价值最大值,不使用结构体等

/** @Date    : 2017-07-16 14:48:08
  * @FileName: POJ 1873 凸包.cpp
  * @Platform: Windows
  * @Author  : Lweleth ([email protected])
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#include <math.h>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

struct point
{
	double x, y;
	point(){}
	point(double _x, double _y){x = _x, y = _y;}
	point operator -(const point &b) const
	{
		return point(x - b.x, y - b.y);
	}
	double operator *(const point &b) const
	{
		return x * b.x + y * b.y;
	}
	double operator ^(const point &b) const
	{
		return x * b.y - y * b.x;
	}
};

double xmult(point p1, point p2, point p0)
{
    return (p1 - p0) ^ (p2 - p0);
}  

double distc(point a, point b)
{
	return sqrt((double)((b - a) * (b - a)));
}

int sign(double x)
{
	if(fabs(x) < eps)
		return 0;
	if(x < 0)
		return -1;
	else
		return 1;
}
////////////

point pt[50];
int v[50], l[50];
struct yuu
{
	point p[50];
	int val;
	int cnt;
	int len;
	int sta;
	double cst;
	void init(){cnt = len = val = cst = 0;}
}tt[1 << 16];

point stk[50];

/*int cmpA(point a, point b)//以p[0]基准 极角序排序
{
	int t = xmult(a, b, p[0]);
	if(t > 0)
		return 1;
	if(t == 0)
		return distc(a, p[0]) < distc(b, p[0]);
	if(t < 0)
		return 0;
}*/

int cmpB(yuu a, yuu b)//预处理用
{
	if(a.val == b.val)
		return a.cnt < b.cnt;
	return a.val < b.val;
}

int cmpC(point a, point b)//水平序排序
{
	return sign(a.x - b.x) < 0 || (sign(a.x - b.x) == 0 && sign(a.y - b.y) < 0);
}

/*void grahamS()
{
	while(!s.empty())
		s.pop();
	for(int i = 0; i < min(n, 2); i++)
		s.push(i);
	int t = 1;
	for(int i = 2; i < n; i++)
	{
		while(s.size() > 1)
		{
			int p2 = s.top();
			s.pop();
			int p1 = s.top();
			if(xmult(p[p1], p[p2], p[i]) > 0)
			{
				s.push(p2);
				break;
			}
		}
		s.push(i);
	}
}*/

int graham(int st)//水平序
{
	sort(tt[st].p, tt[st].p + tt[st].cnt, cmpC);
	int top = 0;
	for(int i = 0; i < tt[st].cnt; i++)
	{
		while(top >= 2 && sign(xmult(stk[top - 2], stk[top - 1], tt[st].p[i])) <= 0)
			top--;
		stk[top++] = tt[st].p[i];
	}
	int tmp = top;
	for(int i = tt[st].cnt - 2; i >= 0; i--)
	{
		while(top > tmp && sign(xmult(stk[top - 2],stk[top - 1] ,tt[st].p[i] )) <= 0)
			top--;
		stk[top++] = tt[st].p[i];
	}
	if(tt[st].cnt > 1)
		top--;
	return top;
}

int check(int st)
{
	int ma = graham(st);
	double ans = 0;
	stk[ma] = stk[0];
	for(int i = 0; i < ma; i++)
		ans += distc(stk[i + 1], stk[i]);
	tt[st].cst = ans;
	if(sign(tt[st].len - ans) < 0)
		return 0;
	return 1;
}

int main()
{
	int n;
	int cc = 0;
	while(~scanf("%d", &n) && n)
	{
		if(cc)
			printf("\n");
		cc++;
		double x, y;
		for(int i = 0; i < n; i++)
		{
			scanf("%lf%lf%d%d", &x, &y, v + i, l + i);
			pt[i] = point(x, y);
		}
		int c = 0;
		for(int st = 0; st < (1 << n); st++)//预处理
		{
			tt[st].sta = st;
			tt[st].init();
			c = 0;
			for(int i = 0; i < n; i++)
			{
				if(st & (1 << i))
					tt[st].len += l[i], tt[st].val += v[i];
				else
					tt[st].p[c++] = pt[i];

			}
			tt[st].cnt = c;
		}
		sort(tt, tt + (1 << n), cmpB);

		//////
		for(int st = 0; st < (1 << n); st++)//遍历
		{
			if(check(st))
			{
				printf("Forest %d\n", cc);
				printf("Cut these trees:");
				for(int i = 0; i < n; i++)
				{
					if(tt[st].sta & (1 << i))
						printf(" %d", i + 1);
				}
				printf("\nExtra wood: %.2lf\n", ((double)tt[st].len - tt[st].cst + eps));
				break;
			}
		}
	}
    return 0;
}
时间: 2024-10-14 08:55:58

Uva5211/POJ1873 The Fortified Forest 凸包的相关文章

poj 1873 The Fortified Forest(凸包)

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5737   Accepted: 1636 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been

POJ 1873 - The Fortified Forest 凸包 + 搜索 模板

通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 >= 2,即 n >= 3 时凸包才有意义 顶点数为 1 时,tmp = - 1 要做特殊判断. 总结了一下凸包模板 //template Convex Hull friend bool operator < (const point &p1, const point &p2){

UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)

题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees from thieves, the king or

Poj 1873 The Fortified Forest

地址:http://poj.org/problem?id=1873 题目: The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6421   Accepted: 1811 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of r

poj1873The Fortified Forest

链接 居然是WF的水题~ 二进制枚举砍哪些树,剩余的树围成一个凸包. 因为传数组WA了两发,忘记修改排序数组中的p[0]; 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include

POJ 1873 The Fortified Forest(凸包+枚举 World Finals 1999啊)

题目链接:http://poj.org/problem?id=1873 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees fr

计算几何专题

ACM计算几何题目推荐 一. 点,线,面,形基本关系,点积叉积的理解      POJ 2318 TOYS && POJ 2398 Toy Storage  点与线段的位置    POJ 3304 Segments  线段与直线的位置    POJ 1269 Intersecting Lines  直线位置    POJ 1556 The Doors  线段相交+最短路   POJ 2653 Pick-up sticks  线段相交    POJ 1066 Treasure Hunt  线

poj 1873(枚举所有的状态+凸包)

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6115   Accepted: 1720 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been

poj 1873 凸包+枚举

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6198   Accepted: 1744 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been