The Fortified Forest - POJ 1873(状态枚举+求凸包周长)
题目大意:有个国王他有一片森林,现在他想从这个森林里面砍伐一些树木做成篱笆把剩下的树木围起来,已知每个树都有不同的价值还有高度,求出来砍掉那些树可以做成篱笆把剩余的树都围起来,要使砍伐的树木的价值最小,如果有价值相同的尽量使砍伐的树木少一些。
分析:因为树木的数量是比较少的,所以枚举所有的状态,判断那个树需要砍那个树不需要,然后按照要求求出来答案即可。
代码如下:
==============================================================================================
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std; const double EPS = 1e-;
const int MAXN = ;
const int oo = 1e9+; int vi[MAXN], li[MAXN], sta[MAXN], top; struct point
{
double x, y; point(double x=, double y=):x(x), y(y){}
point operator - (const point &t)const{
return point(x-t.x, y-t.y);
}
double operator *(const point &t)const{
return x*t.x + y*t.y;
}
double operator ^(const point &t)const{
return x*t.y - y*t.x;
}
}p[MAXN], date[MAXN];
int Sign(double t)
{
if(t > EPS)return ;
if(fabs(t) < EPS)return ;
return -;
}
double Dist(point a, point b)
{
return sqrt((a-b)*(a-b));
}
bool cmp(point a, point b)
{
int t = Sign((a-p[])^(b-p[])); if(t == )
return Dist(a, p[]) < Dist(b, p[]);
return t > ;
}
void Graham(int N)
{
int k=; for(int i=; i<N; i++)
{
if(p[k].y>p[i].y || (Sign(p[k].y-p[i].y)== && p[k].x > p[i].x))
k = i;
}
swap(p[], p[k]);
sort(p+, p+N, cmp); sta[]=, sta[]=, top=; if(N < )
{
top = N-;
return ;
} for(int i=; i<N; i++)
{
while(top> && Sign((p[i]-p[sta[top]])^(p[sta[top-]]-p[sta[top]])) <= )
top--;
sta[++top] = i;
}
}
bool Solve(int bitNum, int N, int &val, int &nOne, double &lastLen)
{
int cut_val=, cnt=, cut_len=; for(int i=; i<N; i++)
{
if(bitNum & )
{
cut_val += vi[i];
cut_len += li[i];
}
else
p[cnt++] = date[i];
bitNum >>= ;
} Graham(cnt); double len=;
sta[++top] = sta[]; for(int i=; i<=top; i++)
{
len += Dist(p[sta[i]], p[sta[i+]]);
} if(Sign(cut_len-len) >= && (val > cut_val || (val==cut_val&&nOne > N-cnt)))
{
lastLen = cut_len - len;
val = cut_val;
nOne = N-cnt;
return true;
}
return false;
} int main()
{
int N, t=; while(scanf("%d", &N), N)
{
int val=oo, ans, nOne=oo;
double lastLen; for(int i=; i<N; i++)
scanf("%lf%lf%d%d", &date[i].x, &date[i].y, &vi[i], &li[i]); int Len = (<<N)-; for(int i=; i<Len; i++)
{
if(Solve(i, N, val, nOne, lastLen) == true)
ans = i;
}
if(t!=)printf("\n");
printf("Forest %d\n", t++);
printf("Cut these trees:");
for(int i=; ans; i++)
{
if(ans & )
printf(" %d", i);
ans >>= ;
}
printf("\nExtra wood: %.2f\n", lastLen);
} return ;
}
The Fortified Forest - POJ 1873(状态枚举+求凸包周长)的更多相关文章
- poj1873(二进制枚举+求凸包周长)
题目链接:https://vjudge.net/problem/POJ-1873 题意:n个点(2<=n<=15),给出n个点的坐标(x,y).价值v.做篱笆时的长度l,求选择哪些点来做篱 ...
- poj 1113:Wall(计算几何,求凸包周长)
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28462 Accepted: 9498 Description ...
- POJ 1113 Wall(Graham求凸包周长)
题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 #include <stdi ...
- HDU 1392 凸包模板题,求凸包周长
1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...
- Wall---hdu1348(求凸包周长 模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 求凸包周长+2*PI*L: #include <stdio.h> #include ...
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1348:Wall(计算几何,求凸包周长)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- poj 1873(枚举所有的状态+凸包)
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6115 Accepted: 1 ...
- POJ 2187 Beauty Contest【凸包周长】
题目: http://poj.org/problem?id=1113 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
随机推荐
- ie6常见的兼容性
1.IE6怪异解析之padding与border算入宽高 原因:未加文档声明造成非盒模型解析 解决方法:加入文档声明<!doctype html> 2.IE6在块元素.左右浮动.设定mar ...
- libctemplate——C语言模块引擎简介及使用
前言 由先声明此libctemplate不是Google那个ctemplate.这个库是用C语言实现的,只有一个实现文件和一个头文件.Gooogl的ctemplate是C++实现的,和线程还扯上了关系 ...
- HDU1878欧拉回路
这道题WA了好多次.测试数据感觉有点问题-- 并查集啊,必须有. #include<stdio.h> #include<string.h> int ad[1003]; int ...
- eclipse和cygwin搭建C++环境的修正版本
最近嫌弃切换系统麻烦.用了cygwin作为netbeans下C++的环境继续学习.我学的很渣,就不卖弄了. 网络上有很多这样的文章.经过对比和实验发现一个深坑.教程部分还是跟其他的一样,重点部分红字做 ...
- Django练习项目之搭建博客
背景:自从今年回家过年后,来到公司给我转了试用,我的学习效率感觉不如从前,而且刚步入社会我总是想要怎么想明白想清楚一些事,这通常会花掉,消耗我大量的精力,因为我想把我的生活管理规划好了,而在it技术学 ...
- 让IE8兼容问题,参考文档bootstrap
问题:制作的页面基本没啥问题,只有IE8不好使 参考文档:bootstrap官网 方案一 方案二
- Action配置
Action是一个逻辑控制器,并不直接对浏览器生成响应,而是返回指定逻辑视图(一个字符串). 不推荐在Action的name属性值中使用点(.)和中划线(-),有可能会引发一些未知异常. 1使用A ...
- 使用css3画饼图
CSS3 Gradient介绍参考地址: http://www.cnblogs.com/lhb25/archive/2013/01/30/css3-linear-gradient.html http: ...
- curl批处理从官方demo封装
官方demo // 创建一对cURL资源 $ch1 = curl_init(); $ch2 = curl_init(); // 设置URL和相应的选项 curl_setopt($ch1, CURLOP ...
- uboot移植之环境变量在NandFlash
一.概述 u-boot环境变量可以设置在Norflash上,也可以在NandFlash上. 倘若环境变量在NorFlash上,再假设S3C2440从NorFlash启动,是能正确从NorFlash上读 ...