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 Input
char 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;
}