题目链接 Rooter's Song

题意  有n个舞者站在x轴上或y轴上,每个人有不同的出发时间。x轴上的舞者垂直x轴正方向移动,y轴上的舞者垂直y轴正方向移动。

当x轴的舞者和y轴的舞者相遇时,他们会互换运动轨迹。求每个舞者的最后位置。

把所有会发生碰撞的舞者塞到一起,按照坐标大小升序排序。

对于某个在x轴上的舞者, 计算他右边的舞者个数,和他上面的舞者个数(即y轴会和他碰撞的所有舞者个数)

对于某个在y轴上的舞者, 计算他上面的舞者个数,和他右边的舞者个数(即x轴会和他碰撞的所有舞者个数)

然后根据这些信息计算出每个舞者的碰撞次数,再求出每个舞者最后的位置。

时间复杂度$O(Mlogn)$    $(M = max(w, h))$

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 4e5 + 10;
const int d = 1e5; struct node{
int pos, id;
friend bool operator < (const node &a, const node &b){
return a.pos < b.pos;
}
}; int n, w, h, op, p, t, mx, s;
vector <node> X[N], Y[N];
int flag[N], ans[N], b[N], c[N]; int main(){ scanf("%d%d%d", &n, &w, &h);
mx = 0; rep(i, 1, n){
scanf("%d%d%d", &op, &p, &t);
b[i] = op, c[i] = p;
if (op == 1) X[p - t + d].push_back({p, i});
else Y[p - t + d].push_back({p, i});
mx = max(mx, p - t + d);
} rep(i, 0, mx){
sort(X[i].begin(), X[i].end());
sort(Y[i].begin(), Y[i].end());
} rep(i, 0, mx){
s = (int)X[i].size();
t = (int)Y[i].size(); if (s == 0 || t == 0) continue; rep(j, 0, s - 1){
int nx = s - j - 1, ny = t;
int num;
if (nx < ny) num = 2 * nx + 1;
else num = ny * 2;
if (num & 1) flag[X[i][j].id] = 2; else flag[X[i][j].id] = 1;
if (flag[X[i][j].id] == 1) ans[X[i][j].id] = X[i][j + num / 2].pos;
else ans[X[i][j].id] = Y[i][num / 2].pos;
} rep(j, 0, t - 1){
int nx = s, ny = t - j - 1;
int num;
if (ny < nx) num = 2 * ny + 1;
else num = nx * 2;
if (num & 1) flag[Y[i][j].id] = 1; else flag[Y[i][j].id] = 2;
if (flag[Y[i][j].id] == 2) ans[Y[i][j].id] = Y[i][j + num / 2].pos;
else ans[Y[i][j].id] = X[i][num / 2].pos; }
} rep(i, 1, n) if (!flag[i]){ flag[i] = b[i]; ans[i] = c[i];}
rep(i, 1, n) if (flag[i] == 1) printf("%d %d\n", ans[i], h);
else printf("%d %d\n", w, ans[i]);
return 0;
}

  

Codeforces 848B Rooter's Song(分类+模拟)的更多相关文章

  1. codeforces 848B Rooter's Song 思维题

    http://codeforces.com/problemset/problem/848/B 给定一个二维坐标系,点从横轴或纵轴垂直于发射的坐标轴射入(0,0)-(w,h)的矩形空间.给出点发射的坐标 ...

  2. codeforces 848B Rooter's Song

    题目链接 正解:排序+模拟. 我们注意到两个点碰撞的必要条件,$pi+tj=pj+ti$,移项以后发现就是$pi-ti=pj-tj$,那么我们可以把$p-t$相同的点分为同一组. 然后我们还可以发现一 ...

  3. codeforces 848B - Rooter's Song(构造+几何)

    原题链接:http://codeforces.com/problemset/problem/848/B 题意:好多个人分别从x,y轴不同位置不同时间往垂直坐标轴方向移动,一旦相遇他们转向,问所有人的到 ...

  4. [CodeForces - 848B] Rooter's Song 思维 找规律

    大致题意: 有一个W*H的长方形,有n个人,分别站在X轴或Y轴,并沿直线向对面走,第i个人在ti的时刻出发,如果第i个人与第j个人相撞了 那么则交换两个人的运动方向,直到走到长方形边界停止,问最后每个 ...

  5. codeforces 723B Text Document Analysis(字符串模拟,)

    题目链接:http://codeforces.com/problemset/problem/723/B 题目大意: 输入n,给出n个字符的字符串,字符串由 英文字母(大小写都包括). 下划线'_' . ...

  6. Codeforces Round #304 C(Div. 2)(模拟)

    题目链接: http://codeforces.com/problemset/problem/546/C 题意: 总共有n张牌,1手中有k1张分别为:x1, x2, x3, ..xk1,2手中有k2张 ...

  7. Codeforces 749C:Voting(暴力模拟)

    http://codeforces.com/problemset/problem/749/C 题意:有n个人投票,分为 D 和 R 两派,从1~n的顺序投票,轮到某人投票的时候,他可以将对方的一个人K ...

  8. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  9. Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. OS X快捷键小技巧

    退出command+Q,关分页Command+W,刷新Command+R,新开分页Command+T 全屏 ctrl+command+F 每个Mac使用者都知道点击下窗口左上角黄色圆形的按钮就可以最小 ...

  2. Difference between x:Reference and x:Name

    {x:Reference ...} -> returns just a reference of an object it doesn't create that "bridge&qu ...

  3. hibernate3缓存(hibernate)

    一级缓存:当应用程序调用Session 的save() .update() .savaeOrUpdate() .get() 或load() ,以及调用查询接口的list() .iterate() 或f ...

  4. shell脚本,检查给出的字符串是否为回文

    [root@localhost wyb]# .sh #!/bin/bash #检查给出的字符串是否为回文 read -p "Please input a String:" numb ...

  5. SimpleWeather APP

    参考 iOS 7 Best Practices; A Weather App Case Study: Part 1/2 iOS 7 Best Practices; A Weather App Case ...

  6. ios 登录功能学习研究

    登录功能是我在湖畔做的第一个需求. 当时PD给我的草图和下图类似: (图片来自知乎iOS客户端登录界面) 不过需求中要求用户名或者密码错误时,输入框要抖动(类似Mac登录密码错误的抖动效果). 如果实 ...

  7. HDU-2018-奶牛的故事

    这题找到递推式就好写了,递推式大致是: f=n (n<=4) f=f(n-1)+f(n-3) (n>4) 其实这题的题意,我觉得是有很大的问题的,它前后说的每年年初的意思都不一样,敬请参考 ...

  8. pip install mysqlclient 报错:error: Microsoft Visual C++ 14.0 is required.

    解决办法: 1. 在网址:https://www.lfd.uci.edu/~gohlke/pythonlibs/  下载对应的whl文件,如我的环境是python3.7.2  windows32位版本 ...

  9. Redis string类型常用操作

      Redis 有 string.list.set.zset.hash数据类型.string类型是最基础的,其他类型都是在string类型上去建立的,所以了解熟悉string类型的常用操作对于学习re ...

  10. 破解点触码的识别之第三方平台超级鹰的SDK(python3版本)

    import requestsfrom hashlib import md5 class Chaojiying(object): def __init__(self, username, passwo ...