作业:

[total_score, user => [username, realname], scores[]] $standings = []; foreach ($usernames as $username) { $user = queryUser($username); $row = ['total_score' => 0]; $scores = []; $row['user'] = [ 'username' => $user['username'], 'realname' => $user['realname'], ]; $cond = "submitter = '{$user['username']}' AND unix_timestamp(submit_time) <= " . $submission_end_time->getTimestamp(); $cond_problem = "problem_id IN (".implode(',', $problem_ids).")"; $query = DB::query("SELECT MAX(id) as id, problem_id, MAX(score) as score FROM submissions WHERE (problem_id, score) IN (SELECT problem_id, MAX(score) FROM submissions WHERE $cond AND $cond_problem GROUP BY problem_id) AND $cond GROUP BY problem_id"); while ($_row = DB::fetch($query)) { $scores[$_row['problem_id']] = [ 'submission_id' => $_row['id'], 'score' => $_row['score'], ]; } foreach ($problem_ids as $problem_id) { if ($scores[$problem_id]) { $row['scores'][] = [ 'submission_id' => $scores[$problem_id]['submission_id'], 'score' => intval($scores[$problem_id]['score']), ]; $row['total_score'] += $scores[$problem_id]['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']); }); ?>