2025 年 2 月USACO竞赛银奖组问题二—Vocabulary Quiz

Bessie is helping Elsie with her upcoming vocabulary quiz. The words to be tested will be drawn from a bank of M distinct words, where no word in the bank is a prefix of another word in the bank.

While the bank is nonempty, Bessie will select a word from the bank, remove it from the bank, and read it to Elsie one character at a time from left to right. Elsie's task is to tell Bessie once she can uniquely identify it, after which Bessie will stop reading.

Bessie has already decided to read the words from the word bank in the order w1,w2,…,wM. If Elsie answers as quickly as possible, how many characters of each word will Bessie read?

The words are given in a compressed format. We will first define N+1 (1≤N≤ 106 ) distinct words, and then the word bank will consist of all those words that are not a prefix of another word. The words are defined as follows:

Initially, the 0th word will be the empty string.

Then for each 1≤i≤N, the ith word will be equal to the pith word plus an additional character at the end (0≤pi<i). The characters are chosen such that all N+1 words are distinct.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains N, where N+1 is the number of words given in the  compressed format.

The next line contains p1,p2,…,pN where pi represents that the i-th word is formed by taking the pi-th word and adding a single character to the end.

Let M be the number of words that are not a prefix of some other word. The next M lines will contain w1,w2,…,wM, meaning that the with word will be the ith to be read. It is guaranteed that the words to be read form a permutation of the words in the word bank.

OUTPUT FORMAT (print output to the terminal / stdout):

Output M lines, where the ith line contains the number of characters of the ith word that Bessie reads.

SAMPLE INPUT:

5
0 1 2 3 4
5

SAMPLE OUTPUT:

0

There are 6words labeled 0…5. Word 5 is the only one that is not a prefix of another word, so it is the only one in the bank. In general, once only one word is left in the bank, Elsie won't need any characters to identify it.

SAMPLE INPUT:

4
0 0 1 1
4
3
2

SAMPLE OUTPUT:

2
1
0

The bank consists of words 2, 3, and 4.

Elsie needs two characters to identify word 4 since words 3 and 4 share their first character in common.

Once Bessie reads the first character of word 3 , Elsie has enough characters to uniquely identify it, since word 4 was already read.

SAMPLE INPUT:

4
0 0 1 1
2
3
4

SAMPLE OUTPUT:

1
2
0

SCORING:

Inputs 4-5: No word has length greater than 20.
Inputs 6-10: The sum of the lengths of all words in the word bank does not exceed 107.
Inputs 11-18: No additional constraints.

Problem credits: Benjamin Qi

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛银奖组问题一—The Best Lineup

Farmer John has N (1≤N≤2⋅105) cows in a line a. The i'th cow from the front of line a is labeled an integer ai (1≤ai N). Multiple cows may be labeled the same integer.

FJ will construct another line b in the following manner:

Initially, b is empty.
While a is nonempty, remove the cow at the front of a and potentially add that cow to the back of b.

FJ wants to construct line b such that the sequence of labels in b from front to back is lexicographically greatest (see the footnote).

Before FJ constructs line b, he can perform the following operation at most once:

Choose a cow in line a and move it anywhere before its current position.

Given that FJ optimally performs the aforementioned operation at most once, output the lexicographically greatest label sequence of b he can achieve.

Each input will consist of T (1≤T≤100) independent test cases.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains T.

The first line of each test case contains N.

The second line of each test case contains N space-separated integers a1,a2,…,aN.

It is guaranteed that the sum of N over all test cases does not exceed 106.

OUTPUT FORMAT (print output to the terminal / stdout):

For each test case, output the lexicographically greatest b on a new line.

SAMPLE INPUT:

3
5
4 3 2 1 3
6
5 1 2 6 3 4
6
4 1 3 2 1 1

SAMPLE OUTPUT:

4 3 3 2 1
6 5 4
4 3 2 1 1

In the first test case, FJ can move the fifth cow to directly after the second cow. Now, a=[4,3,3,2,1]. It can be shown [4,3,3,2,1] is also the lexicographically greatest b.

