Problem J. Distance to Work

Input file: standard input
Output file: standard output
Time limit: 2 seconds
Memory limit: 256 mebibytes

链接:https://www.nowcoder.com/acm/contest/141/J
来源:牛客网

Eddy has graduated from college. Currently, he is finding his future job and a place to live. Since Eddy is currently living in Tien-long country, he wants to choose a place inside Tien-long country to live. Surprisingly, Tien-long country can be represented as a simple polygon on 2D-plane. More surprisingly, Eddy can choose any place inside Tien-long country to live. The most important thing Eddy concerns is the distance from his place to the working place. He wants to live neither too close nor too far to the working place. The more specific definition of “close” and “far” is related to working place.

Eddy has M choices to work in the future. For each working place, it can be represented as a point on 2D-plane. And, for each working place, Eddy has two magic parameters P and Q such that if Eddy is going to work in this place, he will choose a place to live which is closer to the working place than portion of all possible living place choices.

Now, Eddy is wondering that for each working place, how far will he lives to the working place. Since Eddy is now busy on deciding where to work on, you come to help him calculate the answers.

For example, if the coordinates of points of Tien-long country is (0,0), (2,0), (2, 2), (0, 2) in counter-clockwise order. And, one possible working place is at (1,1) and P=1, Q=2. Then, Eddy should choose a place to live which is closer to (1, 1) than half of the choices. The distance from the place Eddy will live to the working place will be about 0.7978845608.

Input

The first line contains one positive integer N indicating the number of points of the polygon representing Tien-long country.
Each of following N lines contains two space-separated integer (xi, yi) indicating the coordinate of i-th points. These points is given in clockwise or counter-clockwise order and form the polygon.
Following line contains one positive integer M indicating the number of possible working place Eddy can choose from.
Each of following M lines contains four space-separated integer xj, yj, P, Q, where (xj, yj) indicating the j-th working place is at (xj, yj) and magic parameters is P and Q.

3 ≤ N ≤ 200
1 ≤ M ≤ 200
1 ≤ P < Q ≤ 200
|xi|, |yi|, |xj|, |yj| ≤ 1000
It’s guaranteed that the given points form a simple polygon.

Output

Output M lines. For i-th line, output one number indicating the distance from the place Eddy will live to the i-th working place.

Absolutely or relatively error within 10−610−610^{-6} will be considered correct.

Sample Input Sample Output
4
0 0
2 0
2 2
0 2
1
1 1 1 2
0.797884560809
3
0 0
1 0
2 1
2
0 0 1 2
1 1 1 3
1.040111537176
0.868735603376

计算几何,二分半径之后套套板子。

