标签:ini nes written star search pes code == oge
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:11940 | Accepted: 3730 |
Description
Input
Output
Sample Input
4 0 1 2 2 4 1 6 4 6 0 1 2 -0.6 5 -4.45 7 -5.57 12 -10.8 17 -16.55 0
Sample Output
4.67 Through all the pipe.
Source
题意:
给n个点 构成两条平行的折线
问从管道口出发的光线最远能到达的横坐标
思路:
最远的光线一定是贴着管道的某两个端点走的
现在枚举这两个端点 判断其与后面折线的交点
刚开始没想到判断交点时 可以先判断line 和 line(up[k], down[k])
这样得到的k就是最小的不能达到的k
用这个k就可以拿来算line 和 line(up[k-1], up[k])以及line(down[k -1], down[k])的交点了
1 //#include <bits/stdc++.h> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<stdio.h> 6 #include<cstring> 7 8 using namespace std; 9 typedef long long int LL; 10 11 #define zero(x) (((x) > 0? (x) : -(x)) < eps) 12 const double eps = 1e-8; 13 int sgn(double x) 14 { 15 if(fabs(x) < eps) return 0; 16 if(x < 0) return -1; 17 else return 1; 18 } 19 20 struct point{ 21 double x, y; 22 point(){} 23 point(double _x, double _y) 24 { 25 x = _x; 26 y = _y; 27 } 28 point operator -(const point &b)const 29 { 30 return point(x - b.x, y - b.y); 31 } 32 double operator ^(const point &b)const 33 { 34 return x * b.y - y * b.x; 35 } 36 double operator *(const point &b)const 37 { 38 return x * b.x + y * b.y; 39 } 40 void input() 41 { 42 scanf("%lf%lf", &x, &y); 43 } 44 }; 45 46 struct line{ 47 point s, e; 48 line(){} 49 line(point _s, point _e) 50 { 51 s = _s; 52 e = _e; 53 } 54 //0表示直线重合,1表示平行,2相交 55 pair<int, point>operator &(const line &b)const 56 { 57 point res = s; 58 if(sgn((s - e) ^ (b.s - b.e)) == 0){ 59 if(sgn((s - b.e) ^ (b.s - b.e)) == 0){ 60 return make_pair(0, res); 61 } 62 else return make_pair(1, res); 63 } 64 double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e)); 65 res.x += (e.x - s.x) * t; 66 res.y += (e.y - s.y) * t; 67 return make_pair(2, res); 68 } 69 }; 70 71 //判断直线与线段相交 72 bool seg_inter_line(line l1, line l2) 73 { 74 return sgn((l2.s - l1.e) ^ (l1.s - l1.e)) * sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= 0; 75 } 76 77 int n; 78 point up[100], down[100]; 79 int main() 80 { 81 while(scanf("%d", &n) != EOF && n != 0){ 82 for(int i = 0; i < n; i++){ 83 up[i].input(); 84 down[i].x = up[i].x; 85 down[i].y = up[i].y - 1.0; 86 } 87 88 89 bool flag = false; 90 double ans = -100000000.0; 91 int k; 92 for(int i = 0; i < n; i++){ 93 for(int j = i + 1; j < n; j++){ 94 for(k = 0; k < n; k++){ 95 if(seg_inter_line(line(up[i], down[j]), line(up[k], down[k])) == 0){ 96 break; 97 } 98 } 99 if(k >= n){ 100 flag = true; 101 break; 102 } 103 if(k > max(i, j)){ 104 if(seg_inter_line(line(up[i], down[j]), line(up[k - 1], up[k]))){ 105 pair<int, point>pr = line(up[i], down[j]) & line(up[k - 1], up[k]); 106 point p = pr.second; 107 ans = max(ans, p.x); 108 } 109 if(seg_inter_line(line(up[i], down[j]), line(down[k - 1], down[k]))){ 110 pair<int, point>pr = line(up[i], down[j]) & line(down[k - 1], down[k]); 111 point p = pr.second; 112 ans = max(ans, p.x); 113 } 114 } 115 116 for(k = 0; k < n; k++){ 117 if(seg_inter_line(line(down[i], up[j]), line(up[k], down[k])) == 0){ 118 break; 119 } 120 } 121 if(k >= n){ 122 flag = true; 123 break; 124 } 125 if(k > max(i, j)){ 126 if(seg_inter_line(line(down[i], up[j]), line(up[k - 1], up[k]))){ 127 pair<int, point>pr = line(down[i], up[j]) & line(up[k - 1], up[k]); 128 point p = pr.second; 129 ans = max(ans, p.x); 130 } 131 if(seg_inter_line(line(down[i], up[j]), line(down[k - 1], down[k]))){ 132 pair<int, point>pr = line(down[i], up[j]) & line(down[k - 1], down[k]); 133 point p = pr.second; 134 ans = max(ans, p.x); 135 } 136 } 137 } 138 if(flag){ 139 break; 140 } 141 } 142 //cout<<ans<<endl; 143 if(flag){ 144 printf("Through all the pipe.\n"); 145 } 146 else{ 147 printf("%.2f\n", ans); 148 } 149 } 150 return 0; 151 }
标签:ini nes written star search pes code == oge
原文地址:https://www.cnblogs.com/wyboooo/p/9737596.html