作业:

[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"; $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']); }); ?>