2025 年 1 月USACO竞赛银奖组问题二—Farmer John's Favorite Operation

It is another cold and boring day on Farmer John's farm. To pass the time, Farmer John has invented a fun leisure activity involving performing operations on an integer array.

Farmer John has an array a of N (1≤N≤2⋅105) non-negative integers and an integer M(1≤M≤109). Then, FJ will ask Bessie for an integer x. In one operation, FJ can pick an index i and subtract or add 1 to ai. FJ's boredom value is the minimum number of operations he must perform so that aix is divisible by M for all 1≤iN.

Among all possible x, output FJ's minimum possible boredom value.

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

The first line contains T (1≤T≤10), the number of independent test cases to solve.

The first line of each test case contains N and M.

The second line of each test case contains a1,a2,...,aN (0≤ai≤109).

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

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

For each test case, output an integer on a new line containing FJ's minimum possible boredom value among all possible values of x.

SAMPLE INPUT:

2
5 9
15 12 18 3 8
3 69
1 988244353 998244853

SAMPLE OUTPUT:

10
21

In the first test case, one optimal choice of x is 3. FJ can perform 10 operations to make a=[12,12,21,3,12].

SCORING:

Input 2: N≤1000 and M≤1000.
Input 3: N≤1000.
Inputs 4-5: M≤105.
Inputs 6-16: No additional constraints.

Problem credits: Chongtian Ma

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

咨询一对一备赛规划

USACO竞赛考试网-二维码

2025 年 1 月USACO竞赛银奖组问题一—Cow Checkups

Farmer John's N (1≤N≤5⋅105) cows are standing in a line, with cow 1
at the front of the line and cow N at the back of the line. FJ's cows also come in many different species. He denotes each species with an integer from 1 to N. The i'th cow from the front of the line is of species ai (1≤ai ≤N).

FJ is taking his cows to a checkup at a local bovine hospital. However, the bovine veterinarian is very picky and wants to perform a checkup on the i'th cow in the line, only if it is species bi (1≤biN).

FJ is lazy and does not want to completely reorder his cows. He will perform the following operation exactly once.

Select two integers l and r such that 1≤lrN. Reverse the order of the cows that are between the l-th cow and the r-th cow in the line, inclusive.

FJ wants to measure how effective this approach is. Find the sum of the number of cows that are checked by the veterinarian over all N(N+1)/2 possible operations.

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

The first line contains an integer N.

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

The third line contains b1,b2,…,bN.

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

Output one line with the sum of the number of cows that are checked by the veterinarian over all possible operations.

SAMPLE INPUT:

3
1 3 2
3 2 1

SAMPLE OUTPUT:

3

If FJ chooses (l=1,r=1), (l=2,r=2), or (l=3,r=3) then no cows will be checked. Note that those operations do not modify the location of the cows.

The following operations result in one cow being checked.

  • l=1,r=2: FJ reverses the order of the first and second cows so the species of each cow in the new lineup will be [3,1,2]. The first cow will be checked.
  • l=2,r=3: FJ reverses the order of the second and third cows so the species of each cow in the new lineup will be [1,2,3]. The second cow will be checked.
  • l=1,r=3: FJ reverses the order of the first, second, and third cows so the species of each cow in the new lineup will be [2,3,1]. The third cow will be checked.

The total number of cows checked over all six operations is 0+0+0+1+1+1=3.

SAMPLE INPUT:

3
1 2 3
1 2 3

SAMPLE OUTPUT:

12

There are three possible operations that cause 3 cows to be checked: (l=1,r=1), (l=2,r=2), and (l=3,r=3). The remaining operations each result in 1 cow being checked. The total number of cows checked over all six operations is 3+3+3+1+1+1=12.

SAMPLE INPUT:

7
1 3 2 2 1 3 2
3 2 2 1 2 3 1

SAMPLE OUTPUT:

60

SCORING:

Input 4: N≤100
Input 5: N≤5000
Inputs 6-9:ai,bi  are all generated uniformly at random in the range [1,N]
Inputs 10-15:ai,bi are all generated uniformly at random in the range [1,2]
Inputs 16-23: No additional constraints.

Problem credits: Chongtian Ma, Haokai Ma, and Alex Liang

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

咨询一对一备赛规划

USACO竞赛考试网-二维码

2025 年 1 月USACO竞赛金奖组问题三—Photo Op

Farmer John's farm is full of lush vegetation and every cow wants a photo of its natural beauty. Unfortunately, Bessie still has places to be, but she doesn't want to disrupt any photo ops.

