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 ...
随机推荐
- 10分钟学会前端工程化(webpack4.0)
一.概要 1.1.前端工程化 随着前端的不断发展与壮大,前端变得越来越复杂,组件化.模块化.工程化.自动化成了前端发展中不可或缺的一部分,具体到前端工程化,面临的问题是如何提高编码->测试-&g ...
- iOS中创建自定义的圆角按钮
iOS中很多时候都需要用到指定风格的圆角按钮,尽管UIButton提供了一个方式创建圆角按钮: + (id)buttonWithType:(UIButtonType)buttonType;//指定bu ...
- php 中函数获取可变参数的方法, 这个语法有点像 golang 语言中的
原文呢:http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict Onl ...
- github 新建远程仓库 及 删除远程仓库
一.新建远程仓库 1.点击 ' + ' 号 2.选择 ' new repository ' 3.填写信息 创建仓库 二.删除远程仓库 1.点击 ' Settings ' 按钮 2.滑动到最底部,点击 ...
- c++面试题总结(4)
一.找错题 试题1: void test1() { ]; "; strcpy( string, str1 ); } 试题2: void test2() { ],str1[]; int i; ...
- [转载]JSONP跨域的原理解析
JavaScript是一种在Web开发中经常使用的前端动态脚本技术.在JavaScript中,有一个很重要的安全性限制,被称为“Same-Origin Policy”(同源策略).这一策略对于Java ...
- Ubuntu安装教程--Win7系统中含100M保留分区
1.检查 Win7 保留分区 1)进入 Win7 打开库目录.在左側栏找到"计算机",瞄准点右键选择"管理"菜单: 2)在出来的管理面板左边找到"磁盘 ...
- java验证码图片
package com.su.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; impor ...
- python 爬虫1 開始,先拿新浪微博開始
刚刚開始学. 目的地是两个.一个微博,一个贴吧 存入的话,临时还没想那么多.先存到本地目录吧 分词和推荐后面在整合 mysql mongodb hadoop redius 后面在用 我最终知道为什么大 ...
- subclassdlgitem
subclassdlgitem 该函数用来子类化一个控件. Subclass(子类化)是MFC中最常用的窗体技术之一.子类化完成两个工作:一是把窗体类对象attach到一个windows窗体实体中(即 ...