【排序】【规律】Codeforces Round #254 (Div. 2) - D. Rooter's Song

D. DZY Loves FFT

Source

http://codeforces.com/contest/445/problem/D

Description

Wherever the destination is, whoever we meet, let‘s render this song together.

On a Cartesian coordinate plane lies a rectangular stage of size w?×?h, represented by a rectangle with corners (0,?0), (w,?0), (w,?h) and (0,?h). It can be seen that no collisions will happen before one enters the stage.

On the sides of the stage stand n dancers. The i-th of them falls into one of the following groups:

  • Vertical: stands at (xi,?0), moves in positive y direction (upwards);
  • Horizontal: stands at (0,?yi), moves in positive x direction (rightwards).

According to choreography, the i-th dancer should stand still for the first ti milliseconds, and then start moving in the specified direction at 1 unit per millisecond, until another border is reached. It is guaranteed that no two dancers have the same group, position and waiting time at the same time.

When two dancers collide (i.e. are on the same point at some time when both of them are moving), they immediately exchange their moving directions and go on.

Dancers stop when a border of the stage is reached. Find out every dancer‘s stopping position.

Input

The first line of input contains three space-separated positive integers n, w and h (1?≤?n?≤?100?000, 2?≤?w,?h?≤?100?000) — the number of dancers and the width and height of the stage, respectively.

The following n lines each describes a dancer: the i-th among them contains three space-separated integers gi, pi, and ti (1?≤?gi?≤?2, 1?≤?pi?≤?99?999, 0?≤?ti?≤?100?000), describing a dancer‘s group gi (gi?=?1 — vertical, gi?=?2 — horizontal), position, and waiting time. If gi?=?1 then pi?=?xi; otherwise pi?=?yi. It‘s guaranteed that 1?≤?xi?≤?w?-?1 and 1?≤?yi?≤?h?-?1. It is guaranteed that no two dancers have the same group, position and waiting time at the same time.

Output

Output n lines, the i-th of which contains two space-separated integers (xi,?yi) — the stopping position of the i-th dancer in the input.

Examples

Input

8 10 81 1 101 4 131 7 11 8 22 2 02 5 142 6 02 6 1

Output

4 810 58 810 610 21 87 810 6

Input

3 2 31 1 22 1 11 1 5

Output

1 32 11 3

Note

The first example corresponds to the initial setup in the legend, and the tracks of dancers are marked with different colours in the following figure.

In the second example, no dancers collide.

Solution

挺不错的一题,官方题解讲得也挺详细的:

How to deal with "waiting time"?

Move every dancer ti units backwards in the first place, that is to (xi?-?ti,?0) for the vertical-moving group, and (0,?yi?-?ti) for the horizontal-moving group. Then start the time, making everyone start moving immediately.

When do dancers collide? What changes and what keeps the same?

Notice that if two dancers collide before any other collision happens, then they have the same x?+?y values for their initial positions. Furthermore, after a collision, the two dancers keep having the same x?+?y, and also with the same relative orders of x and y. Also, after a collision, the union of all dancers‘ tracks will be the same as if they "went through" each other and no collision happened at all (see the figure for sample 1 to get a general idea on this).

Therefore, divide dancers into groups by pi?-?ti, and collisions will happen within groups only. Dancers in the same group will move on the same x?+?y line (a line of slope ?-?1), and however collisions take place, they will keep current relative order of x and y. It‘s proved before that in each group, dancers‘ exiting positions is the same as if no collision happened at all (namely, (xi,?h) for initially-vertical dancers, and (w,?yi) for initially-horizontal ones). For each group, find out all such positions. Sort all dancers according to their initial x values, and sort these positions in the direction of (0,?h) to (w,?h) then (w,?0). Match the sorted dancers to these sorted positions and obtain the answers for all dancers. This solution works in .

注意排序的顺序,附上自己的代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 const int maxn = 100100;
 5
 6 struct Node{
 7     int x,y;
 8 }Ans[maxn];
 9
