We are asked: For each test case, given N reservations with arrival and departure times, find the maximum number of guests simultaneously present. Note: if a guest arrives at the same time another leaves, they are never considered simultaneously.
Interpretation: Typical interval overlapping problem, but with exclusive end: a guest is present from arrival inclusive to departure exclusive? Actually the statement: "if one guest arrives at the same time another leaves, they are never considered to be at the hotel simultaneously". That implies that at the exact time of departure, the departing guest is no longer present, and at the exact time of arrival, the arriving guest becomes present after the departure has already left? Usually we treat intervals as [arrival, departure) if we consider departure as leaving exactly at that time and arrivals happening at that time after departures. So the simultaneous count at time t: number of guests with arrival <= t and departure > t? But careful: The note says: "if one guest arrives at the same time another leaves, they are never considered to be at the hotel simultaneously". This means at a time equal to both arrival and departure of two guests, we should count only those who are present excluding those who just left and those who just arrived? Actually typical approach: sort events, treat departure before arrival to avoid counting overlapping at a point. E.g., sample 2: N=5, arrivals: 1,2,3,4,5; departures: 2,3,4,5,6. The answer is 1. Let's examine: At time 1, only guest1 present. At time 2, guest1 departs at time2, guest2 arrives at time2; they are not simultaneous, so at time2 only guest2? Actually at the exact moment 2, we consider that guest1 already left (departure time2) and guest2 arrives (arrival time2) but they don't overlap. So count at time2 is 1? Or 0? Let's think: The typical solution for max overlapping intervals with exclusive end is to treat departure as -1 after arrival, or to sort events with departure before arrival when times equal. Then we compute max concurrent. That yields answer 1. That matches.
Given constraints: N <= 100, times <= 1000. So we can simply create array of size maybe up to 1000? But times might be up to 1000, so we can do sweep over timeline. Or sort events.
Approach: For each test case, read N, then list arrivals, list departures. Then combine into events: each arrival: +1; each departure: -1. Sort events by time; if times equal, process departures before arrivals (since depart then arrive). Then iterate through sorted events, maintaining current count, track max.
Alternatively, because times are small, we could create an array of size 1001 (or 2000?) and for each guest, increment counts for each time unit they are present? But careful: With exclusive endpoints, if we treat time as integer moments, we might need to define presence on integer times. However the note says if arrival equals departure, they are not simultaneously present. Since all departure > arrival, but times are integers. Could presence be considered at any real time? Usually this is a classic problem: find maximum number of overlapping intervals with exclusive boundaries when endpoints coincide. The event-based approach works.
Given constraints, we can implement event sorting: Create list of pairs (time, type) where type = 0 for departure (or -1) and 1 for arrival. Then sort by time, with type (departure before arrival). But we need to be careful: When we sort, we want for same time: departure first (so we subtract before adding). So we can assign type order: departure = 0, arrival = 1? Actually we want to process -1 before +1. So we can store (time, delta) with delta = -1 for departure, +1 for arrival. Then sort by time, and for same time, sort by delta (negative first). Since -1 < +1, it will be processed first automatically if we sort by (time, delta). That works.
Then initialize current = 0, max = 0. For each event: current += delta; after processing each event, update max = max(current, max). Note: If we process a departure (-1) then arrival (+1) at same time, the count after departure might go down, then after arrival go up. The max might be captured after arrival. That's correct.
Let's test sample 1: N=3, arrivals:1,2,3; departures:4,5,6. Events: (1,+1),(2,+1),(3,+1),(4,-1),(5,-1),(6,-1). Sorted: times distinct; current increments to 1,2,3 then decrements. Max=3. Sample2: arrivals:1,2,3,4,5; departures:2,3,4,5,6. Events: (1,+1),(2,+1),(3,+1),(4,+1),(5,+1),(2,-1),(3,-1),(4,-1),(5,-1),(6,-1). Sorting: time1: +1 => cur=1 max=1. time2: we have (2,-1) then (2,+1). After -1: cur=0 max stays 1? Actually we need to update max after each event. After -1, cur=0, max=1; after +1, cur=1, max=1. time3: -1 then +1: cur goes 0 then 1. time4: -1 then +1: cur 0->1. time5: -1 then +1: cur0->1. time6: -1: cur=0. So max remains 1. So answer 1. Sample3: N=7: arrivals:13,6,5,8,2,10,12; departures:19,18,6,9,9,11,15. Let's compute. We'll test later.
Thus solution works.
Edge Cases: N=0? Possibly? Not mentioned, but N is positive integer? "Each test case begins with an integer N, the number of guests." Could be 0? But constraints? Probably N>=1. But just in case, handle N=0: max=0.
Implementation: read T. For each test case, read N. Then read line of N integers for arrivals; but input format: "Two lines follow, each with exactly N positive integers." So we can read next line(s) containing numbers. However input may be spaced across lines arbitrarily? It says exactly two lines, each with N integers. So we can just read N integers from next line(s). But careful: There might be multiple lines due to previous newline etc. Use typical reading via Scanner or input stream.
In C, we can read integers with scanf.
We'll implement in C: For each test case:
- read N.
- allocate arrays for arrivals (size N) and departures (size N).
- read next N integers: for i=0 to N-1: scanf("%d", &arr[i]).
- read next N integers: for i=0 to N-1: scanf("%d", &dep[i]).
- create events: array of size 2N; for i: events[2i] = (arr[i], +1); events[2*i+1] = (dep[i], -1).
- sort events by time; if times equal, sort by delta ascending (i.e., -1 before +1). In C, use qsort with comparator:
int cmp(const void* a, const void* b) {
struct event* e1 = (struct event*)a;
struct event* e2 = (struct event*)b;
if (e1->time != e2->time) return e1->time - e2->time;
return e1->delta - e2->delta; // because -1 < +1, so departures come first.
}
- Then sweep.
Complexities: O(N log N) per test case, fine.
But N ≤ 100, times ≤ 1000, so we could also use array of size maybe 1001 and do prefix sum approach with careful handling of inclusive/exclusive. That is: create an array timeline[1002] initially zero. For each guest: timeline[arrival] += 1; timeline[departure] -= 1? But careful: At exact time departure, the guest is no longer present. So if we treat timeline as instantaneous at integer times, we need to decide when the guest is counted. Usually to get correct max for discrete times, we might do: For each time t, count = sum_{i <= t} timeline[i] where timeline[arrival] +=1, timeline[departure] -=1, but then at time = departure, the -1 has been applied, so the guest is not counted at that integer time. However arrival at same time as departure? Not possible because departure > arrival. But if we consider the time as continuous, the max may occur at a time between integers. But using the event method with sweep on sorted events yields correct max over continuous time, given that events only at integer times but we treat the timeline between them as constant. Actually with event sweep we consider the points between events. The max occurs at a time just after an arrival or just before a departure. Since arrivals and departures are at integer times, but we need to respect that at the moment of arrival after the event? In our sweep we update count after processing an event. For an arrival at time t, we add +1, so after processing that event, count is incremented, representing the count just after time t (or at time t+epsilon). For a departure at time t, we subtract -1, and after processing, count is decremented, representing after time t (i.e., from t to next event). So this correctly models [arrival, departure) intervals. Thus max obtained is correct.
So both approaches work. But simpler: sorting events.
Now let's check sample3 manually:
N=7
Arrivals: 13,6,5,8,2,10,12
Departures:19,18,6,9,9,11,15
Let's list intervals:
1: [13,19)
2: [6,18)
3: [5,6) (arr5 dep6, note departure > arrival; departure at 6, so presence until just before 6)
4: [8,9)
5: [2,9)
6: [10,11)
7: [12,15)
Now we can compute overlapping:
At time intervals:
- [2,5): only guest5? Actually guest5 arrives at 2, dep at 9. So count=1.
- At time 5: guest3 arrives at 5, dep at 6: so from 5 to 6: guests 5 and 3 => 2.
- At time 6: guest3 dep at 6, guest2 arrives at 6? Actually guest2 arrival 6. So at 6, guest3 leaves, guest2 arrives. So simultaneous count after 6? From just after 6 until next event: guests: guest5 (2-9), guest2 (6-18) => 2. Also guest? Possibly others? Let's continue.
- At time 8: guest4 arrives at 8, dep at 9: so from 8 to 9: guests: guest5, guest2, guest4 => 3.
- At time 9: guest4 dep at 9, guest5 dep at 9? Actually guest5 dep at 9 as well. So both depart at 9. So at time 9, after departures, those guests leave. Also maybe other events? So from just after 9 until next event (10): only guest2 remains? So count=1.
- At time 10: guest6 arrives at 10, dep at 11: so from 10 to 11: guests: guest2, guest6 => 2.
- At time 11: guest6 dep at 11: so from 11 to 12: only guest2 =>1.
- At time 12: guest7 arrives at 12, dep at 15: so from 12 to 15: guests: guest2, guest7 =>2.
- At time 13: guest1 arrives at 13, dep at 19: so from 13 to 15: guests: guest2, guest7, guest1 =>3.
- At time 15: guest7 dep at 15: so from 15 to 18: guests: guest2, guest1 =>2.
- At time 18: guest2 dep at 18: from 18 to 19: only guest1 =>1.
- At time 19: guest1 dep.
Maximum appears to be 3 (at times 8-9 and 13-15). So answer is 3, matches sample.
Thus event method works.
Implementation details:
- Define struct event { int time; int delta; };
- Allocate array of size 2*N (max 200). Use qsort.
- Then iterate.
Alternative: Since times are small (max 1000), we could do a sweep array of size 1001 for the continuous approach using prefix sums but careful about boundaries. Actually we can create an array cnt[1002] = {0}. For each guest, do cnt[arrival]++; cnt[departure]--; Then compute prefix sum over t=1..1000 and track max prefix sum. However is that exactly correct? Let's test sample2: arrivals:1,2,3,4,5; departures:2,3,4,5,6. Then cnt[1]++, cnt[2]--, cnt[2]++, cnt[3]--, cnt[3]++, cnt[4]--, cnt[4]++, cnt[5]--, cnt[5]++, cnt[6]--. So combined: cnt[1]=1, cnt[2]=0 (since -1+1=0), cnt[3]=0, cnt[4]=0, cnt[5]=0, cnt[6]= -1? Wait compute: Actually arrivals: at 2: +1; at 3:+1; at4:+1; at5:+1; departures: at2:-1;3:-1;4:-1;5:-1;6:-1. So net: time1:1; time2:1-1=0? But we have +1 and -1 at same time 2; if we sum them into a single array, we are effectively doing both at same index. The prefix sum at time t is sum_{i<=t} cnt[i]. At time 1: sum=1. At time2: sum=1+ (cnt[2]) = 1+0=1. But that would indicate count=1 at time2, which is not correct because at time2 exactly, we want count=1? Actually according to the problem, at time2, one guest left and another arrived, they are not simultaneously present, but the count just after time2 (i.e., for t in (2,3)) is 1. The prefix sum method yields 1 at time2, but is that representing count at integer time 2? If we treat intervals as [arrival, departure), then the count is constant on [arrival, departure). The prefix sum approach using cnt[arrival]++, cnt[departure]-- yields that for any t, the count at time t (considered as integer point) equals sum_{i<=t} cnt[i] but that count includes the arrival at time t but excludes departure at time t? Actually if we define presence as [arrival, departure), then the contribution of a guest should be +1 for times t where arrival <= t < departure. Then the prefix sum technique with increments at arrival and decrements at departure (with departure at the end) yields exactly that: for integer t, if we compute prefix sum up to t, we have included +1 for arrivals at <=t, and included -1 for departures at <=t, so for a guest with arrival a and departure d, its contribution to prefix sum at t is 1 if a <= t and d > t? Actually if we include both events at times a and d, then the prefix sum at t is sum of events with time <= t. If a <= t, the +1 is included. If d <= t, the -1 is included. So the net contribution is +1 only if a <= t and d > t (because if d <= t then both +1 and -1 are included, net 0). If d > t, then -1 not included, so net +1. So the prefix sum at t equals the number of guests with a <= t < d. That's exactly the count at integer time t (including both arrival and departure at the same time? Consider t = a = d? impossible). But for t = a, the condition a <= t is true, and d > a is true (since d > a), so the guest is counted. So at integer time equal to arrival, the guest is considered present. At t = d, condition d > t is false, so not counted. So prefix sum gives count for integer times. However the problem says that if one guest arrives at the same time another leaves, they are not simultaneously present. But at integer time points, the arriving guest would be counted and the leaving guest would not, resulting in count possibly staying same? Actually for sample2 at time2: Guest1 departure at 2, guest2 arrival at 2. According to prefix sum at time2: count includes guest1? Guest1: a=1, d=2. For t=2, condition: a<=2 true, d>2 false, so not counted. Guest2: a=2, d=3. a<=2 true, d>2 true, so counted. So count = 1. That's the count at integer time 2, which matches that after the events, guest2 is present. But does the problem consider that at time 2 exactly there is one guest? It says "if one guest arrives at the same time another leaves, they are never considered to be at the hotel simultaneously." That doesn't say there is no guest at that instant; it just says the two are not simultaneously present. The arriving guest is present (since he arrives at that time) and the departing guest is not present (since he leaves at that time). So the count at the instant of time 2 would be 1. So prefix sum at integer times yields exactly the correct count? But wait, consider the maximum number of guests simultaneously present. This maximum could occur at a non-integer time if arrivals and departures happen at the same integer time. For instance, if two guests have intervals [1,2] and [2,3] (with exclusive departure), then at time exactly 2, count is 1 (only second). But if we had a scenario where many arrivals and departures happen at the same time, the maximum might be just after that time? Let's think: Suppose three guests: A: [1,2], B: [2,3], C: [2,4]. At time 2, A leaves, B and C arrive. The count just before time2 is 1 (A). Just after time2 (e.g., 2.0001) count is 2 (B and C). So the maximum might be 2. The prefix sum at integer time 2 gives count = 2? Actually compute: A: a=1,d=2; B: a=2,d=3; C: a=2,d=4. At t=2: A: not counted; B: counted; C: counted => total 2. So it still yields 2. That's because at t=2 (integer), B and C are counted, A is not. So it counts the arriving guests but not the departing ones. That matches the state just after the time point? Actually the integer time 2 could be interpreted as the moment exactly at 2, but the condition for counting is a <= t and d > t. For A, d=2, so d > t is false; so A not counted. So A left exactly at 2? But we treat that as not present at 2. For B and C, they arrive at 2, and we count them at 2 because a <= t is true and d>t is true. So we consider them present at time 2. That implies that at the exact moment 2, the arrivals have already happened and the departure has already left. But is that consistent with "they are never considered to be at the hotel simultaneously"? If A leaves at 2 and B arrives at 2, they are not simultaneously present. If we say B is present at 2 and A is not, that's consistent because they are not both present. So it's okay. The subtlety is: what if we have many arrivals at the same time and many departures at the same time? Suppose we have 5 guests depart at 2 and 5 guests arrive at 2. The maximum simultaneous might be the sum of those remaining plus new arrivals after 2. The count at integer time 2 will be the number of arrivals (since departures are not counted) plus any guests that were already present before 2 and have departure >2. That exactly captures the number after processing both events? But careful: In the real continuous timeline, at time just before 2, we have some count (including those who depart at 2). At time exactly 2, the departures have left, then arrivals happen (or maybe arrivals happen exactly at 2). The instantaneous state at time 2 (after events) includes arrivals and excludes departures. So the count at time 2 (after events) equals the prefix sum at integer 2. So the maximum over all real times is essentially the maximum over the left-closed intervals? Actually the count is piecewise constant on intervals between event times. The maximum can occur just after an arrival or just before a departure. But because we treat arrivals as making the guest present from the arrival time onward, the count just after arrival (i.e., (arrival, next event)) equals the count at the arrival integer time (if we consider it after adding). The count just before departure equals the count at the departure integer time minus 1? Actually at a departure time d, the guest is still present just before d. The count just before d is the count that includes that guest. At integer time d, we exclude it. So the count just before d is not captured by prefix sum at integer d. So if the maximum occurs just before a departure, it might be higher than any integer time count. Consider a scenario: one guest from 1 to 3, and another from 2 to 3? Actually both depart at same time? Let's think: Guest1: [1,4], Guest2: [2,4]. At time just before 4, both present => count=2. At time 4, both have left, count=0. The prefix sum at integer 4 is 0, at integer 3 is 2 (since both present at 3). So max is 2. So okay.
What about a scenario where the maximum occurs in an interval (a,b) where no integer point inside? Actually if a and b are integers, there will be integer points inside (since interval length >0). For exclusive endpoints, if a and b are consecutive integers, e.g., [1,2) then there is no integer strictly between 1 and 2? Actually the open interval (1,2) doesn't contain an integer, but the interval includes all times from 1 inclusive to 2 exclusive. That includes the integer 1? Because 1 is included. So at time 1, the guest is present. So there is an integer point. So any interval of length >=1 includes at least its left endpoint integer. So maximum will be captured at some integer time? Not necessarily: consider intervals that start and end at non-integer times? But in our problem, times are integers. So all event times are integers. The presence intervals are [arrival, departure) with integer endpoints. The set of times includes all real numbers, but the count is constant on each half-open interval between consecutive integer event times? Actually events occur at integer times. Between integer times, say (k, k+1), there are no events. The count on that interval is the same as at integer time k+epsilon? But is that equal to the count at integer k+1? Possibly not because at integer k+1 there might be arrivals or departures. The count just after k+1 (i.e., in (k+1, k+2)) equals the count after processing events at k+1. The count just before k+1 (i.e., in (k, k+1)) equals the count just after processing events at k (i.e., at integer k after arrivals/departures at k). So the count on the interval (k, k+1) is the same as at integer k (if we consider the state after events at k). However, the count at integer k is defined as after events at k? In our prefix sum definition, at integer t, we count guests with a <= t < d. That is the state after arrivals at t (since they are included) and before departures at t (since they are excluded). That's exactly the state for times in (t, t+1) (i.e., after t but before the next integer event). So the count on (t, t+1) equals prefix sum at t. So the maximum over all real times is max over integer t of prefix sum at t. Thus prefix sum method works.
Wait: need to confirm: At time exactly t (integer), do we consider that arrivals at t have already occurred? Our prefix sum definition includes arrivals at t because a <= t. It excludes departures at t because d > t, so departures at t are not counted. That matches state at time t after processing arrivals and before processing departures? Actually we need to be careful about the order: The statement: "if one guest arrives at the same time another leaves, they are never considered to be at the hotel simultaneously." This implies that at the exact same time, the departing guest is considered gone and the arriving guest is considered present. So the state at that exact time should reflect that the departure has already happened and the arrival has already happened. That is exactly what our prefix sum at integer t does: excludes departure at t, includes arrival at t. So it's correct.
Now consider a scenario: multiple departures and arrivals at same time. The prefix sum at t includes all arrivals at t, excludes all departures at t. That's correct.
Thus we can do a simple array of size maybe up to 1000+1 and for each guest: ++cnt[arrival]; --cnt[departure]; Then compute cumulative sum for t from 1 to max_time (maybe 1000) and track max. Since times are positive up to 1000. That is O(N + max_time) per test, which is trivial. However need to consider if times can be 0? Not, they are positive. So start from 1.
But careful: We must include times up to 1000 inclusive. At departure time, we decrement. The cumulative sum at time t gives count for t as described. However what about t=0? Not needed. Also what about times beyond max departure? Not needed. So we can create an array of size 1001+? Since times are up to 1000, we need indices up to 1000. But we need to do prefix sum over all times. Also note that we might need to consider times like 1000 for departure, but no need to consider beyond.
But there is a nuance: The count for times in (t, t+1) equals prefix sum at t if we consider cumulative sum inclusive? Let's test: With intervals: guest from 1 to 3. arrival=1, departure=3. Then cnt[1]++, cnt[3]--. Compute prefix sum for t=1: sum=1 (count at t=1). For t=2: sum=1 (since cnt[2]=0). For t=3: sum=0 (since -1 applied). So the prefix sum is 1 for t=1 and 2. That matches presence on [1,3). Yes.
Now test sample2: arrivals:1,2,3,4,5; depart:2,3,4,5,6.
cnt: inc at 1,2,3,4,5; dec at 2,3,4,5,6.
prefix sum: t=1:1; t=2: cnt[2] inc+dec = (1-1)=0? Actually net at 2: inc and dec, but they are both separate events, if we sum into one array we need to add both contributions: we can do cnt[arr]++; cnt[dep]--; That means cnt[2] gets +1 and -1 = 0 net? Actually that's the sum; but if we add them sequentially, it's fine: we just do for each guest: cnt[arrival] +=1; cnt[departure] -=1; So for guests: guest1: cnt[1]++, cnt[2]--; guest2: cnt[2]++, cnt[3]--; guest3: cnt[3]++, cnt[4]--; guest4: cnt[4]++, cnt[5]--; guest5: cnt[5]++, cnt[6]--. After all, cnt[1]=1; cnt[2]= -1+1? Actually net: cnt[2] gets -1 from guest1 and +1 from guest2 => 0? Wait: It gets -1 then +1 overall net 0. Similarly cnt[3]=0, cnt[4]=0, cnt[5]=0, cnt[6]= -1. Then prefix sum: t=1:1; t=2:1+0=1; t=3:1+0=1; t=4:1+0=1; t=5:1+0=1; t=6:1-1=0. So prefix sum gives 1 for t=1..5, and 0 at t=6. That yields max=1. But is that correct? At t=5, we have guest5 present? guest5 arrival=5, departure=6, so at t=5, present; also guest4 departed at 5? guest4 departure=5, so not present. So count=1. Good. However, what about t=2? At t=2, guest2 present? yes; guest1 left at 2, so not. So count=1. So prefix sum says count=1 at t=2. But is that the maximum? The maximum is 1. So it's fine.
But note: The prefix sum method yields the same as event sweep but maybe simpler. However, there's a subtle point: In the prefix sum method, we are effectively assuming that arrivals and departures at the same time cancel in the array; but the cumulative sum at that time includes both the decrement and increment at that same index? Let's simulate with separate events at same time: if we just do cnt[arr]++, cnt[dep]--, we are adding both to the same index. Then when we compute prefix sum, we are effectively applying both changes at the same time point simultaneously. The cumulative sum after processing index t includes both the increment and decrement. That yields the count after both have been applied. That's exactly what we want: departures and arrivals at same time result in departure removed and arrival added. So it's fine.
But caution: If we have multiple guests arriving and departing at same time, the order within the same index doesn't matter because the net effect is the sum. The cumulative sum will correctly reflect the net change.
Thus the prefix sum method is valid and simpler, given integer times up to 1000.
But is there any case where the maximum occurs at a time that is not an integer and not captured by prefix sum? For integer endpoints intervals, the count is constant on intervals between integer event times, and these intervals correspond to [t, t+1) where t is an integer event time after processing events at t. So the count on (t, t+1) equals the cumulative sum at t (since after processing all events at t). The maximum over all real times is the maximum over t (integer) of that cumulative sum. So yes.
Thus we can just compute prefix sum.
Edge Cases: times up to 1000, but departure times strictly > arrival, so maximum departure could be 1000. So we need array up to 1000 maybe 1001 to include index 1000 for departure, and we need to compute up to 1000. However, what about time 0? not used.
Implementation with array size MAX_T = 1000+2 maybe to be safe. We'll use 1005.
Algorithm per test case:
- initialize array cnt[1005] = {0}
- for i in 0..N-1:
read a, d? Actually we have two separate lines: first line arrivals, second departures. So we need to store arrivals first then departures? We can read into two arrays or directly process: first read arrivals into array arr[N]; then read departures into array dep[N]; then for i: cnt[arr[i]]++; cnt[dep[i]]--; Or we can just process while reading but it's fine.
- After reading all, compute cur = 0, max = 0; for t from 1 to 1000 (or maybe to max_time which we could determine to speed up, but it's small): cur += cnt[t]; if cur > max: max = cur;
- print max.
Check sample3: We'll compute quickly: cnt array: indices 2 to 19. We'll simulate later.
Potential pitfalls: The input might have numbers separated by spaces and newlines. We'll just use scanf to read integers sequentially.
Time complexity: T up to 100, N up to 100 each, array up to 1000, fine.
Now confirm with sample3 using prefix sum method:
Guests:
1: arrival 13, dep19: cnt[13]++, cnt[19]--
2: arr6, dep18: cnt[6]++, cnt[18]--
3: arr5, dep6: cnt[5]++, cnt[6]--
4: arr8, dep9: cnt[8]++, cnt[9]--
5: arr2, dep9: cnt[2]++, cnt[9]--
6: arr10, dep11: cnt[10]++, cnt[11]--
7: arr12, dep15: cnt[12]++, cnt[15]--
Now compute cnt array from 1 to 19:
Initialize all 0.
After all:
idx2: +1
idx5: +1
idx6: +1 (from guest2) and -1 (from guest3) => net 0? Actually guest2: +1 at 6; guest3: -1 at 6; so net 0. So cnt[6]=0.
But careful: also maybe other? Only those.
idx8: +1
idx9: -1 (from guest4) and -1 (from guest5) => -2
idx10: +1
idx11: -1
idx12: +1
idx13: +1
idx15: -1
idx18: -1
idx19: -1
Also idx? idx? maybe idx? Let's list systematically:
- Guest1: +13, -19
- Guest2: +6, -18
- Guest3: +5, -6
- Guest4: +8, -9
- Guest5: +2, -9
- Guest6: +10, -11
- Guest7: +12, -15
Now combine by index:
2: +1 (guest5)
5: +1 (guest3)
6: +1 (guest2) -1 (guest3) = 0? Actually net: +1 from guest2, -1 from guest3 => 0
8: +1 (guest4)
9: -1 (guest4) -1 (guest5) = -2
10: +1 (guest6)
11: -1 (guest6)
12: +1 (guest7)
13: +1 (guest1)
15: -1 (guest7)
18: -1 (guest2)
19: -1 (guest1)
Also indices 1,3,4,7,14,16,17, etc are 0.
Now compute cumulative sum from 1 to 19:
cur=0
t=1: cur=0, max=0
t=2: cur+=1 =>1, max=1
t=3: cur=1, max=1
t=4: cur=1
t=5: cur+=1 =>2, max=2
t=6: cur+=0 =>2 (since cnt[6]=0) (but is it correct? At t=6, should be 2? Let's check: after processing events at 6, we have arrivals and departures: guest3 dep at 6 ( -1 ), guest2 arr at 6 ( +1 ). Net change 0. So after t=6, count remains 2? Wait before t=6 (i.e., after t=5 up to 6), count was 2 (guests5 and guest3). At time 6 events: guest3 dep, guest2 arr. So after events, guests present: guest5 (since dep at 9), guest2 (arr at 6). So count=2. So cumulative sum at t=6 should be 2. Our cur after adding cnt[6]=0 remains 2, correct. So max=2.
t=7: cur=2 (cnt[7]=0)
t=8: cur+=1 =>3, max=3
t=9: cur+=(-2) =>1, max remains 3
t=10: cur+=1 =>2
t=11: cur+=(-1) =>1
t=12: cur+=1 =>2
t=13: cur+=1 =>3, max=3
t=14: cur=3 (cnt14=0) (should be 3? Actually from 13 to 15: guests: guest2, guest7, guest1. So count 3. Yes, max stays 3.
t=15: cur+=(-1) =>2
t=16: cur=2
t=17: cur=2
t=18: cur+=(-1) =>1
t=19: cur+=(-1) =>0
So max=3. Good.
Thus prefix sum works.
Hence we have two simple implementations. Since constraints are tiny, either is fine.
But is there any case where prefix sum might be off? Suppose a guest arrives at time 1 and departs at time 1? Not allowed because departure strictly greater.
Thus it's safe.
Implementation plan: read T. For each test case: read N. Then read N integers into arr. Then read N integers into dep. Create cnt array of size maybe 1002 (since times up to 1000, but we need index up to 1000 inclusive; we can also find max_time to limit loop). We'll just loop from 1 to 1000 inclusive. Initialize cnt[0..1000] = 0. For each i: cnt[arr[i]]++; cnt[dep[i]]--. Then compute cur=0, ans=0; for time = 1 to 1000: cur += cnt[time]; if cur > ans: ans = cur; print ans.
One more check: Input format: The second line has exactly N integers. However, there might be extra spaces or lines? But we can just read all numbers using scanf("%d", &x). So we can read arrivals and departures sequentially: after reading N, we can read next N numbers into an array for arrivals, then next N numbers for departures. That's fine.
Edge Cases: Times might be 0? No, positive.
Thus answer.
Now test with sample input:
3
3
1 2 3
4 5 6
5
1 2 3 4 5
2 3 4 5 6
7
13 6 5 8 2 10 12
19 18 6 9 9 11 15
Our algorithm yields:
Case1: cnt[1]++, cnt[4]--; cnt[2]++, cnt[5]--; cnt[3]++, cnt[6]--; cum sum: t1:1, t2:2, t3:3, t4:2, t5:1, t6:0 -> max3.
Case2: as above -> max1.
Case3: ->3.
Matches.
Now we need to produce final code. Since the problem likely from some judge, typical solution in C.
We must be careful: The input might have multiple test cases, and each test case's lines are separate. So reading with scanf is fine.
Complexity: O(T * (N + 1000)).
Now, what about the scenario where times are up to 1000, but maybe larger? The problem says between 1 and 1000 inclusive. So we can hardcode 1000.
But to be safe, we could find the maximum time among arrivals and departures and loop only to that. But not necessary.
Memory: small.
Thus final code.
Now we should write code with include stdio.h, maybe define MAX 1000. Or use 1005.
Implementation:
#include <stdio.h>
#include <string.h>
#define MAX_TIME 1000
int main() {
int T;
scanf("%d", &T);
while (T--) {
int N;
scanf("%d", &N);
int arr[N], dep[N];
for (int i = 0; i < N; i++) scanf("%d", &arr[i]);
for (int i = 0; i < N; i++) scanf("%d", &dep[i]);
int cnt[MAX_TIME+2]; // maybe up to 1000+ something
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < N; i++) {
cnt[arr[i]]++;
cnt[dep[i]]--;
}
int cur = 0, max = 0;
for (int t = 1; t <= MAX_TIME; t++) {
cur += cnt[t];
if (cur > max) max = cur;
}
printf("%d\n", max);
}
return 0;
}
We need to ensure that MAX_TIME is at least the maximum possible time, which is 1000. So loop to 1000 inclusive.
But note: if a departure time is 1000, we decrement at index 1000. The cumulative sum at t=1000 will include that decrement. But what about times after 1000? No guests present. So max captured earlier.
Now, is there any off-by-one if a guest arrives at 1000 and departs at something >1000? Not possible because departure <=1000? Actually constraints say up to 1000 inclusive. So departure can be 1000? But arrival times also up to 1000, and departure strictly greater, so maximum departure could be 1000 only if arrival <1000, say arrival=999, departure=1000. That's allowed. So we need to include index 1000. Our loop goes to MAX_TIME (1000) inclusive, so includes that decrement. Good.
But what about arrivals at 1000? Could be if arrival=1000, departure must be >1000, but maximum is 1000 per constraints, so arrival=1000 with departure>1000 not allowed because departure max is 1000. So arrival=1000 not possible because then departure would be >1000, exceeding max. So maybe arrival max is 999? Actually constraints: both arrival and departure between 1 and 1000 inclusive, and departure strictly greater than arrival. So if arrival=1000, the smallest departure is 1001, which exceeds 1000, not allowed. So arrival cannot be 1000. Similarly departure can be 1000, but arrival must be <1000. So we will have some index 1000 with -1, but no +1 at 1000. So okay.
Now confirm that the prefix sum method correctly captures the maximum when events occur at same time. Already done.
Thus solution is done.
Now produce final answer with explanation.
But the problem statement says: "Note that if one guest arrives at the same time another leaves, they are never considered to be at the hotel simultaneously (see the second example)." This matches our modeling.
Therefore final answer: provide code and explanation.
We'll also consider alternative using sorting if needed but simpler is prefix sum.
Now produce final output. ```c
#include <stdio.h>
#include <string.h>
#define MAX_TIME 1000 // times are in [1, 1000]
int main() {
int T;
scanf("%d", &T);
while (T--) {
int N;
scanf("%d", &N);
int arrival[N], departure[N];
for (int i = 0; i < N; ++i) scanf("%d", &arrival[i]);
for (int i = 0; i < N; ++i) scanf("%d", &departure[i]);
int cnt[MAX_TIME + 2]; // extra cell for safety
memset(cnt, 0, sizeof(cnt));
// mark arrivals (+1) and departures (-1)
for (int i = 0; i < N; ++i) {
cnt[arrival[i]]++;
cnt[departure[i]]--;
}
int cur = 0, ans = 0;
for (int t = 1; t <= MAX_TIME; ++t) {
cur += cnt[t];
if (cur > ans) ans = cur;
}
printf("%d\n", ans);
}
return 0;
}