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 (SoungEarlf@gmail.com)
* @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;
}

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

  1. poj1873 The Fortified Forest 凸包+枚举 水题

    /* poj1873 The Fortified Forest 凸包+枚举 水题 用小树林的木头给小树林围一个围墙 每棵树都有价值 求消耗价值最低的做法,输出被砍伐的树的编号和剩余的木料 若砍伐价值相 ...

  2. POJ 1873 The Fortified Forest [凸包 枚举]

    The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6400   Accepted: 1 ...

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

    通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 &g ...

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

    题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a ...

  5. POJ 1873 The Fortified Forest 凸包 二进制枚举

    n最大15,二进制枚举不会超时.枚举不被砍掉的树,然后求凸包 #include<stdio.h> #include<math.h> #include<algorithm& ...

  6. 简单几何(凸包+枚举) POJ 1873 The Fortified Forest

    题目传送门 题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值 分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度.虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘 ...

  7. POJ 1873 The Fortified Forest(枚举+凸包)

    Description Once upon a time, in a faraway land, there lived a king. This king owned a small collect ...

  8. The Fortified Forest - POJ 1873(状态枚举+求凸包周长)

    题目大意:有个国王他有一片森林,现在他想从这个森林里面砍伐一些树木做成篱笆把剩下的树木围起来,已知每个树都有不同的价值还有高度,求出来砍掉那些树可以做成篱笆把剩余的树都围起来,要使砍伐的树木的价值最小 ...

  9. POJ 1873 The Fortified Forest(凸包)题解

    题意:二维平面有一堆点,每个点有价值v和删掉这个点能得到的长度l,问你删掉最少的价值能把剩余点围起来,价值一样求删掉的点最少 思路:n<=15,那么直接遍历2^15,判断每种情况.这里要优化一下 ...

随机推荐

  1. 软件工程-东北师大站-第七次作业(PSP)

    1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图

  2. .net web 应用程序C#

    简介 开发环境:VS2015 ASP.NET:可以开发出几乎所有运行在Windows上的应用程序:.NET是一种架构,一种新的API:引入程序集代替DLL: ADO.NET:一组.NET组件提供对数据 ...

  3. 严重: Failed to destroy end point associated with ProtocolHandler ["http-nio-8080"] java.lang.NullPointer

    刚接触servlet类,按照课本的方法使用eclipse新建了一个servlet类. 新建完成后,在web.xml里面进行注册 这时候就会报错了. 五月 07, 2016 11:23:28 上午 or ...

  4. 《剑指offer》---左旋转字符串与右旋转字符串

    本文算法使用python3实现 1. 问题1 1.1 题目描述:   汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S, ...

  5. Strust2: 工作流程

    以下为Struts2的体系结构图: Struts2框架处理用户请求,大体分为以下几个过程: (1)用户发出一个HttpServletRequest请求 (2)请求经过一系列过滤器,最后达到Filter ...

  6. DB2 9.5 数据库分区管理及应用实践

    DB2 数据库分区是 DB2 企业版 DPF(Data Partitioning Feature)选件提供的,它主要用来为大规模数据处理.高并发数据访问提供支持.DB2 数据库分区采用 Share-n ...

  7. 给新建的kvm虚拟机创建网络接口

    (一)首先必须创建网卡连接桥接口的启动脚本和停止脚本,其中脚本中的 $1:表示为虚拟机的网卡的右边接口,这两个脚本就是讲虚拟机的网卡的右边接口接在网桥上,实现桥接模型     # 1:/etc/qem ...

  8. Mac下使用svn命令

    Mac系统自带svn命令,能够很方便的同步更新代码,使用方法: 1.导入项目svn import /Users/username/Desktop/Project1 svn://192.168.1.12 ...

  9. 两个float 怎么比较大小

    转自:http://blog.csdn.net/mydriverc2/article/details/49888947 float 类型不能比较相等或不等,但可以比较>,<,>=,& ...

  10. 第209天:jQuery运动框架封装(二)

    运动框架 一.函数------单物体运动框架封装 1.基于时间的运动原理 动画时间进程 动画距离进程 图解: 物体从0移动到400 当物体移动到200的时候 走了50% 同样的,物体总共运行需要4秒 ...