10 int n,w,h,g[maxn],p[maxn],t[maxn];
11 vector<vector<int>>grp(2*maxn);
12 vector<vector<Node>>res(2*maxn);
13
14 bool cmp1(int a,int b){
15     if(g[a] != g[b]) return g[a] > g[b];
16     if(g[a] == 2) return p[a] > p[b];
17     return p[a] < p[b];
18 }
19
20 bool cmp2(Node a,Node b){
21     if(a.y == h && b.x == w) return 1;
22     if(a.x == w && b.y == h) return 0;
23     if(a.y == h) return a.x < b.x;
24     return a.y > b.y;
25 }
26
27 int main(){
28     cin >> n >> w >> h;
29     for(int i = 1;i <= n;++i){
30         scanf("%d%d%d",&g[i],&p[i],&t[i]);
31         if(g[i] == 1){
32             grp[p[i]-t[i]+100000].push_back(i);
33             res[p[i]-t[i]+100000].push_back({p[i],h});
34         }
35         else{
36             grp[p[i]-t[i]+100000].push_back(i);
37             res[p[i]-t[i]+100000].push_back({w,p[i]});
38         }
39     }
40     for(int i = 0;i < 200000;++i){
41         if(grp[i].empty()) continue;
42         sort(grp[i].begin(),grp[i].end(),cmp1);
43         sort(res[i].begin(),res[i].end(),cmp2);
44         for(int j = 0;j < grp[i].size();++j) Ans[grp[i][j]] = res[i][j];
45     }
46     for(int i = 1;i <= n;++i) printf("%d %d\n",Ans[i].x,Ans[i].y);
47
48     return 0;
49 }

【排序】【规律】Codeforces Round #254 (Div. 2) - D. Rooter's Song

时间: 2024-10-13 23:47:46

【排序】【规律】Codeforces Round #254 (Div. 2) - D. Rooter's Song的相关文章

Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)

题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的次序将1-n种试剂滴入试管,如果正在滴入的试剂能与已经滴入 的试剂反应,那么危险数*2,否则维持不变.问最后最大的危险系数是多少. 分析:其实这个题根本不用考虑倒入的顺序,只是分块就行,结果就是每个子集里元素的个数-1 和  的2的幂. 1 #include <iostream> 2 #inclu

Codeforces Round #254 (Div. 2) DZY Loves Chemistry【并查集基础】

一开始不知道题意是啥意思,迟放进去反应和后放进去反应有什么区别 对于第三组数据不是很懂,为啥312,132的组合是不行的 后来发现这是一道考察并查集的题目 QAQ 怒贴代码: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostream> 6 #include <algorithm> 7

Codeforces Round #254 (Div. 2)

Codeforces Round #254 (Div. 2) 题目链接 A题:给定一个棋盘,放B,W不能相邻,输出摆法 思路:模拟国际象棋,B放在白格,A放在黑格即可 B题:给定一些化学物品,给定哪些可以反应,现在一一加入试管,如果试管之前有加过可以反应的,危险度乘2,初始危险度为1,求最小危险度 思路:用并查集,找出有多少个集合,这些先加进去保证不会反应,那么剩下的一个个加进去都乘2即可 C题:求一个最小密度连通子图,子图必须为一个集合,并且两点存在的话,他们的边必须存在 思路:推一推公式发现

【推导】【分类讨论】Codeforces Round #431 (Div. 1) B. Rooter&#39;s Song

给你一个这样的图,那些点是舞者,他们每个人会在原地待ti时间之后,以每秒1m的速度向前移动,到边界以后停止.只不过有时候会碰撞,碰撞之后的转向是这样哒: 让你输出每个人的停止位置坐标. ①将x轴上初始坐标记为(pi,0),y轴上的初始坐标记为(0,pi).只有pi-ti相同的才有可能发生碰撞.于是可以按照这一点将人划分为很多组,不同组之间绝对不会互相影响. ②假设一组内每个人都不会发生碰撞,那么所有的路线交叉点都是碰撞点.所以碰撞次数可能达到n^2次,暴力不可行. ③对于一组内,形成了一个网格图

找规律 Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks

题目传送门 1 /* 2 找规律,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 1e4 + 10; 12 const int INF = 0x3f3f3f3f; 13 char s

Codeforces Round #254 (Div. 1)-A,B

A:选取两点一边就能够了,非常明显能够想出来... 可是一開始看错题了,sad.... #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<queue> #include<math.h> using namespace std; #define eps 1e-6 #define zero(x) ((fabs(x)<

Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market 二分答案 +排序

Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market 二分答案 +排序 题意 有 a[ i ] 个数 要求选最多的数 使其和不超过 S ,且在此情况下,和最小选最多数情况下 和最小 且 每个数有加成 如果选了 k个数 那么加成后 就是 a[ i ] + k*i ; 题解 二分mid 表示选了个数 加成一下,将加成以后结果排序一下 , 若前 mid数 和大于 s 则此方案不可行 PS 要用 long long ..... 还有 co

找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

题目传送门 1 /* 2 找规律/贪心:ans = n - 01匹配的总数,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 2e5 + 10; 12 const int INF =

数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

题目传送门 1 /* 2 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <iostream> 8 #include <cmath> 9 #include <vector> 10 using namespace std; 11