codeforces 724C
在一个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的更多相关文章
- Codeforces 724C [坐标][乱搞][模拟]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 从(0,0)出发与x轴正方向呈45度角的射线,在给定的矩形区域内不断发射,直到射入矩形的某个角停止. 给出多个坐标,问光线最早经过某坐标的时间. ...
- codeforces 724c Ray Tracing
好题 原题: There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is ...
- Codeforces 724C Ray Tracing 扩展欧几里得
吐槽:在比赛的时候,压根就没想到这题还可以对称: 题解:http://blog.csdn.net/danliwoo/article/details/52761839 比较详细: #include< ...
- CodeForces 724C Ray Tracing(碰撞类,扩展gcd)
又一次遇到了碰撞类的题目,还是扩展gcd和同余模方程.上次博客的链接在这:http://www.cnblogs.com/zzyDS/p/5874440.html. 现在干脆解同余模直接按照套路来吧,如 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)【A,B,C,D】
呵呵哒,上分~ CodeForces 724A: 题意: 给你两个星期几,问连续两个月的头一天是否满足: #include <iostream> #include <stdio.h& ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
随机推荐
- linxu下的shell脚本加密,shell生成二机制可执行文件
再安全的加密也抵不过逆向,斗智斗勇吧,持续加密持续破解 1.简单的加密:gzexe file.sh 2.使用shc加密:下载地址:http://www.datsi.fi.upm.es/~frosal/ ...
- python中pymysql使用
python中pymysql使用 https://blog.csdn.net/johline/article/details/69549131 import pymysql # 连接数据库 conne ...
- 基于Office 365 无代码工作流分析-表单基本需求分析!
3.2表单的制作 基于下图的需求,我们须要定义例如以下的表单列表:
- sql 导入数据库 出现乱码问题 解决办法 设置 --default-character-set=utf8
mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql
- Our happy ending
题目链接 题意: 输入n.k.L,n个数,最大值不超过L,在序列中取若干个数和能达到k的序列个数 n,k<=20 , 0<=L<=10^9 分析: 题目关键在于和k比較小,所以能够考 ...
- 【Java】Java代码经典错误清单
一.String 对照 == 和 equals.详细描写叙述例如以下 "=="操作符的作用 1)用于基本数据类型的比較,例如以下: byte(字节) 8 -128 - 127 0 ...
- 在HDInsight中的Hadoop介绍
在HDInsight中的Hadoop介绍 概览 Azure的HDInsight是,部署和规定的Apache™Hadoop®集群在云中,提供用于管理,分析和大数据报告软件框架中的服务. 大数据 数据被描 ...
- SEO搜索引擎基础原理
- yum lock
状态 :睡眠中,进程ID:18439Another app is currently holding the yum lock; waiting for it to exit... 另一个应用程序是: ...
- js中数组遍历的几种方法及其区别
参考网站: http://www.cnblogs.com/lvmh/p/6104397.html 第一种最常用的:for循环 for(j = 0; j < arr.length; j++) { ...