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中的水题.考虑到其点的 ...
随机推荐
- EXTENDED LIGHTS OUT (高斯消元)
In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual ...
- session_start 统计实时访客人数
void Session_Start(object sender, EventArgs e) { Application.Lock(); Application["OnLine"] ...
- java 序列化和反序列化的实现原理
老是听说序列化反序列化,就是不知道到底什么是序列化,什么是反序列化?今天就在网上搜索学习一下,这一搜不要紧,发现自己曾经用过,竟然不知道那就是JDK类库中序列化和反序列化的API. ----什么是序列 ...
- 20165305 苏振龙《Java程序设计》第五周学习总结
第七章 Java支持在一个类中声明另一个类,这样的类称作内部类,而包含内部类的类成为内部类的外嵌类. 和某类有关的匿名类就是该类的一个子类,该子类没有明显的用类声明来定义,所以称做匿名类. 和某接口有 ...
- django 常用方法总结 < 手写分页-上传头像-redis缓存,排行 ...>
1.不使用自带模块<Paginator>的手写分页功能views.pydef post_list(request): page = request.GET.get('page', 1) # ...
- django -- 修改admin 密码问题
1.python manage.py shell 2.from django.contrib.auth.models import User 3.user=User.objects.get(usern ...
- .net core创建项目(指令方式)
所谓的指令创建项目,就是不用再已安装的VS2015的环境下或者VS Core下创建,直接通过DOS指令创建也是OK的. 1.找到你所准备保存项目的项目文件夹(你也可以到某个目录用指令创建项目文件夹[ ...
- GoldenGate 12.3 MA架构介绍系列(1) - 安装
GoldenGate 12.3微服务架构与传统架构的区别可参考: http://www.cnblogs.com/margiex/p/7439574.html 下载地址:http://www.oracl ...
- dropout——gluon
https://blog.csdn.net/lizzy05/article/details/80162060 from mxnet import nd def dropout(X, drop_prob ...
- FFMPEG编译参数解析
Standard options: 基本选项参数 --help 显示此帮助信息|print this message --log[=FILE|yes|no] 记录测试并输出到config.err文件| ...