In the second test case, FJ can move the fourth cow to the front of the line.

In the third test case, FJ does not need to perform any operations. He can construct b by adding each cow beside the second cow to the back of b. It can be shown this results in the lexicographically greatest b.

SCORING:

Inputs 2-4: N≤100
Inputs 5-8: N≤750
Inputs 9-18: No additional constraints

FOOTNOTE:

Recall that a sequence s is lexicographically greater than a sequence t if and only if one of the following holds:

At the first position i where siti, si>si.
If no such i exists, s is longer than t.

Problem credits: Chongtian Ma, Haokai Ma, Andrew Li

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛金奖组问题三—Friendship Editing

Farmer John's N cows are labeled 1 to N(2≤N≤16). The friendship relationships between the cows can be modeled as an undirected graph with M (0≤MN(N−1)/2) edges. Two cows are friends if and only if there is an edge between them in the graph.

In one operation, you can add or remove a single edge from the graph. Count the minimum number of operations required to ensure that the following property holds: If cows a and b are friends, then for every other cow c, at least one of a
and b is friends with c.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains N and M.

The next M lines each contain a pair of friends a and b (1≤a<bN). No pair of friends appears more than once.

OUTPUT FORMAT (print output to the terminal / stdout):

The number of edges you need to add or remove.

SAMPLE INPUT:

3 1
1 2

SAMPLE OUTPUT:

1

The network violates the property. We can add one of edges (2,3)
or (1,3), or remove edge (1,2)to fix this.

SAMPLE INPUT:

3 2
1 2
2 3

SAMPLE OUTPUT:

0
No changes are necessary.

SAMPLE INPUT:

4 4
1 2
1 3
1 4
2 3

SAMPLE OUTPUT:

1

SCORING:

Inputs 4-13: One input for each N∈[6,15] in increasing order.
Inputs 14-18: N=16

Problem credits: Benjamin Qi

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛金奖组问题二—The Best Subsequence

Farmer John has a binary string of length N (1≤N≤109), initially all zeros.

He will first perform M(1≤M≤2⋅105) updates on the string, in order. Each update flips every character from l to r. Specifically, flipping a character changes it from 0 to 1, or vice versa.

Then, he asks you Q (1≤Q≤2⋅105) queries. For each query, he asks you to output the lexicographically greatest subsequence of length k comprised of characters from the substring from l to r. If your answer is a binary string s1s2…sk, then output (that is, its value when interpreted as a binary number) modulo 109+7.

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

Recall that string A is lexicographically greater than string B of equal length if and only if at the first position i, if it exists, where AiBi, we have Ai>Bi.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains N, M, and Q.

The next M lines contain two integers, l and r (1≤lrN) — the endpoints of each update.

The next Q lines contain three integers, l, r, and k (1≤lrN,1≤krl+1) — the endpoints of each query and the length of the subsequence.

OUTPUT FORMAT (print output to the terminal / stdout):

Output Q lines. The ith line should contain the answer for the ith query.

SAMPLE INPUT:

5 3 9
1 5
2 4
3 3
1 5 5
1 5 4
1 5 3
1 5 2
1 5 1
2 5 4
2 5 3
2 5 2
2 5 1

SAMPLE OUTPUT:

21
13
7
3
1
5
5
3
1

After performing the M operations, the string is 10101.

For the first query, there is only one subsequence of length 5, 10101, which is interpreted as 1⋅24+0⋅23+1⋅22+0⋅21+1⋅20=21.

For the second query, there are 5 unique subsequences of length 4: 0101, 1101, 1001, 1011, 1010. The lexicographically largest subsequence is 1101, which is interpreted as 1⋅23+1⋅22+0⋅21+1⋅20=13.

For the third query, the lexicographically largest sequence is 111, which is interpreted as 7.

SAMPLE INPUT:

9 1 1
7 9
1 8 8

SAMPLE OUTPUT:

3

SAMPLE INPUT:

30 1 1
1 30
1 30 30

SAMPLE OUTPUT:

73741816

Make sure to output the answer modulo 109 +7.

SCORING:

Input 4: N≤10,Q≤1000
Input 5: M≤10
Inputs 6-7: N,Q≤1000
Inputs 8-12: N≤2⋅105 
Inputs 13-20: No additional constraints.

Problem credits: Chongtian Ma

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛金奖组问题一—Bessie's Function

Bessie has a special function f(x) that takes as input an integer in [1,N]
and returns an integer in [1,N] (1≤N≤2⋅105). Her function f(x) is defined by N
integers a1aN where f(x)=ax (1≤aiN).

Bessie wants this function to be idempotent. In other words, it should satisfy f(f(x))=f(x)for all integers x∈[1,N].

For a cost of ci, Bessie can change the value of ai to any integer in [1,N] (1≤ci≤109). Determine the minimum total cost Bessie needs to make f(x)
idempotent.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains N.

The second line contains N

space-separated integers a1,a2,…,aN.

The third line contains N space-separated integers c1,c2,…,cN.

OUTPUT FORMAT (print output to the terminal / stdout):

Output the minimum total cost Bessie needs to make f(x) idempotent.

SAMPLE INPUT:

5
2 4 4 5 3
1 1 1 1 1

SAMPLE OUTPUT:

3

We can change a1=4, a4=4, a5=4. Since all ci equal one, the total cost is equal to 3
, the number of changes. It can be shown that there is no solution with only 2
or fewer changes.

SAMPLE INPUT:

8
1 2 5 5 3 3 4 4
9 9 2 5 9 9 9 9

SAMPLE OUTPUT:

7

We change a3=3 and a4=4. The total cost is 2+5=7.

SCORING:

Subtasks:

Input 3: N≤20
Inputs 4-9:aii
Inputs 10-15: All aiare distinct.
Inputs 16-21: No additional constraints.
Additionally, in each of the last three subtasks, the first half of tests will satisfy ci=1 for all i.

Problem credits: Avnith Vijayram

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛白金组问题三—True or False Test

Bessie is taking an N-question true or false test (1≤N≤2⋅105). For the i-th question, she will gain apoints if she gets it correct, lose bi points if she gets it incorrect, or remain even if she does not answer it (0<ai,bi ≤109).

Bessie knows all the answers because she is a smart cow, but worries that Elsie (who is administering the test) will retroactively change up to k of the questions after the test such that Bessie does not get those questions correct.

Given Q (1≤QN+1) candidate values of k (0≤kN), determine the number of points Bessie can guarantee for each k, given that she must answer at least k questions.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains N and Q.

The next N lines each contain ai and bi.

The next Q lines each contain a value of k. No value of k appears more than once.

OUTPUT FORMAT (print output to the terminal / stdout):

The answer for each k on a separate line.

SAMPLE INPUT:

2 3
3 1
4 2
2
1
0

SAMPLE OUTPUT:

-3
1
7

For each value of k, it is optimal for Bessie to answer all of the questions.

SCORING:

Inputs 2-4: N≤100
Inputs 5-7: Q≤10, N≤2⋅105 
Inputs 7-20: No additional constraints.

Problem credits: Benjamin Qi

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛白金组问题二—Transforming Pairs

Answer Q(1≤Q≤105) independent queries each of the following form:

You are given four integers a,b,c,d(−1018≤a,b,c,d≤1018). In one operation you can either do a+=b, or b+=a. Determine the minimum number of operations to transform (a,b) into (c,d), or if it is impossible to do so, output −1.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains Q.

The next Q lines each contain four integers a,b,c,d.

OUTPUT FORMAT (print output to the terminal / stdout):

The answer for each query on a separate line.

SAMPLE INPUT:

4
5 -3 -1 -3
5 3 5 2
5 3 8 19
5 3 5 3

SAMPLE OUTPUT:

2
-1
3
0

First query: (5,−3)→(2,−3)→(−1,−3)