记录这题的原因是,比赛的时候eps调太大了没有过,调小到1e-8反而过了。—-玄学调参!

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include <iomanip>typedef long double ld;
using namespace std;const ld eps = 1e-8;
const ld PI = acos(-1.0);int dcmp(ld x){if( x > eps ) return 1;return x < -eps ? -1 : 0;
}struct Point{ld x,y;Point(){x = y = 0;}Point(ld a,ld b){x = a;y = b;}inline void input(){scanf("%Lf%Lf",&x,&y);}inline Point operator-(const Point &b)const{return Point(x - b.x,y - b.y);}inline Point operator+(const Point &b)const{return Point(x + b.x,y + b.y);}inline Point operator*(const ld &b)const{return Point(x * b,y * b);}inline Point operator/(const ld &b)const{return Point(x / b,y / b);}inline ld dot(const Point &b)const{return x * b.x + y * b.y;}inline ld cross(const Point &b,const Point &c)const{return (b.x - x) * (c.y - y) - (c.x - x) * (b.y - y);}inline ld Dis(const Point &b)const{return sqrt((*this-b).dot(*this-b));}inline bool InLine(const Point &b,const Point &c)const{ return !dcmp(cross(b,c));}inline bool OnSeg(const Point &b,const Point &c)const{  return InLine(b,c) && (*this - c).dot(*this - b) < eps;}
};inline ld min(ld a,ld b){return a < b ? a : b;
}
inline ld max(ld a,ld b){return a > b ? a : b;
}
inline ld Sqr(ld x){return x * x;
}
inline ld Sqr(const Point &p){return p.dot(p);
}Point LineCross(const Point &a,const Point &b,const Point &c,const Point &d){ld u = a.cross(b,c) , v = b.cross(a,d);return Point((c.x * v + d.x * u) / (u + v) , (c.y * v + d.y * u) / (u + v));
} ld LineCrossCircle(const Point &a,const Point &b,const Point &r,ld R,Point &p1,Point & p2){Point fp = LineCross(r , Point(r.x+a.y-b.y , r.y+b.x-a.x) , a , b);ld rtol = r.Dis(fp);ld rtos = fp.OnSeg(a , b) ? rtol : min(r.Dis(a) , r.Dis(b));ld atob = a.Dis(b);ld fptoe = sqrt(R * R - rtol * rtol) / atob;if( rtos > R - eps ) return rtos;p1 = fp + (a - b) * fptoe;p2 = fp + (b - a) * fptoe;return rtos;
}ld SectorArea(const Point &r,const Point &a,const Point &b,ld R){ ld A2 = Sqr(r - a) , B2 = Sqr(r - b) , C2 = Sqr(a - b);return R * R * acos( (A2 + B2 - C2) * 0.5 / sqrt(A2) / sqrt(B2)) * 0.5;
}ld TACIA(const Point &r,const Point &a,const Point &b,ld R){ld adis = r.Dis(a) , bdis = r.Dis(b);if( adis < R + eps && bdis < R + eps )return r.cross(a , b) * 0.5;Point ta , tb;if( r.InLine(a,b) ) return 0.0;ld rtos = LineCrossCircle(a, b, r, R, ta, tb);if( rtos > R - eps ) return SectorArea(r, a, b, R);if( adis < R + eps )return r.cross(a, tb) * 0.5 + SectorArea(r, tb, b, R);if( bdis < R + eps )return r.cross(ta, b) * 0.5 + SectorArea(r, a, ta, R);return r.cross(ta, tb) * 0.5 + SectorArea(r, tb, b, R) + SectorArea(r, a, ta, R);
}const int MAXN  = 505;
Point p[MAXN];ld SPICA(int n,Point r,ld R){int i;ld ret = 0 , if_clock_t;for( i = 0 ; i < n ; ++i ){if_clock_t = dcmp(r.cross(p[i], p[(i + 1) % n]));if( if_clock_t < 0 )ret -= TACIA(r, p[(i + 1) % n], p[i], R);else ret += TACIA(r, p[i], p[(i + 1) % n], R);}return fabs(ret);
}int main(){int n,i;Point sum=Point(0,0);scanf("%d",&n);for( i = 0 ; i < n ; ++i ) {p[i].input();sum=sum+p[i];}sum=sum/n;ld tot=0;for (i=0;i<n-1;i++) {tot+=sum.cross(p[i],p[i+1]);}tot+=sum.cross(p[n-1],p[0]);tot=fabs(tot/2.0L);int m;scanf("%d",&m);while (m--) {Point circle;circle.input(); ld P,Q;cin >> P >> Q;ld R;ld l,r,mid;l=0;r=1500000;while (fabs(l-r)>eps) {mid=R=(l+r)/2.0L;ld size=SPICA(n,circle,R);if (size*Q>(Q-P)*tot) r=mid; else l=mid;}cout << setiosflags(ios::fixed) << setprecision(12);cout << (l+r)/2.0L << endl;}return 0;
}

