UVA 11722(概率+几何)

http://uva.onlinejudge.org/external/117/11722.pdf

Problem

Solution

求概率方法就是lrj的方法。怎么求面积比,又不要分太多情况讨论呢?首先,线段与直线相交怎么判断,直线与直线求交点怎么求,然后,怎么把四边形的面积转换为三个三角形面积相加,怎么判断两条直线之间没有夹四边形的情况?解决这些情况就行了,基本模板大白书都有。

My code

//Hello. I‘m Peter.
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define MAXN
#define N
#define M
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
const double eps=1e-9;
int dcmp(double x){
    if(fabs(x)<eps) return 0;
    else if(x<0) return -1;
    else return 1;
}
int t1,t2,s1,s2,w;
struct Point{
    double x,y;
    Point(){};
    Point(double xx,double yy){
        x=xx,y=yy;
    }
};
typedef Point Vector;
Vector operator +(const Vector a,const Vector b){
    return Vector(a.x+b.x,a.y+b.y);
}
Vector operator -(const Vector a,const Vector b){
    return Vector(a.x-b.x,a.y-b.y);
}
double operator *(const Vector a,const Vector b){
    return a.x*b.x+a.y*b.y;
}
double operator %(const Vector a,const Vector b){
    return a.x*b.y-a.y*b.x;
}
Vector operator*(const Vector a,const double b){
    return Vector(a.x*b,a.y*b);
}
Vector operator*(const double b,const Vector a){
    return Vector(a.x*b,a.y*b);
}
bool operator==(const Point a,const Point b){
    return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
Point Poi_LineInterLine(Point p,Vector v,Point q,Vector w){
    Vector pq=q-p;
    double t=(w%pq)/(w%v);
    return p+v*t;
}
double Square_Triangle(Point p1,Point p2,Point p3){
    Vector v=p2-p1,w=p3-p1;
    return 0.5*fabs(v%w);
}
struct Segment{
    Point p1,p2;
}seg[10];
struct Line{
    Point p;
    Vector v;
};
bool Line_Inter_Segment(Line l,Segment s){
    return dcmp(l.v%(s.p1-l.p))*dcmp(l.v%(s.p2-l.p))<=0;
}
void get_p(Line l,vector<Point>&res){
    res.clear();
    for(int i=1;i<=4;i++){
        if(!Line_Inter_Segment(l,seg[i])) continue;
        Point p1=Poi_LineInterLine(l.p,l.v,seg[i].p1,seg[i].p2-seg[i].p1);
        int len=(int)res.size();
        bool ok=true;
        for(int j=0;j<len;j++){
            if(p1==res[j]){
                ok=false;
                break;
            }
        }
        if(ok) res.push_back(p1);
    }
}
inline bool comp(const Point a,const Point b){
    if(dcmp(a.x-b.x)) return a.x<b.x;
    else return a.y<b.y;
}
int main(){
    int T=read();
    for(int kase=1;kase<=T;kase++){
        printf("Case #%d: ",kase);
        t1=read(),t2=read(),s1=read(),s2=read(),w=read();
        Point p1,p2,p3,p4;
        p1=Point(t1,s1);
        p2=Point(t2,s1);
        p3=Point(t2,s2);
        p4=Point(t1,s2);
        seg[1].p1=p1,seg[1].p2=p2;
        seg[2].p1=p2,seg[2].p2=p3;
        seg[3].p1=p3,seg[3].p2=p4;
        seg[4].p1=p4,seg[4].p2=p1;
        Line l1,l2;
        l1.p=Point(0,w);
        l1.v=Vector(1,1);
        l2.p=Point(0,-w);
        l2.v=Vector(1,1);
        vector<Point>res;

        double ansS=abs(t2-t1)*abs(s2-s1);
        int len;
        get_p(l1,res);
        len=(int)res.size();
        if(len>1){
            sort(res.begin(),res.end(),comp);
            ansS-=Square_Triangle(res[0],p1,p4);
            ansS-=Square_Triangle(res[0],res[1],p4);
            ansS-=Square_Triangle(res[1],p3,p4);
        }
        if(p2.y>=p2.x+w) ansS-=abs(t2-t1)*abs(s2-s1);

        get_p(l2,res);
        len=(int)res.size();
        if(len>1){
            sort(res.begin(),res.end(),comp);
            ansS-=Square_Triangle(res[0],p1,p2);
            ansS-=Square_Triangle(res[0],res[1],p2);
            ansS-=Square_Triangle(res[1],p2,p3);
        }
        if(p4.y<=p4.x-w) ansS-=abs(t2-t1)*abs(s2-s1);
        ansS/=(abs(t2-t1)*abs(s2-s1));
        printf("%.8f\n",ansS);
    }
    return 0;
}
时间: 2024-08-08 05:39:06

UVA 11722(概率+几何)的相关文章

UVa 11722 (概率 数形结合) Joining with Friend

高中也做个这种类似的题目,概率空间是[t1, t2] × [s1, s2]的矩形,设x.y分别代表两辆列车到达的时间,则两人相遇的条件就是|x - y| <= w 从图形上看就是矩形夹在两条平行线之间的部分. 因为情况众多,一个一个分类很麻烦,而且可能有漏掉情况,所以就用计算几何的办法求了个凸多边形,多边形 与 矩形面积之比就是概率. 代码有点挫,将就看,=_=|| 1 #include <cstdio> 2 #include <vector> 3 #include <

uva 11722 - Joining with Friend(概率)

题目连接:uva 11722 - Joining with Friend 题目大意:你和朋友乘火车,而且都会路过A市.给定两人可能到达A市的时段,火车会停w.问说两人能够见面的概率. 解题思路:y = x + w 和y = x - w在给定时间内围成的面积除以时间的总面积,就是求面积的时候要分情况处理. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int t

UVA 11971 - Polygon(概率+几何概型)

UVA 11971 - Polygon 题目链接 题意:给一条长为n的线段,要选k个点,分成k + 1段,问这k + 1段能组成k + 1边形的概率 思路:对于n边形而言,n - 1条边的和要大于另外那条边,然后先考虑3边和4边形的情况,根据公式在坐标系中画出来的图,总面积为x,而不满足的面积被分成几块,每块面积为x/2k,然后在观察发现一共是k + 1块,所以符合的面积为x?x?(k+1)/2k,这样一来除以总面积就得到了概率1?(k+1)/2k 代码: #include <cstdio>

uva 12723 概率dp

Dudu is a very starving possum. He currently stands in the first shelf of a fridge. This fridge iscomposed of N shelves, and each shelf has a number Qi (1 ≤ i ≤ N) of food. The top shelf, whereDudu is, is identified by the number 1, and the lowest is

集训第六周 数学概念与方法 UVA 11722 几何概型

---恢复内容开始--- http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31471 题意,两辆火车,分别会在[t1,t2],[s1,s2]的时间段停留在同一个站点w分钟,问两辆火车能够在这个站点相遇的概率. 思路,枚举每一种情况,把两辆火车的相交区间画出来,然后求都在这个区间的概率 #include"iostream" #include"cstdio" #include"cmath&

UVA - 11722 Joining with Friend (概率)

You are going from Dhaka to Chittagong by train and you came to know one of your old friends is going from city Chittagong to Sylhet. You also know that both the trains will have a stoppage at junction Akhaura at almost same time. You wanted to see y

UVA 11427 (概率DP+期望)

题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 题目大意:每晚打游戏.每晚中,赢一局概率p,最多玩n局,如果最后不能保证胜率大于p,则从此不玩.问打游戏的天数的期望. 解题思路: 首先分析每天晚上的. 设f[i][j]为前i天,已经赢j局的概率. 由全概率公式,那么当天晚上完蛋的概率q=f[n][0]+f[n][1]+.....f[n][终止条件]. 至于为什么从完蛋(输)的角度考虑,主要是由于n局的

UVA 11346 Probability (几何概型, 积分)

题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321">https://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321 题目大意:在A是一个点集 A = {(x, y) | x ∈[-a, a],y∈[-b, b]},求取出

UVa 11971 (概率) Polygon

题意: 有一根绳子,在上面随机选取k个切点,将其切成k+1段,求这些线段能够成k+1边形的概率. 分析: 要构成k+1边形,必须最长的线段小于其他k个线段之和才行. 紫书上给出了一种解法,但是感觉理解得不是太好,所以又去网上找了其他解法. 知乎上有人问过这个问题,而且给出了很多种严格的解法. 最后代码里将(1LL << i)写成(1 << i),这种细节应当注意. 1 #include <cstdio> 2 typedef long long ll; 3 4 ll gc