【排序】【规律】Codeforces Round #431 (Div. 2) - D. Rooter's Song
D. Rooter's Song
Source
http://codeforces.com/contest/849/problem/D
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).
![](http://codeforces.com/predownloaded/c8/ef/c8ef4febb53df4c92041d2cc722683350ca6d14e.png)
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.
![](http://codeforces.com/predownloaded/9b/9f/9b9fb73178590e383ca6e5925d5dcf60501e28ef.png)
Dancers stop when a border of the stage is reached. Find out every dancer's stopping position.
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 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.
8 10 8
1 1 10
1 4 13
1 7 1
1 8 2
2 2 0
2 5 14
2 6 0
2 6 1
4 8
10 5
8 8
10 6
10 2
1 8
7 8
10 6
3 2 3
1 1 2
2 1 1
1 1 5
1 3
2 1
1 3
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.
![](http://codeforces.com/predownloaded/64/1e/641e1661ff9a4907ac92affb7e75a1c67422a16f.png)
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 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步