Second query: Impossible.

Third query: (5,3)→(8,3)→(8,11)→(8,19)

Fourth query: No operations necessary.

SCORING:

Input 2: |a|,|b|,|c|,|d|≤10
Input 3: a,b≥0
Input 4: a≥0≥b
Input 5: a≤0≤b
Input 6: a,b≤0
Input 7: c,d≥0
Input 8: c≥0≥d
Input 9: c≤0≤d
Input 10: c,d≤0
Inputs 11-14: Q≤103
Inputs 15-19: No additional constraints.

Problem credits: Benjamin Qi

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

2025 年 2 月USACO竞赛白金组问题一—Min Max Subarrays

注意:此问题的时限为 3 秒,是默认值的 1.5 倍。

You are given a length-N integer array a1,a2,…,aN (2≤N≤106,1≤aiN
). Output the sum of the answers for the subproblem below over all N(N+1)/2
contiguous subarrays of a.

Given a nonempty list of integers, alternate the following operations (starting with the first operation) until the list has size exactly one.

1.Replace two consecutive integers in the list with their minimum.
2.Replace two consecutive integers in the list with their maximum.

Determine the maximum possible value of the final remaining integer.

For example,

[4, 10, 3] -> [4, 3] -> [4]
[3, 4, 10] -> [3, 10] -> [10]

In the first array, (10,3) is replaced by min(10,3)=3 and (4,3) is replaced by max(4,3)=4.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains N.

The second line contains a1,a2,…,aN.

OUTPUT FORMAT (print output to the terminal / stdout):

The sum of the answer to the subproblem over all subarrays.

SAMPLE INPUT:

2
2 1

SAMPLE OUTPUT:

4

The answer for [2] is 2, the answer for [1] is 1, and the answer for [2,1] is 1.

Thus, our output should be 2+1+1=4.

SAMPLE INPUT:

3
3 1 3

SAMPLE OUTPUT:

12

SAMPLE INPUT:

4
2 4 1 3

SAMPLE OUTPUT:

22

Consider the subarray [2,4,1,3].

1.Applying the first operation on (1, 3), our new array is [2,4,1].
2.Applying the second operation on (4, 1), our new array is [2,4].
3.Applying the third operation on (2, 4), our final number is 2.

It can be proven that 2 is the maximum possible value of the final number.

SCORING:

Inputs 4-5: N≤100
Inputs 6-7: N≤5000
Inputs 8-9: max(a)≤10
Inputs 10-13: No additional constraints.

Problem credits: Benjamin Qi

扫码领取USACO试题答案+详细解析

咨询一对一备赛规划

USACO竞赛评分规则是怎样的?USACO竞赛不同级别的晋级后备考重点!

作为一项在全球范围内享有盛誉的国际竞赛,USACO不仅能够全面检验参赛选手们的编程技能与算法理解能力,更是许多有志于计算机科学及相关领域的留学生们首选的竞赛。对于申请全球顶尖高校的计算机专业,优异的USACO成绩常常成为申请者的强大优势。

USACO竞赛评分规则

2025年USACO竞赛四个级别比赛都是3道题,总分1000分。每道题333.3分。每道题有10个测试点,通过一个可得33.33分。

判分标准

2025年USACO竞赛和NOI系列赛事相同,即依据程序所能正确求解的测试点数量按比例计分。

对于各个测试点,一般题目会标注相应的时限要求和内存要求(如未具体标注,则C/C++/Pascal默认时限2秒,Java/Python默认时限4秒,内存均默认256MB)。

评测示例

即最终包含了10个测试点,其中7个正确、3个超时——绿色表示正确,红色表示错误(x表示错误答案,t表示时间超限,!表示运行时错误或内存超限,e表示输出文件为空,m表示找不到输出文件)。

USACO竞赛不同级别的晋级后备考重点

青铜级别 Bronze(入门级别)

备考重点:掌握基本的编程概念,如分支、循环,以及基础数据结构(列表、函数、二维列表、基础数组)。重点练习穷举算法、模拟算法、贪心算法、全排列、杂类题目和递归。

