POJ 1873 The Fortified Forest(凸包)题解
题意:二维平面有一堆点,每个点有价值v和删掉这个点能得到的长度l,问你删掉最少的价值能把剩余点围起来,价值一样求删掉的点最少
思路:n<=15,那么直接遍历2^15,判断每种情况。这里要优化一下,如果价值比当前最优大了continue。POJ的G++输出要用%f...orz,还是乖乖用C++...
代码:
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = + ;
const int MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
struct node{
double x, y, l;
int v, id;
}p[maxn], s[maxn], q[maxn];
int n, top;
double dis(node a, node b){
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool cmp(node a, node b){
double A = atan2((a.y - p[].y), (a.x - p[].x));
double B = atan2((b.y - p[].y), (b.x - p[].x));
if(A != B) return A < B;
else{
return dis(a, p[]) < dis(b, p[]);
}
}
double cross(node a, node b, node c){ //(a->b)X(a->c)
return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
}
void solve(){
int pos = ;
for(int i = ; i <= n; i++){
if(p[i].y < p[pos].y || (p[i].y == p[pos].y && p[i].x < p[pos].x)){
pos = i;
}
}
swap(p[], p[pos]);
sort(p + , p + n + , cmp);
s[] = p[], s[] = p[];
top = ;
for(int i = ; i <= n; i++){
while(top >= && cross(s[top - ], p[i], s[top]) >= ){
top--;
}
s[++top] = p[i];
}
}
double need(double len){
if(n <= ) return len;
if(n == ){
return len - 2.0 * dis(p[], p[]);
}
solve();
double Need = ;
for(int i = ; i < top; i++){
Need += dis(s[i], s[i + ]);
}
Need += dis(s[top], s[]);
return len - Need;
}
int main(){
int N, ca = ;
while(~scanf("%d", &N) && N){
for(int i = ; i < N; i++){
scanf("%lf%lf%d%lf", &q[i].x, &q[i].y, &q[i].v, &q[i].l);
q[i].id = i + ;
}
int sz = , que[], tempQue[];
int MinValue = INF;
double ans = ;
for(int i = ; i < ( << N); i++){
int num = , value = ;
double len = ;
n = ;
for(int j = ; j < N; j++){
if(i & ( << j)){
p[++n] = q[j];
}
else{
value += q[j].v;
len += q[j].l;
tempQue[num] = q[j].id;
num++;
}
}
if(value > MinValue) continue;
double tmp = need(len);
if(tmp < ) continue;
if(value < MinValue || (value == MinValue && num < sz)){
MinValue = value;
ans = tmp;
sz = num;
for(int j = ; j < num; j++){
que[j] = tempQue[j];
}
}
}
if(ca != ) printf("\n");
printf("Forest %d\n", ca++);
printf("Cut these trees: ");
for(int i = ; i < sz; i++){
printf("%d ", que[i]);
}
printf("\n");
printf("Extra wood: %.2lf\n", ans);
}
return ;
}
POJ 1873 The Fortified Forest(凸包)题解的更多相关文章
- POJ 1873 The Fortified Forest [凸包 枚举]
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6400 Accepted: 1 ...
- POJ 1873 - The Fortified Forest 凸包 + 搜索 模板
通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 &g ...
- POJ 1873 The Fortified Forest 凸包 二进制枚举
n最大15,二进制枚举不会超时.枚举不被砍掉的树,然后求凸包 #include<stdio.h> #include<math.h> #include<algorithm& ...
- 简单几何(凸包+枚举) POJ 1873 The Fortified Forest
题目传送门 题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值 分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度.虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘 ...
- POJ 1873 The Fortified Forest(枚举+凸包)
Description Once upon a time, in a faraway land, there lived a king. This king owned a small collect ...
- ●POJ 1873 The Fortified Forest
题链: http://poj.org/problem?id=1873 题解: 计算几何,凸包 枚举被砍的树的集合.求出剩下点的凸包.然后判断即可. 代码: #include<cmath> ...
- POJ 1873 The Fortified Forest
题意:是有n棵树,每棵的坐标,价值和长度已知,要砍掉若干根,用他们围住其他树,问损失价值最小的情况下又要长度足够围住其他树,砍掉哪些树.. 思路:先求要砍掉的哪些树,在求剩下的树求凸包,在判是否可行. ...
- poj1873 The Fortified Forest 凸包+枚举 水题
/* poj1873 The Fortified Forest 凸包+枚举 水题 用小树林的木头给小树林围一个围墙 每棵树都有价值 求消耗价值最低的做法,输出被砍伐的树的编号和剩余的木料 若砍伐价值相 ...
- Uva5211/POJ1873 The Fortified Forest 凸包
LINK 题意:给出点集,每个点有个价值v和长度l,问把其中几个点取掉,用这几个点的长度能把剩下的点围住,要求剩下的点价值和最大,拿掉的点最少且剩余长度最长. 思路:1999WF中的水题.考虑到其点的 ...
随机推荐
- jdbc连接oracle的几种格式
1. SID的方式.已经不推荐使用这种方式了. jdbc:oracle:thin:[<user>/<password>]@<host>[:<port>] ...
- Mockito/PowerMockito Straige Issues
http://blog.csdn.net/xiaoyaoyulinger/article/details/52415494 http://breezylee.iteye.com/blog/208843 ...
- python中的lxml模块
Python中自带了XML的模块,但是性能不太好,相比之下,LXML增加了很多实用的功能. lxml中主要有两部分, 1) etree,主要可以用来解析XML字符串, 内部有两个对象,etree._E ...
- Redis实现分布式Session
相关博客: http://www.cnblogs.com/yanweidie/p/4763556.html http://www.cnblogs.com/lori/p/5368722.html?utm ...
- html5-新增表单的小结details summary
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- B树,B+树,B*树以及R树的介绍
https://blog.csdn.net/peterchan88/article/details/52248714 作者:July.weedge.Frankie.编程艺术室出品. 说明:本文从B树开 ...
- jQuery安装
http://www.runoob.com/jquery/jquery-install.html 网页中添加jQuery: 方法一:可以从http://jquery.com/download/ 下载j ...
- jQuery选择器--selector1,selector2,selectorN和ancestor descendant
selector1,selector2,selectorN 概述 将每一个选择器匹配到的元素合并后一起返回.你可以指定任意多个选择器,并将匹配到的元素合并到一个结果内 参数 selector1 ...
- MVC 下拉列表三级联动
当前所做的项目,关于数据库设计的时候有点小意思,表A是三个联合主键,key1,key2,key3,表B是四个联合主键 key1,key2,key3,key4,其中表B的联合外键关联表A对应的联合主键, ...
- XML系列之--解析电文格式的XML(二)
上一节介绍了XML的结构以及如何创建.讲到了XML可作为一种简单文本存储数据,把数据存储起来,以XML的方式进行传递.当接收到XML时,必不可少的就是对其进行解析,捞取有效数据,或者将第三方数据以节点 ...