关闭

Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) -- C. Ray Tracing(STL乱搞)

标签: C++Codeforces算法
291人阅读 评论(0) 收藏 举报
分类:

大体题意:

告诉你一个n*m的矩形,在告诉你 有k 个传感器位于矩形内部,你从(0,0)位置发射一个东西,只能斜着跑,求到达每一个传感器的最短时间,不能到达输出-1,碰到四个角落退出!否则一指弹!

思路:

充分反应了自己能力的一道题目= =~

没有瞎搞出来,赛后补得!

可以建立一个map<pair<int,int>,set<int> >的东西,前一个pair  表示直线,记录直线的斜率k和b(忘了叫啥了= =)

第二个set记录的是  这个直线经过的输入的传感器标号,传感器放到结构体Node里面!

为了快速计算!我们可以知道 起始坐标和 起始方向,来算出终点坐标,在把这条线所对应所有的传感器 都给他们赋值一个时间,这个时间肯定是最短的,然后删掉这些点,这样 当下次在访问到这条线时,他已经空了,空了表示 重复访问这条线了 ,会形成死循环,需要break了!

在一点注意时间开long long 即可!

就是知道起始坐标和方向算终点坐标比较麻烦!

#include <bits/stdc++.h>
#define fi first
#define se second
#define mr make_pair
using namespace std;
const int maxn = 100000 + 10;
const int inf = 0x3f3f3f3f;
bool vis[maxn][4];
typedef long long ll;
struct Node{
    int x,y;
    ll v;
}p[maxn];
int n,m,K;
map<pair<int,int> ,set<int> >mp;
bool out(int &x,int &y){
    if (x == 0 && y == 0 || x == 0 && y == m || x == n && y == 0 || x == n && y == m)return 1;
    return 0;
}

set<int>::iterator it;
//============================================getnode
Node getNode(int x,int y,int k){
    Node e;
    int b = y-k*x;
    if (x>=0 && x <= n && y == 0){
        if (k == 1){
            int ty = k*n+b;
            if (ty > m){
                e.y = m;
                e.x = (m-b)/k;
            }
            else {
                e.x = n;
                e.y = ty;
            }
        }
        else {
            int ty = b;
            if (ty > m){
                e.y = m;
                e.x = (m-b)/k;
            }
            else {
                e.y = ty;
                e.x = 0;
            }
        }
    }
    else if (x == n && y >= 0 && y <= m){
        if (k == 1){
            int tx = (-b)/k;
            if (tx < 0){
                e.x = 0;
                e.y = b;
            }
            else {
                e.x = tx;
                e.y = 0;
            }

        }
        else {
            int ty = b;
            if (ty > m){
                e.y = m;
                e.x = (m-b)/k;
            }
            else {
                e.x = 0;
                e.y = ty;
            }
        }
    }
    else if (y == m && x >= 0 && x <= n){
        if (k == 1){
            int tx = (-b)/k;
            if (tx < 0){
                e.x = 0;
                e.y = b;
//                printf("hjah\n");
            }
            else {
                e.y = 0;
                e.x = tx;
//                printf("hha\n");
            }
        }
        else {
            int tx = (-b)/k;
            if (tx > n){
                e.x = n;
                e.y = k*n+b;
            }
            else {
                e.x = tx;
                e.y = 0;
            }

        }
    }
    else if (x == 0 && y >= 0 && y <= m){
        if (k == 1){
            int ty = k*n+b;
            if (ty > m){
                e.y = m;
                e.x = (m-b)/k;
            }
            else {
                e.x = n;
                e.y = ty;
            }
        }
        else {
            int ty = k*n+b;
            if (ty < 0){
                e.y = 0;
                e.x = (-b)/k;
            }
            else {
                e.x = n;
                e.y = ty;
            }
        }
    }
    return e;

}
//=============================================  以上的函数 是知道初始坐标 和方向 计算终止坐标  分类讨论即可!
int main(){

    scanf("%d %d %d",&n,&m,&K);
    for (int i = 0; i < K; ++i){
        int u,v;
        scanf("%d %d",&u,&v);
        p[i].x = u;
        p[i].y = v;
        p[i].v = -1; // 时间初始化为-1,表示没赋值!
        mp[mr(1,v-u)].insert(i); // 经过这个点的两条直线 加入这个点
        mp[mr(-1,u+v)].insert(i);
    }
    int x=0,y = 0,k = 1;
    int lef=K;
    ll tim=0ll;
    while(1){
        Node e = getNode(x,y,k);

        if (!mp.count(mr(k,y-k*x)) ){//这条直线并没有经过传感器 就只增加时间 继续下次运动
            tim+=abs(e.x-x);
            k = -k;
            x=e.x;
            y=e.y;
            continue;
        }
        if (mp[mr(k,y-k*x)].empty())break;  // 再次访问到这条直线,已经变空了,说明重复访问了!
        set<int>& tmp = mp[mr(k,y-k*x)];
        for (;!tmp.empty() ;){
                int id = *(tmp.begin());
                --lef;
                if (p[id].v == -1)p[id].v = tim + abs(p[id].x-x);
                tmp.erase(tmp.begin());//统计一个删除一个!
        }
        tim += abs(e.x-x);
        x = e.x; //  移动起点 ,更换方向 继续下次运动!
        y = e.y;
        k = -k;
        if (out(e.x,e.y))break;// 到达四个角落退出
    }
    for (int i = 0; i < K; ++i){
        printf("%lld\n",p[i].v);
    }
    return 0;
}

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 0001 ≤ 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 - 11 ≤ 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
Note

In the first sample, the ray will consequently pass through the points (0, 0)(1, 1)(2, 2)(3, 3). Thus, it will stop at the point (3, 3) after3 seconds.

In the second sample, the ray will consequently pass through the following points: (0, 0)(1, 1)(2, 2)(3, 3)(2, 4)(1, 3)(0, 2),(1, 1)(2, 0)(3, 1)(2, 2)(1, 3)(0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3)(2, 4),(0, 2)(2, 0) and (3, 1).


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:140464次
    • 积分:7218
    • 等级:
    • 排名:第3233名
    • 原创:602篇
    • 转载:2篇
    • 译文:0篇
    • 评论:35条
    文章分类
    最新评论