题意:某两个人 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. 【校招面试 之 C/C++】第33题 C++ 11新特性(四)之STL容器

    C++ 11新增array.forward_list(单链表).unordered_set.unordered_map集中容器.

  2. Django 改变xadmin后台英文为中文

    1.标题 setting.py文件: LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' 修改: LANGUAGE_CODE = 'zh-Hans' TIME_ZONE ...

  3. 定时器修改button标题闪烁

    在做一个项目时,用到UIButton来设置接收短信验证码的倒计时,但是用NSTimer来设置标题会出现连续闪烁的问题. 经过测试发现了一下内容,如果只是单独的设置button的titleLabel的内 ...

  4. springmvc+logback项目的日志搭建

    一.重写HTMLLayout 两个自定义类:LzhHTMLLayoutBase和LzhHTMLLayout LzhHTMLLayoutBase代码如下: package top.liaozhengha ...

  5. go语言使用go-sciter创建桌面应用(七) view对象常用方法,文件选择,窗口弹出,请求

    view对象的详细文档请看: https://sciter.com/docs/content/sciter/View.htm demo9.html代码如下: <!DOCTYPE html> ...

  6. 整站变灰CSS代码

    * { filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); -webkit-filter: grayscale(100% ...

  7. angular2.0学习笔记3.了解angular2.0项目结构

    1.我们应用的代码都位于src文件中,包括所有的组件.模板.样式.图片以及我们的应用所需的任何东西都在这个文件来里. 2.src这个文件夹之外的文件都是为构建应用提供支持用的. src文件夹及用途说明 ...

  8. PAT 1016 部分A+B(15)(C++&JAVA&&Python)

    1016 部分A+B(15 分) 正整数 A 的"D​A​​(为 1 位整数)部分"定义为由 A 中所有 D​A​​ 组成的新整数 P​A​​.例如:给定 A=3862767,D​ ...

  9. [Centos] ERROR: Could not find useradd in chroot, maybe the install failed?

    [mockbuild at localhost ~]$ mock -r centos-5-x86_64-testdev.cfg initinitcleanprepThis may take a whi ...

  10. PC 上的 LVM 灾难修复

    LVM 介绍 LVM 简介 LVM 是逻辑盘卷管理(Logical Volume Manager)的简称,最早是 IBM 为 AIX 研发的存储管理机制.LVM 通过在硬盘和分区之间建立一个逻辑层,可 ...