10212:Green Light

问题 G: Green Light
时间限制: 1 Sec 内存限制: 128 MB Special Judge
题目描述
Sarah is cycling to work. On her way there, she encounters the same traffic light every day. Before she reaches the lights, she alternates between using social media on her mobile device and glancing at the traffic lights, observ-
ing if they are green, yellow or red at that time. From experience, she knows that the lights have a fixed green-yellow-red cycle, and how long each stage lasts. So if the light goes from red to green at time T, she knows it will
stay green until (but not including) T +Tg, then go yellow until (but not including) T +Tg +Ty and finally stay red until (but not including) T +Tg +Ty +Tr, at which point it will turn green again. However, she does not know T, the time at which the traffic light cycle starts. based on her observations, she can deduce what values of T are (im)possible. Assuming that each possible value of T that is consistent with her observations is equally likely, can you compute the probability that the lights will be green at a certain time?

输入
• The first line contains three positive integers Tg Ty Tr, corresponding to the duration (in seconds) for which the lights stay green, yellow, and red (0 < Tg, Ty, Tr ≤ 108).
• The second line contains a single positive integer n, the number of times Sarah looked at the lights (3 ≤ n < 1000).
• Each of the next n lines contains one integer 0 ≤ t ≤ 109 followed by a color c: the time (in seconds) of the observation and color of the lights at that moment. Sarah did see the lights being each color (green, yellow and red) at least once.
• The last line contains an integer 0 ≤ tq ≤ 109 and a color cq. These specify the question asked: What is the probability of the lights being of color cq at time tq?

输出
0 ≤ p ≤ 1, the probability of the lights being color cq at time tq. Your answer will be considered correct when it differs from the actual answer by at most 10−3 either absolutely or relatively.

样例输入
4 4 4
3
2 green
18 yellow
34 red
5 green
样例输出
0.25

题意

有一个红绿灯,你知道绿、黄、红持续时间分别为Tg,Ty,Tr,但是不知道初始绿灯亮的时间T,接下来有n次观察,观察到第t秒的时候红绿灯的颜色,最后一行是一次询问,询问在第t秒颜色为c的概率

做法

对于每次观察,合法的初始时间T的范围是一系列连续的区间,在第一次观察时,选择一段区间保存,之后拿每一段区间与当前的区间集合求交集,最终询问的值即为: 最终区间总长度/进行最后一次合并前的区间总长度

代码

#include <bits/stdc++.h>
#define rint register int
typedef long long ll;
using namespace std;
const int N=100000+5;
const int inf=0x3f3f3f3f;
struct data
{
    int l,r;
};
vector<data>vec;
int mod;
void mge(int ll,int rr)
{
    if(vec.size()==0)
    {
        vec.push_back({ll,rr});
        return;
    }
    vector<data>tmp;
    for(int i=-3*mod;i<=3*mod;i+=mod)
    {
        int nl=ll+i;
        int nr=rr+i;
        for(auto it:vec)
        {
            int tl=max(it.l,nl);
            int tr=min(it.r,nr);
            if(tr>tl) tmp.push_back({tl,tr});
        }
    }
    if(tmp.size()==0)
    {
        cout<<0<<endl;
        exit(0);
    }
    vec=tmp;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in","r",stdin);
#endif
 
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    int tg,ty,tr;
    cin>>tg>>ty>>tr;
    mod=tg+ty+tr;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x;
        string s;
        cin>>x>>s;
        //cout<<x<<' '<<x%mod<<' '<<s<<' '<<vec.size()<<endl;
        //for(auto it:vec) cout<<it.l<<' '<<it.r<<' ';
        //cout<<endl;
        x%=mod;
        if(s=="green")
        {
            mge(x-tg,x);
        }
        else if(s=="yellow")
        {
            mge(x-tg-ty,x-tg);
        }
        else mge(x-tg-ty-tr,x-ty-tg);
    }
    //cout<<l<<' '<<r<<endl;
    int sum=0;
    for(auto it:vec)
    {
        sum+=it.r-it.l;
    }
 
    int x;
    string s;
    cin>>x>>s;
    x%=mod;
    //cout<<x-tg-ty-tr<<' '<<x-ty-tg<<endl;
    if(s=="green")
    {
        mge(x-tg,x);
    }
    else if(s=="yellow")
    {
        mge(x-tg-ty,x-tg);
    }
    else mge(x-tg-ty-tr,x-ty-tg);
 
    int sum1=0;
    for(auto it:vec)
    {
        sum1+=it.r-it.l;
    }
    cout<<(double)sum1/sum<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值