描述
Steven loves swimming! The swimming pool he goes to every afternoon consists of N parallel lanes which are number from 0 to N-1. Steven plans to swim for R rounds. Each round he chooses a lane and swim to the other side, spending exactly L units of time.
Since the weather is getting hotter, the swimming pool is very crowded. To prevent from accidents, the swimming pool manager asks everyone to submit their plans before swimming. A plan contains specific informations for a round, including t - the time he starts, l - the time he spends, n - the lane he chooses, d - the direction he swims(0 for swimming from west to east and 1 for swimming from east to west). Everyone starts at some integeral time and swims at a constant speed.
The plans should not conflict with each other: two swimmers are not allowed to meet in the middle of a lane (meeting at the sides is allowed).
Steven arrives at time T and gets Q plans of the other swimmers. So here's the question, what's the earliest time that Steven can finish his swimming plans?
Note that Steven starts from the west side and he moves from one side to the other only by swimming.
输入
Input contains multiple testcases.
The first line is an integer TASKS, representing the number of testcases.
For each testcase, the first line contains five integers T, L, R, N and Q which are described above.
The following Q lines describe the plans, each line with four integers, t, l, n, d, which are described above.
For all testcases:
0 <= T, t <= 104
1 <= L, l <= 104
1 <= R <= 104
0 <= Q <= 104
1 <= N <= 103
输出
The earliest time that Steven can finish his swimming plans.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Event{
int t;
int x;
int n;
}Event;
Event event[2][20000];
int count[2];
void AddEvent(int t, int x, int n, int d){
event[d][count[d]].t = t;
event[d][count[d]].x = x;
event[d][count[d]].n = n;
count[d]++;
}
int comp(const void* a, const void* b){
Event* c = (Event*)a;
Event* d = (Event*)b;
if(c->t != d->t)
return c->t - d->t;
else
return c->x - d->x;
}
int main(){
int TASK, T, L, R, N, Q;
int t, l, n, d;
int i, D, find, point[2], lane[2][1000];
scanf("%d", &TASK);
while(TASK--){
count[0] = 0;
count[1] = 0;
scanf("%d%d%d%d%d", &T, &L, &R, &N, &Q);
for(i=0; i<Q; i++){
scanf("%d%d%d%d", &t, &l, &n, &d);
if(l < L){
AddEvent(t-L+l, 1, n, d);
AddEvent(t, -1, n, d);
}else if(l == L){
AddEvent(t-1, 1, n, d);
AddEvent(t+1, -1, n, d);
}else{
AddEvent(t, 1, n, d);
AddEvent(t+l-L, -1, n, d);
}
AddEvent(t-L, 1, n, 1-d);
AddEvent(t+l, -1, n, 1-d);
}
qsort(event[0], Q*2, sizeof(Event), comp);
qsort(event[1], Q*2, sizeof(Event), comp);
D = 0;
point[0] = 0;
point[1] = 0;
memset(lane, 0, sizeof(lane));
while(R){
find = 0;
while(point[D] < 2*Q){
if(event[D][point[D]].t < T){
lane[D][event[D][point[D]].n] += event[D][point[D]].x;
point[D]++;
}else{
for(i=0; i<N; i++){
if(lane[D][i] == 0){
find = 1;
T += L;
break;
}
}
if(find)
break;
T = event[D][point[D]].t;
lane[D][event[D][point[D]].n] += event[D][point[D]].x;
point[D]++;
}
}
if(!find)
T += L;
D = 1 - D;
R--;
}
printf("%d\n", T);
}
return 0;
}
这道题A得好辛苦,memset那句,用sizeof(int)*2*N就会出错
今天先不管了,明天再想
有人能给解释下吗?