Triathlon

【题目链接】Triathlon

【题目类型】半平面交

&题解:

做了2道了,感觉好像套路,都是二分答案,判断半平面交是否为空.

还有刘汝佳的代码总是写const +& 但是我今天试了6次,3次const +& 和3次直接参数传值,发现时间只差了5ms左右,所以我觉得以后不用总是写const +&,因为写的代码长了,但又没有加快多少.

&代码:

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. struct Point {
  7. double x, y;
  8. Point(double x = 0, double y = 0): x(x) , y(y) {}
  9. };
  10. typedef Point Vector;
  11. Vector operator + (const Vector& A, const Vector& B) { return Vector(A.x + B.x, A.y + B.y); }
  12. Vector operator - (const Vector& A, const Vector& B) { return Vector(A.x - B.x, A.y - B.y); }
  13. Vector operator * (const Vector& A, double p) { return Vector(A.x * p, A.y * p); }
  14. double Dot(const Vector& A, const Vector& B) { return A.x * B.x + A.y * B.y; }
  15. double Cross(const Vector& A, const Vector& B) { return A.x * B.y - A.y * B.x; }
  16. double Length(const Vector& A) { return sqrt(Dot(A, A)); }
  17. Vector Normal(const Vector& A) { double l = Length(A) ; return Vector(-A.y / l , A.x / l); }
  18. double PolygonArea(vector<Point> p) {
  19. int n = p.size();
  20. double ans = 0;
  21. for(int i = 1; i < n - 1; i++) {
  22. ans += Cross(p[i] - p[0], p[i + 1] - p[0]);
  23. }
  24. return ans / 2;
  25. }
  26. struct Line {
  27. Point p, v;
  28. double ang;
  29. Line() {}
  30. Line(Point p, Vector v): p(p), v(v) { ang = atan2(v.y, v.x);}
  31. bool operator < (const Line& l) const {
  32. return ang < l.ang;
  33. }
  34. };
  35. bool OnLeft(const Line& l, const Point& p) {
  36. return Cross(l.v, p - l.p) > 0;
  37. }
  38. Point GetLineIntersection(const Line& a, const Line& b) {
  39. Vector u = a.p - b.p;
  40. double t = Cross(b.v, u) / Cross(a.v, b.v);
  41. return a.p + a.v * t;
  42. }
  43. const double eps = 1e-6;
  44. vector<Point> HalfplaneIntersection(vector<Line> L) {
  45. int n = L.size();
  46. sort(L.begin(), L.end());
  47. int first, last;
  48. vector<Point> p(n), ans;
  49. vector<Line> que(n);
  50. que[first = last = 0] = L[0];
  51. for(int i = 1; i < n; i++) {
  52. while(first < last && !OnLeft(L[i], p[last - 1])) last--;
  53. while(first < last && !OnLeft(L[i], p[first])) first++;
  54. que[++last] = L[i];
  55. if(fabs(Cross(que[last].v, que[last - 1].v)) < eps) {
  56. last--;
  57. //This is que[last] not que[i]
  58. if(OnLeft(que[last], L[i].p)) que[last] = L[i];
  59. }
  60. if(first < last) {
  61. p[last - 1] = GetLineIntersection(que[last - 1], que[last]);
  62. }
  63. }
  64. while(first < last && !OnLeft(que[first], p[last - 1])) last--;
  65. if(last - first <= 1) return ans;
  66. p[last] = GetLineIntersection(que[last], que[first]);
  67. for(int i = first; i <= last; i++)
  68. ans.push_back(p[i]);
  69. return ans;
  70. }
  71. const int maxn = 110;
  72. int V[maxn], U[maxn], W[maxn];
  73. int main() {
  74. //("E:1.in", "r", stdin);
  75. int n;
  76. while(scanf("%d", &n) == 1 && n) {
  77. for(int i = 0; i < n; i++) {
  78. scanf("%d%d%d", &V[i], &U[i], &W[i]);
  79. }
  80. for(int i = 0; i < n; i++) {
  81. int ok = 1;
  82. double k = 10000;
  83. vector<Line> L;
  84. for(int j = 0; j < n; j++) if(i != j) {
  85. if(V[i] <= V[j] && U[i] <= U[j] && W[i] <= W[j]) { ok = 0; break; }
  86. if(V[i] >= V[j] && U[i] >= U[j] && W[i] >= W[j]) { continue; }
  87. double a = (k / V[j] - k / W[j]) - (k / V[i] - k / W[i]);
  88. double b = (k / U[j] - k / W[j]) - (k / U[i] - k / W[i]);
  89. double c = k / W[j] - k / W[i];
  90. Point p;
  91. Vector v(b, -a);
  92. if(fabs(a) > fabs(b)) p = Point(-c / a , 0);
  93. else p = Point(0 , -c / b);
  94. L.push_back(Line(p, v));
  95. }
  96. if(ok) {
  97. // x>0, y>0, x+y<1 ==> -x-y+1>0
  98. L.push_back(Line(Point(0, 0), Vector(0, -1)));
  99. L.push_back(Line(Point(0, 0), Vector(1, 0)));
  100. L.push_back(Line(Point(0, 1), Vector(-1, 1)));
  101. vector<Point> poly = HalfplaneIntersection(L);
  102. if(poly.empty()) ok = 0;
  103. }
  104. puts(ok ? "Yes" : "No");
  105. }
  106. }
  107. return 0;
  108. }

