poj 1873

哇实验室里正在吵架,爽死了!

wf水题。显然二进制枚举,注意剪枝,val>ans的时候剪一下,不然会tle。然后就没惹。

我老人家一开始写了个

poj 1873

感觉非常垃圾,wa了一发又t了一发。

感觉自己可以退役了

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
typedef double db;
#define pdd pair<db,db>
const db eps = 1e-;
const db pi = acos(-);
using namespace std;
int sign(db k){
if (k>eps) return ; else if (k<-eps) return -; return ;
}
int cmp(db k1,db k2){return sign(k1-k2);}
struct point{
db x,y;
db v,l;
point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};}
point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};}
point operator * (db k1) const{return (point){x*k1,y*k1};}
point operator / (db k1) const{return (point){x/k1,y/k1};}
bool operator <(const point &k1)const {
int c=cmp(x,k1.x);
if(c)return c==-;
return cmp(y,k1.y)==-;
}
db abs(){ return sqrt(x*x+y*y);}
db dis(point k1){return ((*this)-k1).abs();}
};
db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;}
db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;}
vector<point> convexHull(vector<point> ps){
int n = ps.size();if(n<=)return ps;
sort(ps.begin(),ps.end());
vector<point> qs(n*);int k=;
for(int i=;i<n;qs[k++]=ps[i++])
while (k>&&cross(qs[k-]-qs[k-],ps[i]-qs[k-])<=)--k;
for(int i=n-,t=k;i>=;qs[k++]=ps[i--])
while (k>t&&cross(qs[k-]-qs[k-],ps[i]-qs[k-])<=)--k;
qs.resize(k-);
return qs;
}
int n;
point p[];
vector<point> v;
db res=,sum=1e9;int ans=-;
void slove(int x){
v.clear();
db l1=,val=;
for(int i=;i<n;i++){
if(&(x>>i)){//砍了
l1+=p[i].l;
val+=p[i].v;
} else{
v.push_back(p[i]);
}
}
if(cmp(val,sum)>=)
return;
v=convexHull(v);
db l2=;
for(int i=;i<v.size();i++){
l2+=v[i].dis(v[(i+)%v.size()]);
}
if(v.size()==)l2=;
if(cmp(l1,l2)>=){//阔以
sum=val;
res=l1-l2;
ans=x;
}
}
int main(){
int cas = ;
while (scanf("%d",&n)&&n){
cas++;
for(int i=;i<n;i++){
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].v,&p[i].l);
}
for(int i=;i<(<<n)-;i++){
slove(i);
}
if(cas>=)printf("\n");
printf("Forest %d\n",cas);
printf("Cut these trees:");
for(int i=;i<n;i++){
if((ans>>i)&){
printf(" %d",i+);
}
}
printf("\nExtra wood: %.2f\n",res);
res=,sum=1e9;
}
}
上一篇:c++算法联系,冒泡排序,bubble sort,插入排序,insert sort,


下一篇:位操作Bit Operation算法题