作业:

format('Y-m-d H:i:s'); foreach ($problems as $problem) { $problem_ids[] = $problem['problem_id']; } sort($problem_ids); foreach ($users as $user) { $usernames[] = $user['username']; } // standings: rank => [user => [username, realname], scores[], rank] $standings = []; foreach ($usernames as $username) { $user = queryUser($username); $row = ['total_score' => 0]; $row['user'] = [ 'username' => $user['username'], 'realname' => $user['realname'], ]; foreach ($problem_ids as $problem_id) { $cond = "submitter = '{$user['username']}' AND problem_id = $problem_id AND submit_time <= '$submission_end_time'"; $submission = DB::selectFirst("SELECT id, score FROM submissions INNER JOIN (SELECT MAX(score) AS score FROM submissions WHERE $cond) AS max USING (score) WHERE $cond ORDER BY submit_time DESC"); if ($submission) { $row['scores'][] = [ 'submission_id' => $submission['id'], 'score' => intval($submission['score']), ]; $row['total_score'] += $submission['score']; } else { $row['scores'][] = null; } } $standings[] = $row; } usort($standings, function($lhs, $rhs) { if ($lhs['total_score'] != $rhs['total_score']) { return $rhs['total_score'] - $lhs['total_score']; } return strcmp($lhs['user']['username'], $rhs['user']['username']); }); ?>