UVa 11722 (概率 数形结合) Joining with Friend
高中也做个这种类似的题目,概率空间是[t1, t2] × [s1, s2]的矩形,设x、y分别代表两辆列车到达的时间,则两人相遇的条件就是|x - y| <= w
从图形上看就是矩形夹在两条平行线之间的部分。
因为情况众多,一个一个分类很麻烦,而且可能有漏掉情况,所以就用计算几何的办法求了个凸多边形,多边形 与 矩形面积之比就是概率。
代码有点挫,将就看,=_=||
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std; struct Point
{
double x, y;
Point(double x=, double y=):x(x), y(y) {}
};
typedef Point Vector;
typedef vector<Point> Polygon; Point operator + (const Point& A, const Point& B)
{ return Point(A.x+B.x, A.y+B.y); } Vector operator - (const Point& A, const Point& B)
{ return Point(A.x-B.x, A.y-B.y); } Vector operator * (const Vector& A, double p)
{ return Vector(A.x*p, A.y*p); } double Cross(const Vector& A, const Vector& B)
{ return A.x * B.y - A.y * B.x; } double PolygonArea(const Polygon& p)
{//求多边形面积
int n = p.size();
double area = ;
for(int i = ; i < n-; i++)
area += Cross(p[i]-p[], p[i+]-p[]);
return area/;
} Point Intersection(Point P, Vector v, Point Q, Vector w)
{//求两直线交点
Vector u = P-Q;
double t = Cross(w, u) / Cross(v, w);
return P+v*t;
} const double sqrt2 = sqrt(2.0);
const Vector v1(, ), v2(, ), v3(, );
double t1, t2, s1, s2, w;
Polygon poly; bool in(const Point& p)
{ return p.x >= t1 && p.x <= t2 && p.y >= s1 && p.y <= s2; } void check(Point p)
{
if(in(p) && fabs(p.x-p.y) <= w)//该点要在矩形内两平行线之间
poly.push_back(Point(p.x, p.y));
} int main()
{
//freopen("in.txt", "r", stdin); int T;
scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
poly.clear();
scanf("%lf%lf%lf%lf%lf", &t1, &t2, &s1, &s2, &w);
Point p(t1, s2), p1(, w), p2(, -w);
check(p);
p = Intersection(Point(t1, ), v2, p1, v3); check(p);
p = Intersection(Point(t1, ), v2, p2, v3); check(p);
p = Point(t1, s1); check(p);
p = Intersection(Point(, s1), v1, p1, v3); check(p);
p = Intersection(Point(, s1), v1, p2, v3); check(p);
p = Point(t2, s1); check(p);
p = Intersection(Point(t2, ), v2, p2, v3); check(p);
p = Intersection(Point(t2, ), v2, p1, v3); check(p);
p = Point(t2, s2); check(p);
p = Intersection(Point(, s2), v1, p2, v3); check(p);
p = Intersection(Point(, s2), v1, p1, v3); check(p);
//for(int i = 0; i < poly.size(); i++) printf("%.3f %.3f\n", poly[i].x, poly[i].y);
double A = PolygonArea(poly);
double B = (t2-t1) * (s2-s1);
double ans = A / B;
printf("Case #%d: %.8f\n", kase, ans);
} return ;
}
代码君
UVa 11722 (概率 数形结合) Joining with Friend的更多相关文章
- 紫书 例题8-9 UVa 1451 (数形结合)
这道题用了数形结合, 真的牛逼, 完全想到不到还可以这么做 因为题目求的是平均值, 是总数除以个数, 这个时候就可以联系 到斜率, 也就是说转化为给你一堆点, 让你求两点之间的最大斜率 要做两个处理 ...
- uva 11722 - Joining with Friend(概率)
题目连接:uva 11722 - Joining with Friend 题目大意:你和朋友乘火车,而且都会路过A市.给定两人可能到达A市的时段,火车会停w.问说两人能够见面的概率. 解题思路:y = ...
- UVa 1451 (数形结合 单调栈) Average
题意: 给出一个01串,选一个长度至少为L的连续子串,使得串中数字的平均值最大. 分析: 能把这道题想到用数形结合,用斜率表示平均值,我觉得这个想法太“天马行空”了 首先预处理子串的前缀和sum,如果 ...
- UVA 1451 Average平均值 (数形结合,斜率优化)
摘要:数形结合,斜率优化,单调队列. 题意:求一个长度为n的01串的子串,子串长度至少为L,平均值应该尽量大,多个满足条件取长度最短,还有多个的话,取起点最靠左. 求出前缀和S[i],令点Pi表示(i ...
- HDU3045 Picnic Cows (斜率DP优化)(数形结合)
转自PomeCat: "DP的斜率优化--对不必要的状态量进行抛弃,对不优的状态量进行搁置,使得在常数时间内找到最优解成为可能.斜率优化依靠的是数形结合的思想,通过将每个阶段和状态的答案反映 ...
- 【做题】TCSRM591 Div1 500 PyramidSequences——数形结合&思维
题意:定义高度为\(x\)的金字塔数列为周期为\(2x-2\)的无限数列.它的每一个周期都是形如\(1,2,...,x-1,x,x-1,...,2\)的形式.记高度为\(x\)的金字塔数列第\(i\) ...
- poj 1430 Binary Stirling Number 求斯特林数奇偶性 数形结合| 斯特林数奇偶性与组合数的关系+lucas定理 好题
题目大意 求子集斯特林数\(\left\{\begin{matrix}n\\m\end{matrix}\right\}\%2\) 方法1 数形结合 推荐一篇超棒的博客by Sdchr 就是根据斯特林的 ...
- UVa 11722 Joining with Friend (几何概率 + 分类讨论)
题意:某两个人 A,B 要在一个地点见面,然后 A 到地点的时间区间是 [t1, t2],B 到地点的时间区间是 [s1, s2],他们出现的在这两个区间的每个时刻概率是相同的,并且他们约定一个到了地 ...
- uva 11722 Joining with Friend
https://vjudge.net/problem/UVA-11722 题意:你和朋友都要乘坐火车,并且都会途径A城市.你们很想会面,但是你们到达这个城市的准确时刻都无法确定.你会在时间区间[t1, ...
随机推荐
- failed creating the Direct3d device--debug
D3DDEVTYPE_REF 使用REF设备,用软件模拟Direct3D API 照理说是为了让电脑能跑本机不能硬件执行的渲染命令的 但我 pDeviceSettings->d3d9.Devic ...
- 【C# 反射泛型】
C# 反射泛型 摘自:http://www.itwis.com/html/net/c/20110411/10175.html C#泛型反射和普通反射的区别,泛型反射和普通反射的区别就是泛型参数的处理上 ...
- POJ 1928
#include <iostream> #include <algorithm> #define MAXN 3000 using namespace std; struct n ...
- jQuery经典面试题及答案精选[转载]
问题:jQuery的美元符号$有什么作用? 回答:其实美元符号$只是”jQuery”的别名,它是jQuery的选择器,如下代码: $(document).ready(function(){ }); 当 ...
- CSS 的命名和书写
CSS书写顺序 1.位置属性(position, top, right, z-index, display, float等) 2.大小(width, height, padding, margin) ...
- crontab 指定执行用户
linux下可以通过配置crontab来定时执行任务,执行体可以是一条系统命令或自己写的一个脚本,同时可以指派用户来执行.配置crontab有两种方法.方法1.使用crontab命令,例如添加一个新的 ...
- hdu 3923 Invoker
完全是套用polya模版…… ;}
- TortoiseSVN文件夹及文件图标不显示解决方法(转发)
地址:http://blog.csdn.net/lishehe/article/details/8257545 由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能 ...
- 解决virtualbox 虚拟机不能ping通win7
凭经验猜测是由于防火墙引起的,关闭防火墙再ping,果然可行.google说这是由于“win7 防火墙默认的禁ping策略”引起的.但是关闭防火墙很不安全,可以按照以下步骤为防火墙添加入站规则来解决问 ...
- 李洪强iOS开发之OC[012] -类的声明实现小结
// // main.m // 11 - 内容总结 // // Created by vic fan on 16/7/9. // Copyright © 2016年 李洪强. All righ ...