踩的坑
薛定谔的测试点5,同样的代码第一次能过,多提交几次就超时了,刷新一下,欸嘿,又过的了了。实名怀疑运行lua时有bug,比如说没清掉全局变量什么的。
另外,把变量写成local的好像性能要好一点,区别有待我研究
题目好像对踩点客户并不敏感,我觉得我的代码明显会把五点整来的客户也赶出去
代码
--IO
local read_func = io.read('*a'):gmatch('%w+')
local read_string = function()
return tonumber(read_func())
end
-- para
local N = read_string()
local K = read_string()
local custom_infos = {}
--TIME : {hour = x, minute = x, second = x}
--{arrive_time = TIME, serve_time = TIME, leave_time = TIME, processing_time = x}
--logic
local compare_time = function(a, b)
return a.hour == b.hour and (a.minute == b.minute and a.second < b.second or a.minute < b.minute) or a.hour < b.hour
end
local time_variation = function(a, b)
return ((b.hour - a.hour) * 60 + (b.minute - a.minute)) * 60 + (b.second - a.second)
end
--main
for index = 1, N do
local custom_info = {
arrive_time = {
hour = read_string(),
minute = read_string(),
second = read_string()
},
serve_time = {},
leave_time = {},
processing_time = read_string()
}
if compare_time(custom_info.arrive_time, {hour = 17, minute = 00, second = 00}) then
table.insert(custom_infos, custom_info)
end
end
table.sort(custom_infos, function(a, b) return compare_time(a.arrive_time, b.arrive_time) end)
local waiting_seconds = 0
local leave_time_array = {}
for index, custom_info in pairs(custom_infos) do
custom_info.serve_time = compare_time({hour = 08, minute = 00, second = 00}, custom_info.arrive_time) and custom_info.arrive_time or {hour = 08, minute = 00, second = 00}
local avaliable_leave_time = leave_time_array[#leave_time_array - K + 1]
if avaliable_leave_time and compare_time(custom_info.arrive_time, avaliable_leave_time) then
custom_info.serve_time = avaliable_leave_time
end
waiting_seconds = waiting_seconds + time_variation(custom_info.arrive_time, custom_info.serve_time)
local leave_minute = custom_info.serve_time.minute + custom_info.processing_time
custom_info.leave_time = {
hour = custom_info.serve_time.hour + math.floor(leave_minute / 60),
minute = leave_minute % 60,
second = custom_info.serve_time.second}
local flag = false
for time_index = #leave_time_array < K - 1 and 1 or #leave_time_array - K + 2, #leave_time_array do
if compare_time(custom_info.leave_time, leave_time_array[time_index]) then
table.insert(leave_time_array, time_index, custom_info.leave_time)
flag = true
break
end
end
if not flag then
table.insert(leave_time_array, custom_info.leave_time)
end
end
print(string.format("%.1f", #custom_infos == 0 and 0 or waiting_seconds / #custom_infos / 60))