在一个nxm的镜面二维空间内,向(1,1)发射一条射线,来回反射,当遇到四个角之一时光线消失。

给K个点,问K个点第一次被射中是什么时候(v = sqrt(2))

解:注意到只有 2*(n+m)个对角线,从而从(1,1)发射光线后,最多折射O(n)此。

只需要模拟光线的折射即可。

具体实现时,记录光线的起点,终点。记录直线方程(截距和斜率的正负)

每一次用上一个终点和直线方程算出新的光线的终点。

(分为2*2*2 = 8种情况讨论)

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector> #define N 100010
#define INF 0x3f3f3f3f3f3f3f3fLL
#define LL long long using namespace std; struct node{
int x,y,id;
void scan(int i){
id=i;
scanf("%d%d",&x,&y);
}
}a[N]; struct line{
node S,T;
int typ,t;
}b[N]; int n,m,K;
LL ansv[N];
vector<node> A[][*N];
queue<line> q;
bool vis[][*N]; int Abs(int x){
if(x<) return -x;
return x;
} void update_min(LL &a,LL b){
if(b<a) a=b;
} bool get_next(line now){
if((now.T.x==||now.T.x==n) && (now.T.y==||now.T.y==m)) return ;
line ans;
ans.S=now.T;
ans.typ=now.typ^; if(ans.typ==){
ans.t = ans.S.x+ans.S.y; if(now.S.x < now.T.x){
if(now.T.y == m){
if(ans.t-n >= ) ans.T = (node){n,ans.t-n};
else ans.T = (node){ans.t,};
}
else ans.T = (node){ans.t-m,m};
}
else{
if(now.T.y == ){
if(ans.t <= m) ans.T = (node){,ans.t};
else ans.T = (node){ans.t-m,m};
}
else ans.T = (node){ans.t,};
}
} else{
ans.t = ans.S.y-ans.S.x+n; if(now.S.x < now.T.x){
if(now.T.y == ){
if(ans.t <= m) ans.T = (node){n,ans.t};
else ans.T = (node){m+n-ans.t,m};
}
else ans.T = (node){n-ans.t,};
}
else{
if(now.T.y == m){
if(ans.t <= n) ans.T = (node){n-ans.t,};
else ans.T = (node){,ans.t-n};
}
else ans.T = (node){m+n-ans.t,m};
}
}
// cout << "node " << ans.T.x << ' ' << ans.T.y << endl;
// cout << ans.typ << ' ' << ans.t << endl;
if(vis[ans.typ][ans.t]) return ;
q.push(ans);
return ;
} int main(){
// freopen("test.txt","r",stdin);
scanf("%d%d%d",&n,&m,&K);
for(int i=;i<=K;i++) a[i].scan(i);
if(n<m){
swap(n,m);
for(int i=;i<=K;i++)
swap(a[i].x,a[i].y);
} for(int i=;i<=K;i++){
A[][a[i].x+a[i].y].push_back(a[i]);
A[][a[i].y-a[i].x+n].push_back(a[i]);
ansv[i]=INF;
} vis[][n]=;
q.push((line){(node){,,}, (node){m,m,}, ,n});
LL ansnow=; while(!q.empty()){
line tmp=q.front(); q.pop(); vector<node> &now = A[tmp.typ][tmp.t];
for(int i=,ln=now.size();i<ln;i++)
update_min(ansv[now[i].id], ansnow + (LL)Abs(now[i].x-tmp.S.x) ); ansnow += (LL)Abs(tmp.T.x-tmp.S.x);
if(!get_next(tmp)) break;
} for(int i=;i<=K;i++)
if(ansv[i]!=INF) printf("%I64d\n",ansv[i]);
else puts("-1");
return ;
}

codeforces 724C的更多相关文章

  1. Codeforces 724C [坐标][乱搞][模拟]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 从(0,0)出发与x轴正方向呈45度角的射线,在给定的矩形区域内不断发射,直到射入矩形的某个角停止. 给出多个坐标,问光线最早经过某坐标的时间. ...

  2. codeforces 724c Ray Tracing

    好题 原题: There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is ...

  3. Codeforces 724C Ray Tracing 扩展欧几里得

    吐槽:在比赛的时候,压根就没想到这题还可以对称: 题解:http://blog.csdn.net/danliwoo/article/details/52761839 比较详细: #include< ...

  4. CodeForces 724C Ray Tracing(碰撞类,扩展gcd)

    又一次遇到了碰撞类的题目,还是扩展gcd和同余模方程.上次博客的链接在这:http://www.cnblogs.com/zzyDS/p/5874440.html. 现在干脆解同余模直接按照套路来吧,如 ...

  5. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)【A,B,C,D】

    呵呵哒,上分~ CodeForces 724A: 题意: 给你两个星期几,问连续两个月的头一天是否满足: #include <iostream> #include <stdio.h& ...

  6. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  9. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

随机推荐

  1. 使用SourceTree 来管理 Gitcafe 的Pages 发布Blog!

    有个好爹的 SourceTree 是来自 JIRA 的娘家 Bitbucket 的新东家 ATLASSIAN.com 一家成功的,对敏捷软件工程拥有全栈式支持的商业公司, 所推出的 MAC 专用, S ...

  2. 【paddle学习】图像分类

    https://zhuanlan.zhihu.com/p/28871960 深度学习模型中的卷积神经网络(Convolution Neural Network, CNN)近年来在图像领域取得了惊人的成 ...

  3. Django简易安装

    Django简易安装 1,下载 https://www.djangoproject.com/download/ 2, 拷贝至python同级目录 python setup.py install 3,在 ...

  4. 【HDOJ 5399】Too Simple

    pid=5399">[HDOJ 5399]Too Simple 函数映射问题 给出m函数 里面有0~m个函数未知(-1) 问要求最后1~n分别相应仍映射1~n 有几种函数写法(已给定的 ...

  5. Youth is not a time of life, it is a state of mind.

    青春不是生命的一段,而是一种精神状态.

  6. C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲

     使用C++风格的数组.不须要管理内存. array要注意不要溢出,由于它是栈上开辟内存. array适用于不论什么类型 #include<iostream> #include< ...

  7. 【转载】读懂IL代码就这么简单(二)

    一 前言 IL系列 第一篇写完后 得到高人指点,及时更正了文章中的错误,也使得我写这篇文章时更加谨慎,自己在了解相关知识点时,也更为细致.个人觉得既然做为文章写出来,就一定要保证比较高的质量,和正确率 ...

  8. POJ1144 Network 无向图割点

    题目大意:求以无向图割点. 定义:在一个连通图中,如果把点v去掉,该连通图便分成了几个部分,则v是该连通图的割点. 求法:如果v是割点,如果u不是根节点,则u后接的边中存在割边(u,v),或者v-&g ...

  9. id、NSObject *、id<NSObject>、instancetype

    1. id 与 NSObject * (1) id 是 Objective-C 对象,但是并不一定是NSObject对象,并非所有的Foundation/Cocoa对象都是继承于NSObject对象的 ...

  10. Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland

    C. Roads in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...