uva 1077 - The Sky is the Limit(离散化)
来源:互联网 发布:标致pp2000软件下载 编辑:程序博客网 时间:2024/06/06 05:17
题目链接:uva 1077 - The Sky is the Limit
代码
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxn = 200005;const double eps = 1e-8;struct Point { double x, y; Point(double x = 0, double y = 0): x(x), y(y) {} Point operator + (const Point& u) { return Point(x+u.x, y+u.y); } Point operator - (const Point& u) { return Point(x-u.x, y-u.y); } Point operator * (const double& k) { return Point(x*k, y*k); }};typedef Point Vector;inline double dcmp(double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }double getCross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }double getDot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }double getLength(Vector a) { return sqrt(getDot(a, a)); }/* 判断线段是否存在交点 */bool haveIntersection (Point a1, Point a2, Point b1, Point b2) { double c1=getCross(a2-a1, b1-a1), c2=getCross(a2-a1, b2-a1), c3=getCross(b2-b1, a1-b1), c4=getCross(b2-b1,a2-b1); return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;}/* 直线pv和直线qw的交点 */bool getIntersection (Point p, Vector v, Point q, Vector w, Point& o) { if (dcmp(getCross(v, w)) == 0) return false; Vector u = p - q; double k = getCross(w, u) / getCross(v, w); o = p + v * k; return true;}int N, C;double X[maxn];Point L[maxn][2], R[maxn][2];void init () { C = 0; double x, h, b; for (int i = 0; i < N; i++) { scanf("%lf%lf%lf", &x, &h, &b); X[C++] = x - b * 0.5; X[C++] = x; X[C++] = x + b * 0.5; L[i][0] = Point(x - b * 0.5, 0); R[i][0] = L[i][1] = Point(x, h); R[i][1] = Point(x + b * 0.5, 0); } Point tmp; for (int i = 0; i < N; i++) { for (int ki = 0; ki < 2; ki++) { for (int j = i+1; j < N; j++) { for (int kj = 0; kj < 2; kj++) { if (haveIntersection (L[i][ki], R[i][ki], L[j][kj], R[j][kj])) { getIntersection (L[i][ki], R[i][ki]-L[i][ki], L[j][kj], R[j][kj]-L[j][kj], tmp); X[C++] = tmp.x; } } } } }}double find (double x) { double ret = 0; Point tmp; for (int i = 0; i < N; i++) { for (int j = 0; j < 2; j++) { if (dcmp(L[i][j].x - x) <= 0 && dcmp(x - R[i][j].x) <= 0) { //double haha = L[i][j].y + (x - L[i][j].x) / (R[i][j].x - L[i][j].x) * (R[i][j].y - L[i][j].y); getIntersection(L[i][j], R[i][j]-L[i][j], Point(x, 0), Vector(0, 1), tmp); ret = max(ret, tmp.y); } } } return ret;}double solve () { sort(X, X + C); C = unique(X, X + C) - X; double ans = 0, pre = find(X[0]); for (int i = 1; i < C; i++) { double cur = find(X[i]); if (dcmp(cur) > 0 || dcmp(pre) > 0) ans += getLength(Point(X[i], cur) - Point(X[i-1], pre)); pre = cur; } return ans;}int main () { int cas = 1; while (scanf("%d", &N) == 1 && N) { init(); printf("Case %d: %.0lf\n\n", cas++, solve()); } return 0;}
0 0
- uva 1077 - The Sky is the Limit(离散化)
- LA4127 The Sky is the Limit(离散化)
- World Final 2008 ,LA 4127 The Sky is the Limit 离散化
- WF 2008 (UVaLive4127 ) - The Sky is the Limit
- The Brain is wider than the Sky !
- The sky in my heart is raining...
- 异常---is exceeding the 65535 bytes limit
- UVa-12069-Robots inside the Labyrinth (离散化+bfs)
- UVA The die is cast (BFS)
- where is the marble?(uva 10474)
- The Scar of the Sky
- Letter to the Sky
- PIE IN THE SKY
- Under The Same Sky
- The Langkawi Sky Association
- (intermediate) 网络流(时序模型+离散化) UVA 11167 - Monkeys in the Emei Mountain
- jsp: the code is exceeding the 65535 bytes limit
- UVa Where is the Marble?
- mybatis实战教程之开发环境搭建、以接口的方式编程、实现数据的增删改查
- 推荐系统导论笔记(六)
- leetcode之Burst Balloons
- python 中 类, 模块, 包的组织
- 实时监听textField的值改变事件
- uva 1077 - The Sky is the Limit(离散化)
- 高精度加法
- 二分问题
- 有符号与无符号
- 简单Prim()算法
- Spring Actuator源码分析
- 浅谈RAM、ROM和FLASH
- CompositePattern--HeadFirst学习
- vs2013 配置 freeglut3.0(opengl的窗口系统库)