CF - Quiz Master

Authors: Rameez Parwez, David Guo

Table of Contents

ExplanationImplementation

Official Editorial (C++)

Explanation

Let's use a sliding window technique on sorted smartness values to track how many topics are being covered as we add and remove a student.

As we expand the team by moving the right pointer, we check the factors of each student's smartness to determine which topics they help cover. If all topics are covered, we calculate the difference between the highest and lowest smartness values in the current team. Then, we attempt to reduce the size of the team from the left to see if the range can be further minimized while still covering all topics.

Implementation

Time Complexity: O(nlog(n)+nM)\mathcal{O}(n \log(n) + n \cdot \sqrt{M})

C++

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
const int M = 1e5;
std::vector<int> factors[M + 1];
int main() {
// Precompute factors for all numbers from 1 to M

Java

import java.io.*;
import java.util.*;
public class Main {
private static final int M = 100000;
private static List<Integer>[] factors = new ArrayList[M + 1];
public static void main(String[] args) throws IOException {
// Precompute factors for all numbers from 1 to M
for (int i = 1; i <= M; i++) { factors[i] = new ArrayList<>(); }

Python

M = 10**5
factors = [[] for _ in range(M + 1)]
# Precompute factors for all numbers from 1 to M
for i in range(1, M + 1):
for j in range(i, M + 1, i):
factors[j].append(i)
for _ in range(int(input())):
n, m = map(int, input().split())

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!