牛客网暑期ACM多校训练营(第三场) J.Distance to Work 计算几何相关推荐

  1. 牛客网暑期ACM多校训练营(第三场): E. Sort String(KMP)

    链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...

  2. 牛客网暑期ACM多校训练营(第三场): C. Shuffle Cards(splay)

    链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ...

  3. 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)

    题目链接: https://www.nowcoder.com/acm/contest/140/J 思路: 都写在代码注释里了,非常好懂.. for_each函数可以去看一下,遍历起vector数组比较 ...

  4. 牛客网暑期ACM多校训练营(第三场): A. Ternary String(欧拉降幂+递推)

    题目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a t ...

  5. 牛客网暑期ACM多校训练营(第三场): C. Chiaki Sequence Reloaded(数位DP)

    题目描述 Chiaki is interested in an infinite sequence a1, a2, a3, ..., which defined as follows: Chiaki ...

  6. 牛客网暑期ACM多校训练营(第九场)

    牛客网暑期ACM多校训练营(第九场) A. Circulant Matrix 做法:看到下标 \(xor\) 这种情况就想 \(FWT\),可是半天没思路,于是放弃了..其实这个 \(n\) 疯狂暗示 ...

  7. 牛客网暑期ACM多校训练营(第一场)

    牛客网暑期ACM多校训练营(第一场) A. Monotonic Matrix 考虑0和1的分界线,1和2的分界线,发现问题可以转化为两条不互相穿过的路径的方案数(可重叠),题解的做法就是把一条路径斜着 ...

  8. 牛客网暑期ACM多校训练营(第二场): H. travel(树形线头DP)

    链接:https://ac.nowcoder.com/acm/contest/140/H 来源:牛客网 题目描述 White Cloud has a tree with n nodes.The roo ...

  9. 牛客网暑期ACM多校训练营(第二场)A .run

    链接:https://www.nowcoder.com/acm/contest/140/A 来源:牛客网 题目描述 White Cloud is exercising in the playgroun ...

  10. 牛客网暑期ACM多校训练营(第一场) J (莫队算法)

    题目链接:https://www.nowcoder.com/acm/contest/139/J 题目大意:给一个序列,进行q次查询,问1~l和r~n中有多少个不同的数字 题目思路:之前只是听说过莫队算 ...

最新文章

  1. 使用apidoc文档神器,快速生成api文档
  2. copy构造函数使用深copy
  3. mysql数据对比同步_跨数据库mysql语句同步数据和对比运算
  4. Codeforces Round #657 (Div. 2)
  5. IDEA 配置Maven项目
  6. 零百1.8秒的电动车竟然是威马... | 云逛成都车展
  7. java 正则表达式 table_JavaEdge/Java/Java中正则表达式.md at master · VegTableBird/JavaEdge · GitHub...
  8. Redis基础(十二)——缓存读写策略
  9. 洛谷P3152 正整数序列
  10. rails--bcrypt对密码加密
  11. vecm模型怎么写系数_VAR模型与向量VECM模型(7)
  12. 中国“秃”如其来的头发经济
  13. python上方菜单栏不见了如何恢复_word菜单栏不见了,如何恢复
  14. Anomalies,Factors,andMultiFactorModels
  15. 异步处理需要消息补偿闭环
  16. 程序员成长之旅——C语言三子棋
  17. 数据分析模型篇—PEST分析
  18. 中科院计算所培训中心2017年第二季度课程安排
  19. 全息干涉图补零尺寸与三种重构方法重建像间的关系研究
  20. 华为IOT,与开发者共建物联网生态

热门文章

  1. 成长,从你发现自己写的代码很LOW开始
  2. 2.1.1队列——雏形(初始版本)
  3. 蓝牙电话之PBAP协议分析
  4. UE4遇到的各种奇葩问题
  5. 浏览器关闭垃圾的百度热点推荐------简洁就是美
  6. Compose基础-SideEffect(二)
  7. Maven(六)Maven传递性和依赖性
  8. Conjugate function and Fenchel’s duality theorem
  9. 【092】韦达定理在一元n次方程中的推广
  10. 发现了吗?西部世界III在Broadway上的广告