在一个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. Linux C多线程编程-线程互斥

    Linux下的多线程编程需要注意的是程序需要包含头文件pthread.h,在生成可执行文件的时候需要链接库libpthread.a或者libpthread.so. 线程创建函数: pthread_cr ...

  2. mysqldump 把数据库备份到异地的服务器

    原文:http://www.open-open.com/code/view/1420121471484 这个方法可以把通过mysqldump 把本地数据库备份到远端主机, 中间数据的传输通过 ssh ...

  3. Java屏幕截图工具 捕获屏幕

    原文:http://www.open-open.com/code/view/1420037709781 import java.awt.BorderLayout; import java.awt.Co ...

  4. ElasticSearch集群本机搭建

    ElasticSearch集群本机搭建 elasticsearch elasticsearch -Ehttp.port=8200 -Epath.data=node2 elasticsearch -Eh ...

  5. Android开发之入口Activity

    Android开发之入口Activity Adnroid App是怎样确定入口Activity的? 难道就由于class的类名叫MainActivity,布局文件叫activity_main.xml? ...

  6. POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  7. 最小公倍数(Least Common Multiple)

    最小公倍数=两个数的乘积/两个数的最大公约数. 接上篇求最大公约数方法,最小公倍数的代码例如以下: public class LCM { //最小公倍数=两数乘积/最大公约数 public stati ...

  8. npm 的常用操作

    cd 项目目录 npm install -y 初始化信息,-y表示所有的选择都表示确定 执行完之后会自动生成一个package.json文件 添加依赖: npm -i(install) jquery ...

  9. JavaScript 的 MVP 框架 Riot.js

    Riot.js 详细介绍 Riot.js是一个客户端模型-视图-呈现(MVP)框架并且它非常轻量级甚至小于1kb.尽管他的大小令人难以置信,所有它能构建的有如下:一个模板引擎,路由,甚至是库和一个严格 ...

  10. Asp.net常用的51个代码(非常实用)

    1.//弹出对话框.点击转向指定页面 Response.Write("<script>window.alert('该会员没有提交申请,请重新提交!')</script> ...