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. ...
随机推荐
- stark组件之注册【模仿Django的admin】
一.先看下django的admin是如何实现注册功能 首先导入admin这个对象和我们的model模块 from django.contrib import admin # Register your ...
- 利用sshtunnel实现跳板机的效果[嵌套ssh实现]
with SSHTunnelForwarder ( ssh_address_or_host = (conf.server_ip,conf.server_port), ssh_username=conf ...
- echarts柱状图Demo
echarts链接:http://gallery.echartsjs.com/editor.html?c=xB1Sfo5JbX 代码: var xData = ['a', 'b', 'c', 'd', ...
- redis集群 与spring-data-redis 集成
所遇到的坑:必须使用如下的jedis 版本与spring-data-redis 版本,才能够达到集群效果 .1.7版本以前是不支持集群的 <dependency> <groupId& ...
- React-router4 笔记
第一次看React-router发现根据阮一峰老师教程老报错,,完全一样的代码还是报错,,然而到最后才发现自己安装的版本太高了! 由于菜鸟比较新,npm install react-router 这样 ...
- opencv 学习总结 方法总结
师者传道受业解惑也,图片识别是门学科,需要师者传教,才会较快解开谜团,解开困惑,没人引导,要学会图片识别,有点难度,因为其中的做法超出自己的想象范围. 大家都知道,在超出想象范围,或者从未想到的方式, ...
- Android.DebugOnDevices
真机调试Android http://www.cnblogs.com/junqilian/archive/2012/11/08/2760734.html
- jQuery的节点选择
jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(&q ...
- spec文件写作规范
spec文件写作规范 2008-09-28 11:52:17 分类: LINUX 1.The RPM system assumes five RPM directories BUILD:rpmbuil ...
- Luogu 2173 [ZJOI2012]网络 - LCT
Solution $LCT$ 直接上$QuQ$ 注意$cut$ 完 需要 $d[u + c * N]--$ 再 $link$, 不然会输出Error 1的哦 Code #include<cs ...