Bessie is currently standing at (X,0) on the XY-plane and she wants to get to (0,Y)
(1≤X,Y≤106). Unfortunately, N (1≤N≤3⋅105 ) other cows have decided to pose on the X axis. More specifically, cow i will be positioned at (xi,0) with a photographer at (0,yi) where (1≤xi,yi≤106) ready to take their picture. They will begin posing moments before time si (1≤si<T) and they will keep posing for a very long time (they have to get their picture just right). Here, 1≤T≤N+1.

Bessie knows the schedule for every cow's photo op, and she will take the shortest Euclidean distance to get to her destination, without crossing the line of sight from any photographer to their respective cow (her path will consist of one or more line segments).

If Bessie leaves at time t , she will avoid the line of sights for all photographer/cow pairs that started posing at time si≤t , and let the distance to her final destination be dt. Determine the values of ⌊dt⌋ for each integer t from 0 to T−1 inclusive.

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

The first line of input contains N and T, representing the number of cows posing on the x-axis and the timeframe that Bessie could leave at.

The second line of input contains X and Y, representing Bessie's starting X coordinate and her target Y coordinate respectively.

The next N lines contain si xi and yi . It is guaranteed that all xi are distinct from each other and X, and all yi are distinct from each other and Y. All si  will be given in increasing order, where si si +1.

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

Print T lines, with the t th (0-indexed) line containing ⌊dt⌋.

SAMPLE INPUT:

4 5
6 7
1 7 5
2 4 4
3 1 6
4 2 9

SAMPLE OUTPUT:

9
9
9
10
12

SAMPLE INPUT:

2 3
10 7
1 2 10
1 9 1

SAMPLE OUTPUT:

12
16
16

For t=0 the answer is =12.

For t=1 the answer is =16.

SAMPLE INPUT:

5 6
8 9
1 3 5
1 4 1
3 10 7
4 9 2
5 6 6

SAMPLE OUTPUT:

12
12
12
12
14
14

For t=5 the answer is . Path: (8,0)→(9,0)→(0,7)→(0,9)

SCORING:

Inputs 4-6: N≤100
Inputs 7-9: N≤3000
Inputs 10-12: T≤10
Inputs 13-18: No additional constraints

Problem credits: Suhas Nagar

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

咨询一对一备赛规划

USACO竞赛考试网-二维码

2025 年 1 月USACO竞赛金奖组问题二—Reachable Pairs

Consider an undirected graph with N nodes labeled 1…N and M edges (1≤N≤2⋅105,0≤M≤4⋅105). You're given a binary string s1s2…sN. At time t
for each t∈[1,N],

If st=0, node t is removed from the graph.
If st=1, node t is removed from the graph, and edges are added between every pair of neighbors that node t had just before removal.

Note that in both cases, when a node is removed from the graph all of its incident edges are removed as well.

Count the number of pairs of nodes that can reach each other via some sequence of edges just before each of timesteps 1…N.

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

The first line contains N and M.

The second line contains the bit string s of length N.

The next M lines each contain two integers denoting an edge of the graph.

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

N lines, the number of pairs before each timestep.

SAMPLE INPUT:

3 2
111
1 2
1 3

SAMPLE OUTPUT:

3
1
0

Before any removals, all pairs of nodes are reachable from each other. After node 1 is removed, an edge is added between 2 and 3, so they can still reach each other.

SAMPLE INPUT:

3 2
000
1 2
1 3

SAMPLE OUTPUT:

3
0
0

Before any removals, all pairs of nodes are reachable from each other. After node 1 is removed, 2 and 3 can no longer reach each other.

SAMPLE INPUT:

7 8
1101101
6 2
1 2
2 3
6 3
1 3
1 7
4 5
2 7

SAMPLE OUTPUT:

11
7
4
2
1
1
0

SCORING:

Inputs 4-6: N≤100
Inputs 7-8: All si equal zero.
Inputs 9-11: All si equal one.
Inputs 12-23: No additional constraints.

Problem credits: Benjamin Qi

2025 年 1 月USACO竞赛金奖组问题一—Median Heap

**Note: The time limit for this problem is 4s, twice the default.**

Farmer John has a binary tree with N nodes where the nodes are numbered from 1 to N (1≤N<2⋅105 and N is odd). For i>1, the parent of node i is ⌊i/2⌋. Each node has an initial integer value ai, and a cost ci to change the initial value to any other integer value (0≤ai,ci ≤109).

