← 返回 stripe 的题目列表Datacenter Router Command Processor
类型:online_judge
Datacenter Router: Command Processor
Implement process_commands(commands) to process a list of command strings in order and return one output string per command.
Input: commands is an array of strings, one command per line.
Output: an array of strings, one result per command in order.
Maintain a set of datacenters with fields:
name (unique)
lat (latitude)
lon (longitude)
capacity (positive integer)
healthy (initially true)
load (initially 0)
Use Earth radius R = 6371 km.
Command 1: REGISTER
Format:
REGISTER name lat lon capacity
Validation (return ERROR if any fails):
lat in [-90, 90] inclusive
lon in [-180, 180] inclusive
capacity > 0
name must be unique (not previously registered)
On success: create the datacenter with healthy=true, load=0, and return OK.
Command 2: SET_HEALTHY
Format:
SET_HEALTHY name true/false
Rules:
If name does not exist: return ERROR
Otherwise update the health flag and return OK
Command 3: DISTANCE
Format:
DISTANCE lat1 lon1 lat2 lon2
Rules:
Validate: lat1, lat2 ∈ [-90, 90] and lon1, lon2 ∈ [-180, 180]; if any out of range return ERROR
Compute great-circle distance using the Haversine formula (R=6371)
Return the distance rounded to the nearest integer (km) as a string
Command 4: ROUTE (distance-based load balancing)
Format:
ROUTE lat lon
Algorithm:
Filter: keep only datacenters with healthy == true
Sort: compute Haversine distance from (lat, lon) to each healthy datacenter; sort by
ascending distance
tie-break by name alphabetically
Select: scan the sorted list and pick the first datacenter with load < capacity
Update & Output:
If found: increment its load by 1 and return:
<chosen_name> <distance_rounded> <healthy_list>
If none found (all healthy datacenters are full): return:
None <healthy_list>
Where:
<distance_rounded> is the rounded distance to the chosen datacenter
<healthy_list> is a comma-separated list of all healthy datacenter names in the sorted order from step 2 (include full ones too)
Test Cases (5)
Case 1 (REGISTER validation + SET_HEALTHY)
input
7
REGISTER us-west 38 -122 100
REGISTER us-east 41 -74 150
REGISTER us-west 50 -100 50
REGISTER invalid-node 91 0 100
REGISTER invalid-cap 0 0 0
SET_HEALTHY us-east false
SET_HEALTHY fake-node true
output
OK
OK
ERROR
ERROR
ERROR
OK
ERROR
Case 2 (DISTANCE basics)
input
3
DISTANCE 38 -122 41 -74
DISTANCE 0 0 0 0
DISTANCE 91 0 0 0
output
4080
0
ERROR
Case 3 (ROUTE: tie by name + capacity exhaustion)
input
7
REGISTER node-A 0 0 1
REGISTER node-B 0 0 1
REGISTER node-C 10 10 100
SET_HEALTHY node-C false
ROUTE 0 0
ROUTE 0 0
ROUTE 0 0
output
OK
OK
OK
OK
node-A 0 node-A,node-B
node-B 0 node-A,node-B
None node-A,node-B
Case 4 (ROUTE: no healthy datacenters)
input
4
REGISTER dc1 0 0 1
SET_HEALTHY dc1 false
ROUTE 0 0
ROUTE 10 10
output
OK
OK
None
None
Case 5 (DISTANCE boundary values)
input
2
DISTANCE -90 -180 90 180
DISTANCE -90 0 -90 0
output
20015
0
Example
Input
7
REGISTER us-west 38 -122 100
REGISTER us-east 41 -74 150
REGISTER us-west 50 -100 50
REGISTER invalid-node 91 0 100
REGISTER invalid-cap 0 0 0
SET_HEALTHY us-east false
SET_HEALTHY fake-node true
Output
OK
OK
ERROR
ERROR
ERROR
OK
ERROR