codeforces 848B
来源:互联网 发布:手机快速充电软件 编辑:程序博客网 时间:2024/06/15 03:16
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. Thei-th of them falls into one of the following groups:
- Vertical: stands at (xi, 0), moves in positivey direction (upwards);
- Horizontal: stands at (0, yi), moves in positivex direction (rightwards).
According to choreography, the i-th dancer should stand still for the firstti milliseconds, and then start moving in the specified direction at1 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.
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: thei-th among them contains three space-separated integersgi,pi, andti (1 ≤ gi ≤ 2,1 ≤ pi ≤ 99 999,0 ≤ ti ≤ 100 000), describing a dancer's groupgi (gi = 1 — vertical,gi = 2 — horizontal), position, and waiting time. Ifgi = 1 thenpi = xi; otherwisepi = yi. It's guaranteed that1 ≤ xi ≤ w - 1 and1 ≤ 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 thei-th dancer in the input.
8 10 81 1 101 4 131 7 11 8 22 2 02 5 142 6 02 6 1
4 810 58 810 610 21 87 810 6
3 2 31 1 22 1 11 1 5
1 32 11 3
先看一张图
假设每个交点都能相撞,规律就很明显了。
首先分别记录x点坐标和y点坐标,以坐标从小到大为第一条件 时间从小到大为第二条件分别排序。
我们不可能直接知道某个起点的最终位置,但是可以知道的是所有终点的位置,所以应该对于x轴和y轴分别枚举终点。
以上图x轴为例,从小到大枚举,那么所有起点一定是由大到小的y轴上可以相撞的点,如果y轴点用光了怎么办,x轴上可以相撞的点由小到大接着填。
如果都用光了,不会有相撞,那个位置的起点直达终点,按照这个规则记录下下标输出就行了。
为什么会这样画画图就懂了。
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxm = 100005;const int INF = 120000;struct node{int pos, id, t;bool operator<(const node &r)const{if (pos != r.pos) return pos < r.pos;else return t < r.t;}}x[maxm], y[maxm];int flag[maxm * 3], fx[maxm * 3], fy[maxm * 3];int f[maxm * 3], vis[maxm * 3], gx[maxm * 3], gy[maxm * 3];struct H{int x, y;}p[maxm];int main(){int n, i, j, k, sum, ord, a, b, w, h;int sx = 0, sy = 0, cnt = 0;scanf("%d%d%d", &n, &w, &h);for (i = 1;i <= n;i++){scanf("%d%d%d", &ord, &a, &b);if (ord == 1){x[++sx].pos = a;x[sx].t = b, x[sx].id = i;}else{y[++sy].pos = a;y[sy].t = b, y[sy].id = i;}}sort(x + 1, x + 1 + sx);sort(y + 1, y + 1 + sy);for (i = 1;i <= sy;i++){int now = y[i].t - y[i].pos + INF;fy[y[i].id] = flag[now];flag[now] = y[i].id, gy[now] = y[i].id;}memset(flag, 0, sizeof(flag));for (i = sx;i >= 1;i--){int now = x[i].t - x[i].pos + INF;fx[x[i].id] = flag[now];flag[now] = x[i].id, gx[now] = x[i].id;}for (i = 1;i <= sx;i++){int now = x[i].t - x[i].pos + INF;if (gy[now] != 0){p[gy[now]].x = x[i].pos, p[gy[now]].y = h;gy[now] = fy[gy[now]];}else if (gx[now] != 0){p[gx[now]].x = x[i].pos,p[gx[now]].y = h;gx[now] = fx[gx[now]];}else { p[x[i].id].x = x[i].pos, p[x[i].id].y = h; }}memset(flag, 0, sizeof(flag));memset(fx, 0, sizeof(fx));memset(fy, 0, sizeof(fy));memset(gx, 0, sizeof(gx));memset(gy, 0, sizeof(gy));for (i = 1;i <= sx;i++){int now = x[i].t - x[i].pos + INF;fx[x[i].id] = flag[now];flag[now] = x[i].id, gx[now] = x[i].id;}memset(flag, 0, sizeof(flag));for (i = sy;i >= 1;i--){int now = y[i].t - y[i].pos + INF;fy[y[i].id] = flag[now];flag[now] = y[i].id, gy[now] = y[i].id;}for (i = 1;i <= sy;i++){int now = y[i].t - y[i].pos + INF;if (gx[now] != 0) { p[gx[now]].x = w, p[gx[now]].y = y[i].pos; gx[now] = fx[gx[now]];}else if (gy[now] != 0){p[gy[now]].x = w, p[gy[now]].y = y[i].pos;gy[now] = fy[gy[now]];}else { p[y[i].id].x = w, p[y[i].id].y = y[i].pos; }}for (i = 1;i <= n;i++)printf("%d %d\n", p[i].x, p[i].y);return 0;}
- codeforces 848B
- codeforces 848B :计数
- codeforces B
- codeforces B
- codeforces B
- codeforces B
- Codeforces 848B Rooter's Song
- CodeForces 626B CodeForces 626B【暴力】
- CodeForces 841B (B) 博弈
- codeforces 134B
- codeforces#98 b
- codeforces 105 div2 B
- Codeforces 166B - Polygons
- codeforces B. Coins
- codeforces----193B Xor
- codeforces----208B Solitaire
- Codeforces 1B - Spreadsheet
- codeforces 214B Hometask
- python字符串与文件的操作
- dijkstra算法入门
- java的初始化块、静态初始化块、构造函数的执行顺序
- 51nod 1416
- 【Zigbee技术入门教程-02】一图读懂ZStack协议栈的基本架构和工作机理
- codeforces 848B
- 总线基础--SPI总线
- C#笔记(4)保存数据到文件,从文件读取数据
- N级指针
- Lintcode-递归-93 平衡二叉树
- 事件分发机制
- mysql集群搭建
- lua和c++全部源码
- 面试OR笔试27——位运算做加法