LA 2218 Triathlon(半平面交)的更多相关文章

  1. POJ 1755 Triathlon [半平面交 线性规划]

    Triathlon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6912   Accepted: 1790 Descrip ...

  2. LA2218 Triathlon /// 半平面交 oj22648

    题目大意: 铁人三项分连续三段:游泳 自行车 赛跑 已知各选手在每个单项中的速度v[i],u[i],w[i] 设计每个单项的长度 可以让某个特定的选手获胜 判断哪些选手有可能获得冠军 输出n行 有可能 ...

  3. LA 2218 (半平面交) Triathlon

    题意: 有n个选手,铁人三项有连续的三段,对于每段场地选手i分别以vi, ui 和 wi匀速通过. 对于每个选手,问能否通过调整每种赛道的长度使得他成为冠军(不能并列). 分析: 粗一看,这不像一道计 ...

  4. POJ 1755 Triathlon (半平面交)

    Triathlon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4733   Accepted: 1166 Descrip ...

  5. POJ 1755 Triathlon(线性规划の半平面交)

    Description Triathlon is an athletic contest consisting of three consecutive sections that should be ...

  6. 简单几何(半平面交+二分) LA 3890 Most Distant Point from the Sea

    题目传送门 题意:凸多边形的小岛在海里,问岛上的点到海最远的距离. 分析:训练指南P279,二分答案,然后整个多边形往内部收缩,如果半平面交非空,那么这些点构成半平面,存在满足的点. /******* ...

  7. LA 3890 (半平面交) Most Distant Point from the Sea

    题意: 给出一个凸n边形,求多边形内部一点使得该点到边的最小距离最大. 分析: 最小值最大可以用二分. 多边形每条边的左边是一个半平面,将这n个半平面向左移动距离x,则将这个凸多边形缩小了.如果这n个 ...

  8. LA 4992 Jungle Outpost(半平面交)

    Jungle Outpost [题目链接]Jungle Outpost [题目类型]半平面交 &题解: 蓝书282 我自己写的代码居然AC了!!! 刘汝佳的说要right要-3什么的,还要特判 ...

  9. LA 3890 Most Distant Point from the Sea(半平面交)

    Most Distant Point from the Sea [题目链接]Most Distant Point from the Sea [题目类型]半平面交 &题解: 蓝书279 二分答案 ...

随机推荐

  1. [No0000F9]C# 运算符重载

    您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的.与其他函数一样,重载运 ...

  2. CentOS-6.9安装配置JDK-7

    CentOS-6.9安装配置JDK-7 安装说明 系统环境:centos-6.9安装方式:rpm安装 软件:jdk-7u79-linux-x64.rpm下载地址:http://download.ora ...

  3. debian设置软件源为阿里云

    首先编辑sources.list这个文件 sudo vim /etc/apt/sources.list 把sources.list文件内容替换成如下 deb http://mirrors.aliyun ...

  4. 免费SSL证书Let's Encrypt(certbot)安装使用教程

    免费SSL证书Let's Encrypt(certbot)安装使用教程 https://www.vpser.net/build/letsencrypt-certbot.html

  5. c# http get post转义HttpUtility.UrlEncode

    //该数据如果要http get.post提交,需要经过转义,否则该数据中含& ''等字符会导致意外错误.需要转义.这里用HttpUtility.UrlEncode来转义.接收方无需反解析 s ...

  6. [Day5]方法

    1.方法 (1)概念:方法就是用来完成解决某件事情或实现某个功能的办法 会包含很多条语句用于完成某些有意义的功能 通过在程序代码中引用方法名称和所需的参数,实现在该程序中执行(或称调用)该方法 (2) ...

  7. odoo 权限设置

    *权限管理的四个层次    # 菜单级别:不属于指定菜单所包含组的用户看不到该菜单,不客全,只是隐藏                 菜单,若知道菜单ID,仍然可以通过指定URL访问    # 对象级 ...

  8. c++ 如何获取多线程的返回值?(std::thread ,std::async)

    //简单的 c++11 线程,简单方便,成员函数随便调用,非成员函数也一样,如需要获取返回时,请自行使用条件变量 std::thread run([&](){ //执行一些耗时的操作 retu ...

  9. java Arrays工具

    package cn.sasa.demo4; import java.util.Arrays; public class ArrayDemo { public static void main(Str ...

  10. 递归、嵌套for循环、map集合方式实现树形结构菜单列表查询

    有时候, 我们需要用到菜单列表,但是怎么样去实现一个菜单列表的编写呢,这是一重要的问题. 比如我们需要编写一个树形结构的菜单,那么我们可以使用JQuery的zTree插件:http://www.tre ...