• codeforces_724C_Ray Tracing


    C. Ray Tracing
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.

    Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.

    At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.

    When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.

    For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print  - 1 for such sensors.

    Input

    The first line of the input contains three integers nm and k (2 ≤ n, m ≤ 100 000, 1 ≤ k ≤ 100 000) — lengths of the room's walls and the number of sensors.

    Each of the following k lines contains two integers xi and yi (1 ≤ xi ≤ n - 1, 1 ≤ yi ≤ m - 1) — coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.

    Output

    Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or  - 1 if this will never happen.

    Examples
    input
    3 3 4
    1 1
    1 2
    2 1
    2 2
    output
    1
    -1
    -1
    2
    input
    3 4 6
    1 1
    2 1
    1 2
    2 2
    1 3
    2 3
    output
    1
    -1
    -1
    2
    5
    -1
    input
    7 4 5
    1 3
    2 2
    5 1
    5 3
    4 3
    output
    13
    2
    9
    5
    -1

    题意:n*m的矩阵,从(0,0)沿45度角射出一束光,速度为根号2,碰到边界反射,到角上结束。k个点,分别输出光到每个点的时间.

    一段一段的模拟。
    每个点y-x和y+x为定值,用两个vector存点,v1[b],v2[b],这样在模拟每一段线段的时候就不用遍历整个点的集合。只需要遍历当前k和b下的vector中的点。
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<string>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    int n,m,t;
    
    struct Node
    {
        int num;
        int x,y;
    
    };
    
    
    Node getPoint(Node st,int k)
    {
        Node p;
        if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=0&&k*(-st.x)+st.y<=m)
        {
            p.x=0;
            p.y=k*(-st.x)+st.y;
            return p;
        }
        if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=0&&k*(n-st.x)+st.y<=m)
        {
            p.x=n;
            p.y=k*(n-st.x)+st.y;
            return p;
        }
        if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=0&&(-st.y+k*st.x)/k<=n)
        {
            p.x=(-st.y+k*st.x)/k;
            p.y=0;
            return p;
        }
        if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=0&&(m-st.y+k*st.x)/k<=n)
        {
            p.x=(m-st.y+k*st.x)/k;
            p.y=m;
            return p;
        }
    }
    
    bool isEnd(int x,int y)
    {
        if(x==n&&y==0)
            return 1;
        if(x==n&&y==m)
            return 1;
        if(x==0&&y==m)
            return 1;
        return 0;
    }
    
    int getCost(int sx,int sy,int k,int tmpx,int tmpy)
    {
        int b=sy-k*sx;
        if(tmpy!=k*tmpx+b)
            return -1;
        return abs(tmpx-sx);
    }
    
    long long ans[100005];
    vector<Node> v1[200005];
    vector<Node> v2[200005];
    int main()
    {
        scanf("%d%d%d",&n,&m,&t);
        /*Node a;
        a.x=3;
        a.y=3;
        Node b=getPoint(a,-1);
        cout<<b.x<<" "<<b.y<<endl;*/
        int all=0;
        for(int i=0; i<t; i++)
        {
            Node node;
            node.num=i;
            scanf("%d%d",&node.x,&node.y);
            int b1=node.y-node.x+100005;
            int b2=node.y+node.x;
            v1[b1].push_back(node);
            v2[b2].push_back(node);
            all++;
        }
        //for(int i=0;i<v2[6].size();i++)
          //  cout<<v2[6][i].num<<" "<<v2[6][i].x<<" "<<v2[6][i].y<<endl;
        Node st;
        st.x=0;
        st.y=0;
        int cnt=0;
        long long time=0;
        while(!isEnd(st.x,st.y))
        {
            int k;
            if(cnt%2)
                k=-1;
            else
                k=1;
            cnt++;
            Node en=getPoint(st,k);
            int b=st.y-k*st.x;
            if(k==1)
            {
                b+=100005;
                for(int i=0; i<v1[b].size(); i++)
                {
                    if(ans[v1[b][i].num]==0)
                    {
                        int cost=getCost(st.x,st.y,k,v1[b][i].x,v1[b][i].y);
                        ans[v1[b][i].num]=time+cost;
                    }
                }
            }
            if(k==-1)
            {
                for(int i=0; i<v2[b].size(); i++)
                {
                    if(ans[v2[b][i].num]==0)
                    {
                        int cost=getCost(st.x,st.y,k,v2[b][i].x,v2[b][i].y);
                        ans[v2[b][i].num]=time+cost;
                    }
                }
            }
            time+=abs(en.x-st.x);
            st=en;
        }
        //cout<<"cnt:"<<cnt<<endl;
        for(int i=0; i<all; i++)
            if(ans[i]==0)
                printf("-1
    ");
            else
                printf("%I64d
    ",ans[i]);
        return 0;
    }
  • 相关阅读:
    weblogic中server的启动
    oracle connect by
    DBA常用的一些视图和sql
    查询分类并实现分类统计SQL存储过程
    从一组数据中寻找第K大的数
    一个关于StringBuilder延伸类
    总结.net的技术特性
    C#Reflection学习记录
    Asp.net 安全补丁发布
    比较无聊的while与for的较量。
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/5958276.html
Copyright © 2020-2023  润新知