学习资源:推荐学习《算法基础:第五版》和《算法竞赛入门:第二版》,这些书籍适合初学者,能够帮助你建立坚实的编程基础。

白银级别 Sliver(难度进阶)

备考重点:在青铜级别的基础上,进一步学习排序、二分查找、递归搜索、图的遍历(DFS&BFS)、FLoodfill算法、前缀和、扫描线算法等算法。提高解决问题的能力和代码优化能力。

学习资源:可以参考《挑战程序设计竞赛2 算法和数据结构》和《算法竞赛进阶指南》,这些书籍提供了进阶的算法知识和实践题目。

黄金级别 Gold(极具挑战)

备考重点:深入理解动态规划、最短路径、最小生成树等高级算法。掌握堆、栈、树、链表等高级数据结构,以及算法的时间和空间复杂度分析。

学习资源:《算法解决导论》和《算法竞赛入门经典第二版》是不错的选择,它们不仅涵盖了高级算法,还提供了大量的练习题和案例分析。

铂金级别 Platinum(含金量极高)

备考重点:在黄金级别的基础上,进一步提升算法优化和问题解决能力。准备应对复杂的问题和多种可能的解决方案,提高编程效率和代码质量。

学习资源:USACO算法书是高级选手的必备资料,它们深入探讨了算法的各个方面,并提供了丰富的实践机会。

【扫码免费领取】USACO真题+备赛书单+一对一备考规划!

预约最新真题讲座、课程详情可添加下方顾问老师咨询

思维导图

美本名校"敲门砖"!USACO计算机竞赛具有这四大特点!

对于许多美本留学生来说,STEM专业无疑是最受欢迎的选择之一。在这个背景下,USACO(美国计算机奥林匹克竞赛)成为了无数学子追逐的目标,它既是一项竞争激烈的赛事,也是进入顶尖大学的一块"敲门砖"。

USACO是一项面向全球中学生的计算机编程与算法竞赛,具有以下几个显著特点:

1.计算机领域的高含金量竞赛:

USACO由美国官方举办,历史悠久,享有很高的声誉。对于计划申请美国大学尤其是STEM(科学、技术、工程和数学)专业的学生来说,参加并获奖可以极大地提升个人背景,增加被顶尖大学录取的机会。

2.结果反馈快:

USACO的比赛结果反馈迅速,通常可以在比赛结束后的短时间内得知成绩,有时甚至当场即可知道结果,一周内公布最终排名。这种快速的评分机制对那些接近申请截止日期(DDL)的学生尤其有利,他们可以及时利用这一成就来加强自己的申请资料。

3.层层晋级,更具挑战性:

USACO采用了一种类似于游戏中的“段位”系统,分为青铜(Bronze)、白银(Silver)、黄金(Gold)和铂金(Platinum)四个级别。每个级别的难度逐渐增加,参赛者从较低的级别开始,通过解决一系列的问题来获得积分,进而晋升到更高的级别。这种设计不仅增加了竞赛的乐趣,还使得不同水平的选手都能找到适合自己的挑战,同时也为参赛者提供了多次尝试和进步的机会。

4.门槛低,受众多:

尽管USACO的题目难度较高,但它对参赛者的年龄和学历没有严格限制,理论上任何对编程有兴趣的人都可以注册账号并参加。这意味着无论是小学生还是高中生,甚至是成人爱好者,都可以根据自己的能力和兴趣参与进来。此外,USACO支持多种编程语言,包括C++、Java、Python等,这为参赛者提供了更大的灵活性。

USACO不仅是一个挑战自我、提高编程技能的平台,也是一个展示个人才能、增强学术背景的重要途径。对于有志于从事计算机科学相关领域的学生来说,参加USACO是一次宝贵的经历。

【扫码免费领取】USACO真题+备赛书单+一对一备考规划!

预约最新真题讲座、课程详情可添加下方顾问老师咨询

思维导图