He has been tasked by the Federal Bovine Intermediary (FBI) with finding an approximate median value within this tree, and has devised a clever algorithm to do so.

He starts at the last node N and works his way backward. At every step of the algorithm, if a node would not be the median of it and its two children, he swaps the values of the current node and the child value that would be the median. At the end of this algorithm, the value at node 1 (the root) is the median approximation.

The FBI has also given Farmer John a list of Q (1≤Q≤2⋅105) independent queries each specified by a target value m (0≤m≤109). For each query, FJ will first change some of the node's initial values, and then execute the median approximation algorithm. For each query, determine the minimum possible total cost for FJ to make the output of the algorithm equal to m.

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

The first line of input contains N.

The next N lines each contain two integers ai and ci.

The next line contains Q.

The next Q lines each contain a target value m.

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

Output Q lines, the minimum possible total cost for each target value m.

SAMPLE INPUT:

5
10 10000
30 1000
20 100
50 10
40 1
11
55
50
45
40
35
30
25
20
15
10
5

SAMPLE OUTPUT:

111
101
101
100
100
100
100
0
11
11
111

To make the median approximation equal 40, FJ can change the value at node 3 to 60. This costs c3=100.

To make the median approximation equal 45, FJ can change the value at node 3 to 60 and the value at node 5 to 45. This costs c3+c5=100+1=101.

SCORING:

Inputs 2-4: N,Q≤50
Inputs 5-7: N,Q≤1000
Inputs 8-16: No additional constraints

Problem credits: Suhas Nagar and Benjamin Qi

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

咨询一对一备赛规划

2025 年 1 月USACO竞赛白金组问题三— Watering the Plants

Bessie's garden has N plants labeled 1 through N(2≤N≤5⋅105) from left to right. Bessie knows that plant i requires at least wi (0≤wi≤106) units of water.

Bessie has a very peculiar irrigation system with N−1 canals, numbered 1 through N−1. Each canal i has an associated unit cost ci (1≤ci≤106), such that Bessie can pay cik to provide plants i and i+1 each with k units of water, where k is a non-negative integer.

Bessie is busy and may not have time to use all the canals. For each 2≤iN compute the minimum cost required to water plants 1 through i using only the first i−1 canals.

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

The first line contains a single positive integer N.

The second line contains N space-separated integers w1,…,wN.

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

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

Output N−1 newline-separated integers. The (i−1)th integer should contain the minimum cost to water the first i plants using the first i−1 canals.

SAMPLE INPUT:

3
39 69 33
30 29

SAMPLE OUTPUT:

2070
2127

The minimum cost to water the first 2 plants using the first canal is to pay 30⋅69=2070 by using the first canal 69 times.

The minimum cost to water the first 3 plants is to use the first canal 39 times and the second canal 33 times, paying 39⋅30+29⋅33=2127.

SAMPLE INPUT:

3
33 82 36
19 1

SAMPLE OUTPUT:

1558
676

SAMPLE INPUT:

8
35 89 44 1 35 3 62 50
7 86 94 62 63 9 49

SAMPLE OUTPUT:

623
4099
4114
6269
6272
6827
8827

SCORING:

Input 4: N≤200, and all wi ≤200.
Inputs 5-6: All wi ≤200.
Inputs 7-10: N≤5000.
Inputs 11-14: All wi and ci are generated independently and uniformly at random.
Inputs 15-19: No additional constraints.

Problem credits: Benjamin Qi

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

咨询一对一备赛规划

2025 年 1 月USACO竞赛白金组问题二— Shock Wave

Bessie is experimenting with a powerful hoof implant that has the ability to create massive shock waves. She has N (2≤N≤105) tiles lined up in front of her, which require powers of at least p0,p1,…,pN−1 to break, respectively (0≤pi≤1018).

Bessie can apply power by punching a specific tile but due to the strange nature of her implant, it will not apply any power to the tile she punches. Instead, if she chooses to punch tile x once, where x is an integer in [0,N−1], it applies |ix| power to tile i for all integers i in the range [0,N−1]. This power is also cumulative, so applying 2 power twice to a tile will apply a total of 4 power to the tile.

Please determine the fewest number of punches required to break all the tiles.

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

The first line contains T(1≤T≤100) representing the number of test cases.

Line 2t contains a single integer N, the number of tiles in test case t.

Line 2t+1 contains N space separated numbers p0,p1,…,pN−1 representing that tile i takes ppower to be broken.

It is guaranteed that the sum of all N in a single input does not exceed 5⋅105.

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

T lines, the ith line representing the answer to the ith test case.

