Explanation
Observe that we are given all of the jury's marks in chronological order. Therefore, if we knew the participant's initial score, we could compute the participant's announced score after each of the judges gives their respective ratings.
This motivates us to use prefix sums. Consider the prefix sum array such that equals the sum of the first jury marks.
Now consider the value , the first of the values that Polycarp remembers being announced. Letting the initial score be denoted by , we observe that for some from to . Thus, we must have that for some . We can then iterate through to find our candidates for the participant's initial score.
Now, we must check if each of our candidates is an actually valid starting score. Recall that Polycarp remembers some of the announced scores, all of which were the participant's score at some point. Therefore, if we arbitrarily pick one of our candidate values, say , then the set of values of the form for ranging from to must contain all of Polycarp's remembered scores.
Thus, we check if each candidate yields a valid starting score, and use the count of valid candidates to compute our answer.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;int main() {int mark_num;int remember_num;cin >> mark_num >> remember_num;// All net changes in the scorevector<int> changes(mark_num + 1);
Java
import java.io.*;import java.util.*;public class JuryMarks {public static void main(String[] args) throws IOException {Kattio io = new Kattio();int numJury = io.nextInt();int numScores = io.nextInt();// All net changes in the scoreint[] changes = new int[numJury + 1];
Python
mark_num, remember_num = [int(i) for i in input().split()]# All net changes in the scorechanges = [0] + [int(i) for i in input().split()]scores = {int(i) for i in input().split()}assert mark_num == len(changes) - 1 and len(scores) == remember_numfor i in range(1, len(changes)):changes[i] += changes[i - 1]
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!