zoukankan      html  css  js  c++  java
  • Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing

    C. Ray Tracing
     

    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

    题意是一束光从(0,0)射出,光线速度为根号2的单位每秒,求一路上有k个传感仪,求各个传感仪第一次接收到光线信号的时间,如果接收不到就输出-1。

    观察到光线与坐标轴的角度始终为45°,那样的话说明光线的位置可以用一个数字来表示,这个类似八皇后的时候的斜线表示。用两个vector来储存各斜线上的点。用dx,dy可以表示光线方向。
    然后就可以枚举光线路径斜线上的传感仪更新答案,再更新光线撞墙的坐标和光线方向,直到光线到达角落。

    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    const int maxn = 2e5 + 10;
    vector< pair<int, int> > va[maxn], vb[maxn];
    ll ans[maxn];
    int main() 
    {
        int n, m, k;
        memset(ans, 0x3f, sizeof(ans));
        scanf("%d %d %d", &n, &m, &k);
        for(int i = 1; i <= k; i++) {
            int x, y;
            scanf("%d %d", &x, &y);
            va[x+y].emplace_back(i, x);
            vb[x-y+m].emplace_back(i, x);
        }
    
        int x = 0, y = 0; //定义初始坐标
        int dx = 1, dy = 1; //定义初始方向
        ll t = 0; //定义初始时间
        while(true) {
            if(dx == dy) {
                for(auto p:vb[x-y+m]) {
                    ans[p.first] = min(ans[p.first], t + abs(p.second - x));
                }
            }
            else {
                for(auto p:va[x+y]) {
                    ans[p.first] = min(ans[p.first], t + abs(p.second - x));
                }
            }
            int time = 0x3f3f3f3f;
            if(dx == -1) time = min(time, x);
            else time = min(time, n - x);
            if(dy == -1) time = min(time, y);
            else time = min(time, m - y);
            x = x + time * dx;
            y = y + time * dy;
            t = time + t;
            bool flag1 = (x == 0 || x == n);
            bool flag2 = (y == 0 || y == m);
            if(flag1 && flag2) break;
            if(flag1) dx = -dx;
            if(flag2) dy = -dy;
        }
        for(int i = 1; i <= k; i++) {
            if(ans[i] == 0x3f3f3f3f3f3f3f3f) puts("-1");
            else printf("%I64d
    ", ans[i]);
        }
    }



     
  • 相关阅读:
    为 TortoiseGit 添加 ssh key---运行 TortoiseGit 开始菜单中的 Pageant 程序将ppk私钥加入即可
    公共wifi下的中间人攻击
    漏洞扫描原理——将主机扫描、端口扫描以及OS扫描、脆弱点扫描都统一放到了一起
    网络扫描——非常不错的文章,主要分为端口扫描(确定开放服务)和主机扫描(确定机器存活)
    网络安全类别划分——网络信息采集(端口扫描、漏洞扫描、网络窃听)、拒绝服务攻击、漏洞攻击
    网络欺骗——网络欺骗就是使攻击者可以相信网络信息系统存在有价值的、可利用的安全弱点 蜜罐等
    深度学习调参技巧总结
    深度学习网络调参技巧
    如何选择神经网络的超参数
    深度学习调参策略(二)
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5944235.html
Copyright © 2011-2022 走看看