UVa 11722 Joining with Friend (几何概率 + 分类讨论)
题意:某两个人 A,B 要在一个地点见面,然后 A 到地点的时间区间是 [t1, t2],B 到地点的时间区间是 [s1, s2],他们出现的在这两个区间的每个时刻概率是相同的,并且他们约定一个到了地点,等待另一个人 w 分钟,问你他们可能见面的概率是多少。
析:就是一个高中的一个几何概率的典型例题,他们相遇的条件是 |s -t | <= w,然后在画出二维图,再求面积即可,现在问题的情况是有好多种,所以需要我们进行分类讨论,答案其实就是下面那条直线上面的在矩形内的面积减去上面那条直线上面的在矩形的面积,所以只要求出这个两个面积即可。首先分类讨论,先把下面那条直线上面的在矩形内的面积放到一个函数里,然后再分类讨论上面即可,具体看代码,可以画画图,理解一下。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const int maxm = 100 + 2;
const LL mod = 100000000;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} inline int up_line(int t, int w){ return t + w; }
inline int up_line_x(int s, int w){ return s - w; }
inline int down_line(int t, int w){ return t - w; }
inline int down_line_x(int s, int w){ return s + w; } double down_line_area(int s1, int s2, int t1, int t2, int w){
double denominator = (t2 - t1) * (s2 - s1);
if(down_line(t1, w) >= s2) return 0.;
if(down_line(t2, w) <= s1) return denominator;
if(down_line(t1, w) <= s1 && down_line(t2, w) <= s2){ // the down line is under the diagonal of the rectangle
int x = t2 - down_line_x(s1, w);
int y = down_line(t2, w) - s1;
return denominator - x * y / 2.;
}
if(down_line(t1, w) >= s1 && down_line(t2, w) >= s2){ // the down line is on the diagonal of the rectangle
int x = down_line_x(s2, w) - t1;
int y = s2 - down_line(t1, w);
return x * y / 2.;
}
// the down line intersect with diagonal of the rectangle
if(down_line(t1, w) <= s1 && down_line(t2, w) >= s2){
int x = down_line_x(s1, w) - t1 + down_line_x(s2, w) - t1;
int y = s2 - s1;
return x * y / 2.;
}
int x = t2 - t1;
int y = s2 - down_line(t1, w) + s2 - down_line(t2, w);
return x * y / 2.;
} int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
int s1, s2, t1, t2, w;
scanf("%d %d %d %d %d", &t1, &t2, &s1, &s2, &w);
double denominator = (t2 - t1) * (s2 - s1);
printf("Case #%d: ", kase);
if(up_line(t2, w) <= s1) printf("0\n"); // the up line under the rectangle
else if(up_line(t1, w) >= s2) // the up line on the rectangle
printf("%.6f\n", down_line_area(s1, s2, t1, t2, w) / denominator);
else if(up_line(t1, w) <= s1 && up_line(t2, w) <= s2){ // the up line is under the diagonal of the rectangle
int x = t2 - up_line_x(s1, w);
int y = up_line(t2, w) - s1;
printf("%.6f\n", (down_line_area(s1, s2, t1, t2, w) - denominator + x * y / 2.) / denominator);
}
else if(up_line(t1, w) >= s1 && up_line(t2, w) >= s2){ // the up line is on the diagonal of the rectangle
int x = up_line_x(s2, w) - t1;
int y = s2 - up_line(t1, w);
printf("%.6f\n", (down_line_area(s1, s2, t1, t2, w) - x * y / 2.) / denominator);
}
// the up line intersect with diagonal of the rectangle
else if(up_line(t1, w) <= s1 && up_line(t2, w) >= s2){
int x = up_line_x(s1, w) - t1 + up_line_x(s2, w) - t1;
int y = s2 - s1;
printf("%.6f\n", (down_line_area(s1, s2, t1, t2, w) - x * y / 2.) / denominator);
}
else{
int x = t2 - t1;
int y = s2 - up_line(t1, w) + s2 - up_line(t2, w);
printf("%.6f\n", (down_line_area(s1, s2, t1, t2, w) - x * y / 2.) / denominator);
}
}
return 0;
}
UVa 11722 Joining with Friend (几何概率 + 分类讨论)的更多相关文章
- UVA - 11722 Joining with Friend 几何概率
Joining with Friend You are going from Dhaka to Chittagong by train and you ...
- uva 11722 - Joining with Friend(概率)
题目连接:uva 11722 - Joining with Friend 题目大意:你和朋友乘火车,而且都会路过A市.给定两人可能到达A市的时段,火车会停w.问说两人能够见面的概率. 解题思路:y = ...
- uva 11722 Joining with Friend
https://vjudge.net/problem/UVA-11722 题意:你和朋友都要乘坐火车,并且都会途径A城市.你们很想会面,但是你们到达这个城市的准确时刻都无法确定.你会在时间区间[t1, ...
- Codeforces 460D Little Victor and Set --分类讨论+构造
题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...
- BZOJ-1067 降雨量 线段树+分类讨论
这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...
- UVaLive 6862 Triples (数学+分类讨论)
题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...
- 枚举(分类讨论):BZOJ 1177: [Apio2009]Oil
1177: [Apio2009]Oil Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1477 Solved: 589[Submit] Descri ...
- Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp
题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x 问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ...
- 【cf789B】Masha and geometric depression(分类讨论/暴力)
B. Masha and geometric depression 题意 在黑板上写数列,首项是b,公比是q,超过l时就停止不写.给定m个数,遇到后跳过不写.问一共写多少个数,如果无穷个输出inf. ...
随机推荐
- 记录下ABAP开发的一些东西(T-code居多)Updated to markdown
几个TCODE se38 开发program,report: sa38 只运行program se37 开发function: se11/se16 管理数据字典/数据表: ko03 Internal ...
- XML 解析技术
xml 解析方式有两种: dom 解析和 sax 解析: 针对着两种解析方式,有三种解析器: sun公司的 jaxp dom4j 组织的 dom4j jdom 组织的 jdom dom 解析XML : ...
- java_3选择与循环
1.三种执行顺序(流程控制语句) 在Java中,有三种执行结构,第一种:顺序结构.第二种:循环结构.第三种:选择结构. 2.顺序结构 自上而下,顺序执行. 3.循环结构 (1)while语句 初始化表 ...
- android 下拉刷新框架PullToRefreshScrollView(com.handmark.pulltorefresh)
很简单,实现OnRefreshListener这个监听器. mPullRefreshScrollView .setOnRefreshListener(new OnRefreshListener< ...
- org.springframework.web.util.Log4jWebConfigurer
org.springframework.web.util.Log4jWebConfigurer @Deprecated Deprecated. as of Spring 4.2.1, in favor ...
- springboot添加fluent日志记录
istio默认会进行日志的记录,但是仅仅记录到服务.以及服务之间调用的信息,不记录业务日志. 如: 所以需要添加业务日志记录. 1.引入依赖 <dependency> <gr ...
- 右手坐标系下LookAt视图矩阵的推导
基本知识 右手坐标系 右手手掌弯曲,手指方向由正X轴指向正Y轴,如果这时Z轴正方向与大拇指方向保持一致,坐标系为右手坐标系,否则为左手坐标系. 向量叉乘的方向 向量(1,0,0)与向量(0,1,0)叉 ...
- L1-025 正整数A+B(15)(思路+测试点分析)
L1-025 正整数A+B(15 分) 题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B, ...
- pop回到之前的某一个页面
循环遍历 - (void)backHome:(UIButton *)button { self.navigationController.navigationBarHidden = NO; 4 Cas ...
- python爬虫_简单使用百度OCR解析验证码
百度技术文档 首先要注册百度云账号: 在首页,找到图像识别,创建应用,选择相应的功能,创建 安装接口模块: pip install baidu-aip 简单识别一: 简单图形验证码: 图片: from ...