codeforces 848B - Rooter's Song 瞎搞
2017-09-02 20:43
330 查看
B. Rooter's Song
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
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: 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
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.
Examples
Input
Output
Input
Output
先看一张图
假设每个交点都能相撞,规律就很明显了。
首先分别记录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;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
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: 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
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.
Examples
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
Output
4 8 10 5 8 8 10 6 10 2 1 8 7 8 10 6
Input
3 2 3 1 1 2 2 1 1 1 1 5
Output
1 3 2 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 849 D. Rooter's Song (技巧)
- Codeforces 527 C Glass Carving----瞎搞
- CodeForces 398A Cards 贪心 暴力 瞎搞
- Codeforces 876E:2-SAT 或者 瞎搞
- codeforces 848B :计数
- codeforces - 738B - Spotlights (瞎搞)
- codeforces 848B Rooter's Song
- CodeForces 723F【DFS瞎搞】
- 【瞎搞】 Codeforces 460D Little Victor and Set 位运算构造
- Codeforces Round #431 (Div. 1) B. Rooter's Song(模拟)
- Codeforces Round #431 (Div. 2) (Codeforces 849D) D. Rooter's Song
- CodeForces 489A (瞎搞) SwapSort
- codeforces 527B:瞎搞
- 【Codeforces】-#222A-KShooshuns and Sequence(思维)
- CodeForces 723B-Text Document Analysis(模拟)
- Codeforces 149C Division into Teams【贪心】
- 【CodeForces 788B】奇妙的一笔画问题
- codeforces 145E - Lucky Queries
- Codeforces 526D - Om Nom and Necklace 【KMP】
- CodeForces 686A