题意:某两个人 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 (几何概率 + 分类讨论)的更多相关文章

  1. UVA - 11722 Joining with Friend 几何概率

                            Joining with Friend You are going from Dhaka to Chittagong by train and you ...

  2. uva 11722 - Joining with Friend(概率)

    题目连接:uva 11722 - Joining with Friend 题目大意:你和朋友乘火车,而且都会路过A市.给定两人可能到达A市的时段,火车会停w.问说两人能够见面的概率. 解题思路:y = ...

  3. uva 11722 Joining with Friend

    https://vjudge.net/problem/UVA-11722 题意:你和朋友都要乘坐火车,并且都会途径A城市.你们很想会面,但是你们到达这个城市的准确时刻都无法确定.你会在时间区间[t1, ...

  4. Codeforces 460D Little Victor and Set --分类讨论+构造

    题意:从区间[L,R]中选取不多于k个数,使这些数异或和尽量小,输出最小异或和以及选取的那些数. 解法:分类讨论. 设选取k个数. 1. k=4的时候如果区间长度>=4且L是偶数,那么可以构造四 ...

  5. BZOJ-1067 降雨量 线段树+分类讨论

    这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...

  6. UVaLive 6862 Triples (数学+分类讨论)

    题意:给定一个n和m,问你x^j + y^j = z^j 的数量有多少个,其中0 <= x <= y <= z <= m, j = 2, 3, 4, ... n. 析:是一个数 ...

  7. 枚举(分类讨论):BZOJ 1177: [Apio2009]Oil

    1177: [Apio2009]Oil Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1477  Solved: 589[Submit] Descri ...

  8. Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array 分类讨论连续递推dp

    题意:给出一个 数列 和一个x 可以对数列一个连续的部分 每个数乘以x  问该序列可以达到的最大连续序列和是多少 思路: 不是所有区间题目都是线段树!!!!!! 这题其实是一个很简单的dp 使用的是分 ...

  9. 【cf789B】Masha and geometric depression(分类讨论/暴力)

    B. Masha and geometric depression 题意 在黑板上写数列,首项是b,公比是q,超过l时就停止不写.给定m个数,遇到后跳过不写.问一共写多少个数,如果无穷个输出inf. ...

随机推荐

  1. Xcode 去掉控制台无用打印信息

    1. 2.在Environment Variables增加一键值对 OS_ACTIVITY_MODE = disable 转自:https://blog.csdn.net/HelloWorld_198 ...

  2. TableViewCell去除选中效果

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableV ...

  3. 10-okHttp的同步与异步

    我的理解如下: 同步: 实时的在等待返回结果: 异步:可以不是同步执行的,放入到执行队列中. 所以建议:如果需要根绝请求的结构做些判断应当用 同步,异步可能由于时间先后出现问题. /*post异步请求 ...

  4. SI和DI

    si和di是8086CPU中和bx功能相近的寄存器,si和di不能够分成两个8位寄存器来使用. 用si和di实现将字符串"welcome to masm!"复制到它后面的数据区中. ...

  5. windows7 Cygwin 下安装 YouCompleteMe 插件

    原创文章,欢迎指正!转载请注明~ 从上周就开始想在cygwin上安装YouCompleteMe插件,按照GITHUB上的官方教程安装,由于自己的理解失误,一直搞不清是按照在windows上安装还是按照 ...

  6. 10.18JS日记

    1.JS的本质就是处理数据,数据来自后台的数据库,所以变量起到了临时存储的作用, ES制定了js的数据类型 2.数据类型有哪些? (1)字符串 String (2)数字  Number (3)布尔 B ...

  7. 同一个线程多次调用start()会出现的问题

    结果: 线程首先会运行一次,然后抛出java.lang.IllegalThreadStateException异常. 根据控制台的异常信息,定位到Thread.java的第708行,也就start() ...

  8. (转帖)CentOS最常用命令及快捷键整理

    原文:http://www.centoscn.com/CentOS/help/2014/0306/2493.html 最近开始学Linux,在VMware Player中安装了CentOS 6.4.为 ...

  9. 可读性很强的C语言的函数指针定义

    通常C/C++程序里面要用到大量的指针,其语法非常难以阅读.比如下面的vp指针类型: #include <iostream> using namespace std; typedef vo ...

  10. swift 判断真机还是模拟器

    if Platform.isSimulator { // Do one thing print("isSimulator") } else { } struct Platform ...