SAMPLE INPUT:

6
5
0 2 4 5 8
5
6 5 4 5 6
5
1 1 1 1 1
5
12 10 8 6 4
7
6 1 2 3 5 8 13
2
1000000000000000000 1000000000000000000

SAMPLE OUTPUT:

2
3
2
4
4
2000000000000000000

For the first test, the only way for Bessie to break all the tiles with two punches is to punch 0 twice, applying total powers of [0,2,4,6,8] respectively.

For the second test, one way for Bessie to break all the tiles with three punches is to punch 0, 2, and 4 one time each, applying total powers of [6,5,4,5,6] respectively.

For the third test, one way for Bessie to break all the tiles with two punches is to punch 0 and 1 one time each, applying total powers of [1,1,3,5,7] respectively.

SCORING:

Input 2: All pi are equal.
Inputs 3-6: N≤100
Inputs 7-14: No additional constraints.

Problem credits: Suhas Nagar

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

咨询一对一备赛规划

USACO竞赛考试网-二维码

2025 年 1 月USACO竞赛白金组问题一—DFS Order

Bessie has a simple undirected graph with vertices labeled 1…N(2≤N≤750). She generates a depth-first search (DFS) order of the graph by calling the function dfs(1), defined by the following C++ code. Each adjacency list (adj[i] for all 1≤iN) may be permuted arbitrarily before starting the depth first search, so a graph can have multiple possible DFS orders.

vector<bool> vis(N + 1);
vector<vector<int>> adj(N + 1); // adjacency list
vector<int> dfs_order;

void dfs(int x) {
if (vis[x]) return;
vis[x] = true;
dfs_order.push_back(x);
for (int y : adj[x]) dfs(y);
}

You are given the initial state of the graph as well as the cost to change the state of each edge. Specifically, for every pair of vertices (i,j) satisfying 1≤i<jN, you are given an integer ai,j(0<|ai,j|≤1000) such that

If ai,j >0, edge (i,j) is not currently in the graph, and can be added for cost ai,j.
If ai,j<0, edge (i,j)is currently in the graph, and can be removed for cost −ai,j.

Determine the minimum total cost to change the graph so that [1,2…,N]
is a possible DFS ordering.

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

The first line contains N.

Then N−1 lines follow. The j−1
th line contains a1,j ,   a2,j,…,aj−1,j separated by spaces.

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

The minimum cost to change the graph so that [1,2,…,N]
is a possible DFS ordering.

SAMPLE INPUT:

4
1
2 3
40 6 11

SAMPLE OUTPUT:

10

Initially, the graph contains no edges. (1,2),(2,3),(2,4)
can be added for a total cost of 1+3+6. The graph now has two possible DFS orderings: [1,2,3,4],[1,2,4,3].

SAMPLE INPUT:

5
-1
10 -2
10 -7 10
-6 -4 -5 10

SAMPLE OUTPUT:

5

Initially, the graph contains edges (1,2),(2,3),(2,4),(1,5),(2,5),(3,5). Edge (3,5) can be removed for a cost of 5.

SAMPLE INPUT:

4
-1
-2 300
4 -5 6

SAMPLE OUTPUT:

9

Initially, the graph contains edges (1,2),(1,3),(2,4). Edge (2,4)
can be removed and edge (1,4) can be added for a total cost of 5+4=9.

SCORING:

Inputs 4-9: All ai,j >0
Inputs 10-16: N≤50
Inputs 17-23: No additional constraints.

Problem credits: Benjamin Qi

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

咨询一对一备赛规划

高含金量热门国际竞赛!0基础学生如何准备USACO?

USACO - 美国计算机奥林匹克竞赛,向全球选手开放,任何对编程有浓厚兴趣的人都可以免费注册并参与其中。其公平的晋级机制与高质量的竞赛题目,使得USACO成为了众多算法爱好者和信奥选手追逐的目标。不论是在学术领域还是在日益发展的工业应用中,计算机科学的地位愈发重要。

0基础学生准备USACO逐步进阶计划

1-2年级:兴趣培养期

目标:激发对编程的兴趣,理解基本逻辑结构。

语言:Scratch

必备知识:

  - 掌握顺序执行、条件判断和循环执行的逻辑结构。

  - 学习变量、函数、列表的概念。

  - 理解广播、克隆原理。

  - 初步了解搜索算法(如线性查找)。

竞赛:参与适合初学者的白名单赛事,例如CIE。

3-4年级:开启竞赛预备期

