题目链接 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. CPP-网络/通信:COM

    ))//打开串口 { ) { CloseCom();//关闭串口 break; } //添加处理代码. } //最后关闭串口 CloseCom();//关闭串口

  2. How to debug add-ins for arcgis

    Debugging add-ins To debug an add-in, follow these steps: Confirm that the add-in is deployed to the ...

  3. Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)

    Person类中的各种属性写法如下: package com.swift.person; import java.util.Arrays; import java.util.List; import ...

  4. grep-sed命令用法:

    用户切换 su username:非登录式切换 su - username:登录式切换 su -l username:登录式切换 su username -c COMMAND   echo -n   ...

  5. 【模板】任意模数NTT

    题目描述: luogu 题解: 用$fft$水过(什么$ntt$我不知道). 众所周知,$fft$精度低,$ntt$处理范围小. 所以就有了任意模数ntt神奇$fft$! 意思是这样的.比如我要算$F ...

  6. Mysql 5.7在Linux上部署及远程访问

    序言:最近要和伙伴一起组队,做.NET Core项目.所以自己就租了一个阿里云服务器,并且装了Linux和MySQL.这里面我的Linux是CentOs 7. 第一步 添加Mysql Yum库 这里面 ...

  7. (三)Python3 循环语句——while

    while语句的一般形式: while 判断条件: 语句 同样需要注意冒号和缩进.另外,在 Python 中没有 do..while 循环. 以下实例使用了 while 来计算 1 到 100 的总和 ...

  8. python--线程的其他方法

    一 . current_thread的用法 import threading import time from threading import Thread, current_thread def ...

  9. Linux中断底半部机制

    参考: Linux下半部处理之软中断 linux中断底半部机制 <深入理解Linux内核>软中断/tasklet/工作队列 软中断和tasklet介绍 详解操作系统中断 Linux内核:中 ...

  10. PAT Basic 1065

    1065 单身狗 “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ 50 000),是已知夫妻/伴侣 ...