作业:

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'"; $max_score_query = DB::selectFirst("SELECT MAX(score) AS score FROM submissions WHERE $cond"); if ($max_score_query) { $max_score = $max_score_query['score']; } else { $max_score = 0; } $submission = DB::selectFirst("SELECT id, score FROM submissions WHERE $cond AND score = $max_score ORDER BY submit_time DESC LIMIT 1"); 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']); }); ?>