目标:熟悉编程语言,开始为竞赛做准备。

语言:Python

必备知识:

  - Python基础语法,包括变量库、模块函数、列表等。

  - 复杂应用的循环和条件语句。

  - 简单图形界面编程(如使用turtle库),游戏开发基础(如pygame库)。

竞赛:继续参与白名单赛事,进一步了解信奥和其他相关竞赛。

5-6年级:USACO入门期

目标:正式开始准备USACO竞赛。

语言:C++

必备知识:

  - C++标准的认识,程序输入输出。

  - 分支与循环语句,二维数组,浮点数操作,字符处理。

竞赛:参加USACO青铜级别比赛,以及其他白名单赛事。

7-8年级:USACO铜升银期

目标:努力在USACO中从青铜级提升到白银级。

语言:C++

必备知识:

  - 深入学习变数、循环、条件语句、函数/方法。

  - 集合、字典/哈希表的应用。

竞赛:专注于USACO白银级别的题目练习,同时参与其他竞赛。

9年级:USACO银升金期

目标:在USACO中从白银级提升到黄金级。

语言:C++

必备知识:

  - 图和树的数据结构。

  - 堆栈、队列和优先级队列。

  - 二分查找、深度优先搜索(DFS)、广度优先搜索(BFS)。

  - 其他高级主题如滑动窗口、前缀和等。

竞赛:重点放在USACO黄金级别的题目上,并尝试解决更复杂的挑战。

10-11年级:USACO金升铂金期

目标:最终目标是在USACO中从黄金级晋升至铂金级。

语言:C++

必备知识:

  - 动态规划、图论中的最短路径和最小生成树算法。

  - 不相交集(并查集)、字符串算法、几何算法。

  - 熟悉Dijkstra, Prim, Kruskal等经典算法。

  - 学习高级数据结构如二叉索引树(BIT)或树状数组。

竞赛:全力以赴准备USACO铂金级别的比赛,争取最佳成绩。

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

预约最新真题讲座、课程详情可扫码咨询⇓

思维导图

USACO本周五即将开赛!USACO竞赛难度分析!

USACO(美国计算机奥林匹克竞赛)以其独特的命题标准和对参赛者逻辑推理、问题解决能力的重视而著称。它与国内的NOIP(全国青少年信息学奥林匹克联赛)在难度上有一定的对应关系,但USACO更强调算法思维深度而非复杂算法知识的记忆,这使得它成为一个更加注重智慧运用的竞技场。

第二场比赛: 2025年1月24日至27日

USACO竞赛难度分析

总体难度特点

算法思维为核心:USACO的比赛设计旨在评估参赛者的算法思维能力和解决问题的智慧,而不是简单地测试他们对高级算法结构的记忆。

逻辑推理与创造力:比赛题目通常要求参赛者能够灵活应用所学知识,创造性地构思解决方案,并优化算法以适应严格的运行时间和内存限制。

编程语言多样性:支持多种编程语言(如C++、Python、Java等),其中C++由于其性能优势和广泛使用,成为许多选手的选择;Python则因其易用性和丰富的库支持受到欢迎。

从铜级到银级

新手友好:对于编程新手来说,升至白银级别相对较为容易。只要掌握了基础编程概念和简单的算法技巧,如循环、条件判断、数组操作以及基本搜索算法等,就能顺利通过这一阶段。

多语言支持:USACO允许使用多种编程语言参赛,为不同背景的学生提供了平等的机会。

从银级到金级

数据结构与算法基础:虽然从白银升至黄金级别的难度有所增加,但对于已经掌握了一定编程技能的学生而言仍然是可实现的目标。此阶段需要学生深入理解基础数据结构(如链表、栈、队列、树、图等)以及一些经典的算法(如排序、查找、贪心算法、递归等)。

系统复习与实践:零基础的学生可能需要更多时间来系统复习相关知识点,但只要有足够的练习和经验积累,也是可以逐步提升自己的水平的。

从金级到铂金级

挑战性显著提高:这是USACO中最难的一个阶段,不仅要求极高的编程熟练度,还需要深厚的算法理论基础。参赛者必须能够快速识别问题的本质,并在有限的时间内找到高效的解决方案。

灵活算法思维:铂金级别的题目往往没有唯一的解法,甚至可能存在多个可行的答案。因此,拥有灵活的算法思维和创新能力是关键所在。参赛者需要能够在短时间内做出最佳选择,并确保算法的效率满足题目要求。

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

预约最新真题讲座、课程详情可扫码咨询⇓

思维导图