WF 2008 (UVaLive4127 ) - The Sky is the Limit

来源:互联网 发布:西宁市数据运营招聘 编辑:程序博客网 时间:2024/06/03 15:35

Link To The Problem


Solution : 离散,模拟


Code :

// WF 2007 The Sky is the limit// Solution :  离散#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>using namespace std;#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)#define DOR(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)#define oo 1e6#define eps 1e-7#define nMax 1010//{ #define pb push_back#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << x << endl#define F first#define S second#define bug puts("OOOOh.....");#define zero(x) (((x)>0?(x):-(x))<eps)#define LL long long#define DB double #define sf scanf#define pf printf#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)double const pi = acos(-1.0);double const inf = 1e9;double inline sqr(double x) { return x*x; }int dcmp(double x){    if(fabs(x)<=eps) return 0;    return x>0?1:-1;}//}// Describe of the 2_K Geomtry// First Part : Point and Line// Second Part Cicle// Third Part Polygan// First Part:// ****************************** Point and Line *******************************\\//  {  class point {public:    double x,y;    point (double x=0,double y=0):x(x),y(y) {}    void make(double _x,double _y) {x=_x;y=_y;}    void read() { scanf("%lf%lf",&x,&y); }    void out() { printf("%.3lf %.3lf\n",x,y);}    double len() { return sqrt(x*x+y*y); }    point friend operator - (point const& u,point const& v) { return point(u.x-v.x,u.y-v.y); }    point friend operator + (point const& u,point const& v) { return point(u.x+v.x,u.y+v.y); }    double friend operator * (point const& u,point const& v){ return u.x*v.y-u.y*v.x; }    double friend operator ^ (point const& u,point const& v) { return u.x*v.x+u.y*v.y; }    point friend operator * (point const& u,double const& k) { return point(u.x*k,u.y*k); }point friend operator / (point const& u,double const& k) { return point(u.x/k,u.y/k); }friend bool operator < (point const& u,point const& v){if(dcmp(v.x-u.x)==0) return dcmp(u.y-v.y)<0;return dcmp(u.x-v.x)<0;}friend bool operator != (point const& u,point const& v){return dcmp(u.x-v.x) || dcmp(u.y-v.y);}point rotate(double s) {return point(x*cos(s) + y*sin(s),\-x*sin(s) + y*cos(s));}};typedef point Vector;typedef class line{public:    point a,b;    line() {}    line (point a,point b):a(a),b(b){}    void make(point u,point v) {a=u;b=v;}    void read() { a.read(),b.read(); }}segment;double det(point u,point v) {return u.x*v.y - u.y*v.x;}double dot(point u,point v) {return u.x*v.x + u.y*v.y;}// Weather P is On the Segment (uv) int dot_on_seg(point p,point u,point v){return dcmp(det(p-u,v-p))==0 && dcmp(dot(p-u,p-v)) <= 0; // '>=' means P is u or v}// The distance from point p to line ldouble PToLine(point p,line l) {return fabs((p-l.a)*(l.a-l.b))/(l.a-l.b).len();}// The ProJect Of Point(p) To Line(l)point PointProjectLine(point p,line l) {double t = dot(l.b-l.a,p-l.a)/dot(l.b-l.a,l.b-l.a);return l.a + (l.b-l.a)*t;}// Weather line u parallel line vint parallel(line u,line v) {return dcmp(det(u.a-u.b,v.a-v.b))==0;}// The Intersection Point Of Line u and Line vpoint intersection(line u,line v) {point ret = u.a;double t = det(u.a-v.a,v.a-v.b)/det(u.a-u.b,v.a-v.b);return ret + (u.b-u.a)*t;}//}// ****************************** First Part end ********************************\\// Second Part:// ********************************* Circle *************************************\\// {struct circle {point O;double r;circle() {};circle(point O,double r):O(O),r(r){};};//}// ****************************** Second Part End *******************************\\// Third Part :// ********************************* Polygan *************************************\\// {int ConvexHull(vector<point>& p){int n=p.size();int m=0;vector<point> q;q.resize(2*n+5);rep(i,n) {while(m>1 && dcmp((q[m-1]-q[m-2])*(p[i]-q[m-2])) <= 0) m--;q[m++] = p[i];}int k = m;for(int i=n-2;i>=0;i--) {while(m>k && dcmp((q[m-1]-q[m-2])*(p[i]-q[m-2])) <= 0) m--;q[m++] = p[i];}q.resize(m) ;if(m>1) q.resize(m-1);// p = q;    // 是否修改原来的多边形return q.size();}// 三角形重心point Center(point a,point b,point c){return (a+b+c)/3.0;}// Centroid of Polyganpoint Centroid(vector<point> p){point O(0,0),ret(0,0);int n = p.size();p.pb(p[0]);double area = 0.0;rep(i,n) {ret = ret + Center(O,p[i],p[i+1])*dot(p[i]-O,p[i+1]-O);area += dot(p[i]-O,p[i+1]-O);}if(dcmp(area)==0) {pf("There maybe something wrong\n");return p[0];}return ret / area;}struct Polygan{vector<point> g;Polygan() {};Polygan(vector<point> g):g(g){};Polygan(point p[],int n) {g.clear();rep(i,n) g.pb(p[i]); };int convex() { return ConvexHull(g); }point center() { return Centroid(g);  } // 多边形的重心};//}// ******************************* Third Part End ********************************\\int n;vector<double> X;vector<line> l;int intersection(line u,line v,point& ret){if(parallel(u,v)) return 0;ret = intersection(u,v);return dot_on_seg(ret,u.a,u.b) && dot_on_seg(ret,v.a,v.b);return 1;}#define SZ(a) (a.size())typedef point P;void read() {double x,h,b;X.clear();l.clear();rep(i,n) {sf("%lf%lf%lf",&x,&h,&b);X.pb(x-b/2.0);X.pb(x+b/2.0);X.pb(x);l.pb(line(P(x-b/2,0),P(x,h)));l.pb(line(P(x,h),P(x+b/2,0)));}}void init(){point tmp;for(int i=0;i<SZ(l);i++){for(int j=i+1;j<SZ(l);j++){if(intersection(l[i],l[j],tmp)){X.pb(tmp.x);}}}}double inline dis(double a,double b){return sqrt(a*a+b*b);}double GetH(double x) {double ret = 0;point p;rep(i,l.size()) {if(!parallel(l[i],line(P(x,-10),P(x,10)))){p = intersection(l[i],line(P(x,-10),P(x,10)));if(dot_on_seg(p,l[i].a,l[i].b))ret = max(ret,p.y);}}return ret;}int cas = 1;void work() {sort(X.begin(),X.end());X.resize(unique(X.begin(),X.end())-X.begin()); double ans = 0;double pre = GetH(X[0]);for(int i=1;i<X.size();i++){double now = GetH(X[i]);double disx = fabs(X[i]-X[i-1]);double disy = fabs(now - pre);if(dcmp(pre) || dcmp(now)) ans += dis(disx,disy);pre = now;}pf("Case %d: %.0lf\n\n",cas++,ans);}int main() {#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout);#endifwhile(~sf("%d",&n),n){read() ;init();work();}return 0;}


原创粉丝点击