The Fortified Forest UVALive - 5211

题意:n棵树,每棵树对应有x,y,v,l分别代表横坐标,纵坐标,价值,砍这棵树能构成的篱笆长度。现在要求输出,在最小被砍价值的情况下,输出选了哪些树,还剩下多少长的篱笆。如果有多个相同的最小值,输出选点最少的。

思路: 二进制枚举选树情况,凸包求周长,模拟下去。几何题小心精度问题

#include<cstdio>
#include<vector>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#define PI acos(-1.0)
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1005;
const int MOD=1e9+7;
template <class T>
bool sf(T &ret){ //Faster Inputchar c; int sgn; T bit=0.1;if(c=getchar(),c==EOF) return 0;while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();sgn=(c=='-')?-1:1;ret=(c=='-')?0:(c-'0');while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');if(c==' '||c=='\n'){ ret*=sgn; return 1; }while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;ret*=sgn;return 1;
}
int n;
int sign(double x){return abs(x)<1e-7?0:x<0?-1:1;
}
struct Point{int x,y;int v,l,id;Point(int x=0, int y=0,int id=0) : x(x), y(y),id(id){}Point operator - (const Point &rhs) const{return Point(x-rhs.x,y-rhs.y);}bool operator == (const Point &rhs) const{return sign(x-rhs.x)==0&&sign(y-rhs.y)==0;}bool operator < (const Point &rhs)const{if(x==rhs.x)    return y<rhs.y;else    return x<rhs.x;}
}p[N];
typedef Point Vector;
double cross(Vector A,Vector B){return (double)A.x*B.y-(double)A.y*B.x;
}
typedef vector<Point> Polygon;
Polygon convex_hull(Polygon P) {sort(P.begin(), P.end());  //排序P.erase(unique(P.begin(), P.end()), P.end());  //删除重复点int n = P.size(), k = 0;Polygon Q(n*2);for (int i=0; i<n; ++i) {while (k > 1 && cross(Q[k-1]-Q[k-2], P[i]-Q[k-2]) <= 0) k--;Q[k++] = P[i];}for (int t=k, i=n-2; i>=0; --i) {while (k > t && cross(Q[k-1]-Q[k-2], P[i]-Q[k-2]) <= 0) k--;Q[k++] = P[i];}Q.resize(k);return Q;
}
double dist(Point a,Point b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double maxv=0;
double restlen=0;
vector<int> nb;
void mian(){for(int i=0;i<n;i++)   sf(p[i].x),sf(p[i].y),sf(p[i].v),sf(p[i].l),p[i].id=i+1;for(int i=0;i<=(1<<n)-1;++i){vector<Point> t;vector<int> choose;int totlen=0,totv=0;for(int j=0;j<=n-1;j++){if((1<<j)&i)    totlen+=p[j].l,choose.pb(j+1);else    t.pb(p[j]),totv+=p[j].v;}vector<Point> ans=convex_hull(t);double dis=0;for(int i=0;i<(int)ans.size()-1;i++)    dis+=dist(ans[i],ans[i+1]);if(sign(totlen-dis)<0)  continue; // 这里直接减被卡精度。几何题判正负用sign安全if(totv>maxv){maxv=totv;restlen=totlen-dis;nb.resize((int)choose.size());for(int i=0;i<(int)choose.size();++i)nb[i]=choose[i];}else if(totv==maxv){int sz1=(int)choose.size();int sz2=(int)nb.size();if(sz1>=sz2)    continue;restlen=totlen-dis;nb.resize((int)choose.size());for(int i=0;i<(int)choose.size();++i)nb[i]=choose[i];}}}
int main(void){int ks=0;while(cin >>n){maxv=restlen=0;nb.clear();if(!n)  break;if(ks>0)    printf("\n");mian();printf("Forest %d\n",++ks);printf("Cut these trees:");
//        cout <<"sz="<<nb.size()<<endl;
//        sort(nb.begin(),nb.end());for(int i=0;i<(int)nb.size();i++)   printf(" %d",nb[i]);printf("\n");printf("Extra wood: %.2f\n",restlen);}return 0;
}

The Fortified Forest UVALive - 5211 [二进制枚举+凸包周长]相关推荐

  1. The Fortified Forest(暴力状态枚举+凸包)

    太爽了!!!!1A #include <iostream> #include <cmath> #include <algorithm> #include <c ...

  2. POJ-1873-The Fortified Forest(二进制枚举+凸包)

    题目链接:http://poj.org/problem?id=1873 题目大意:给出n棵树的位置(x,y)坐标,价值v和长度l.让你从中选择一些树,砍掉他们将其他的树围起来. 要求砍的这些树的价值之 ...

  3. poj 1873 The Fortified Forest (位运算枚举 + 凸包周长)

    题目链接:http://poj.org/problem?id=1873 大意:有一片N棵树的森林,要从中砍掉几棵树做成篱笆,把剩下的树围起来 输入:给N课树,每棵树的坐标是x,y,每棵树有一个vi和l ...

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

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

  5. POJ 1873 The Fortified Forest(枚举+凸包+剪枝)

    POJ 1873 The Fortified Forest 题意: 某王有一些树,要制作一个栅栏将所有树围起来,栅栏制作木材来源就是砍去其中一些树. 现在输入每棵树的坐标,价值,高度,求出所围价值最大 ...

  6. POJ 1873 The Fortified Forest (凸包,状态压缩枚举)

    题目链接:http://poj.org/problem?id=1873 题意:给出一些树,每棵树有坐标,高度,以及价值,要求砍掉一些树,用那些木材,将其它树围起来,要求花最小的代价,代价相同,要求砍掉 ...

  7. uva 811 - The Fortified Forest(暴力+凸包)

    题目链接:uva 811 - The Fortified Forest 最多就15棵树,枚举哪些树要砍,剩下的做凸包. #include <cstdio> #include <cst ...

  8. poj1873 The Fortified Forest (状压+凸包)

    题意: 平面上有n棵树(2~15),现在要砍掉其中的一部分来做成篱笆将剩下的树包围起来,现在给出每棵树的坐标.价值和可以制造篱笆的长度,求砍掉最少价值的树,将剩下的树包围起来,当两种方式的价值相同时, ...

  9. 【poj】1873: The Fortified Forest (凸包+状态压缩+各种搞事)

    题目描述: The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7818   Accep ...

最新文章

  1. 30岁前挣够500万
  2. 用Python对XML读取和处理
  3. 利用Maya进行论文中网格动画数据的渲染
  4. ubuntu下vim语法高亮问题
  5. Docker-Compose快速搭建Oracle-12C系统
  6. gridview绑定数据mysql_【gridview控件】怎么绑定数据库并显示?
  7. Swagger 3.0 官方 starter 诞生,野生的可以扔了!
  8. Visual Studio 2010添加新项缺失[ADO.NET 实体数据模型]解决方法
  9. 台式计算机刚做完系统就蓝屏,做完系统蓝屏怎么办|刚做完系统重启电脑蓝屏解决方法...
  10. 【无人机 学习笔记 2】无人机导航制导与控制
  11. 网吧管理系统的设计与实现
  12. Windows系统的Chrome 调试运行在 IOS-safari (iPad/iPhone)上的页面
  13. 解决数据库查询结果过大导致的录制失败问题(loadrunner)
  14. 凯云水利水电工程造价管理系统 技术解析(七) 机械单价(二)
  15. NLP学习笔记(四) Seq2Seq基本介绍
  16. python:文档转换(只支持Windows操作系统)
  17. 信息安全竞赛解决方案
  18. mysql varchar 单引号_char、varchar数据类型值在使用时可以要用单引号或双引号括起来。...
  19. 微信网页分享给朋友或朋友圈只有url链接
  20. 数据库恢复时的四种转储方法

热门文章

  1. 从2013到2017 CGU 草根战队的电竞梦
  2. 华为——让华盛顿感到恐慌的中国公司(转载)
  3. Transporter上传ipa 卡在正在验证 App - 正在通过App Store进行认证
  4. 开启Excel的自动保存功能(转)
  5. Centos7搭建本地Web服务器
  6. 一个阳光女孩儿的来信——Leo网上答疑(21)
  7. redis持久化到mysql的方案_redis实现持久化存储的两种方案
  8. Telent远程登入实验
  9. 梦幻西游原服务器物品,梦幻西游:一个与世隔绝的服务器,物价自己定,无人能转出去!...
  10. validationEngine中文版 — jquery强大的表单验证插件