USACO2022年12月美国计算机奥赛竞赛银奖组问题三——Range Reconstruction

Bessie has an array a1,…,aN, where 1≤N≤300 and 0≤ai≤109 for all i. She won't tell you a itself, but she will tell you the range of each subarray of a. That is, for each pair of indices i≤j, Bessie tells you ri,j=maxa[i…j]−mina[i…j]. Given these values of r, please construct an array that could have been Bessie's original array. The values in your array should be in the range [−109,109].

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

The first line contains N.
Another N lines follow. The ith of these lines contains the integers ri,i,ri,i+1,…,ri,N.

It is guaranteed that there is some array a with values in the range [0,109] such that for all i≤j, ri,j=maxa[i…j]−mina[i…j].

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

Output one line containing N integers b1,b2,…,bN in the range [−109,109] representing your array. They must satisfy ri,j=maxb[i…j]−minb[i…j] for all i≤j.

SAMPLE INPUT:

3
0 2 2
0 1
0

SAMPLE OUTPUT:

1 3 2
For example, r1,3=maxa[1…3]−mina[1…3]=3−1=2.

SAMPLE INPUT:

3
0 1 1
0 0
0

SAMPLE OUTPUT:

0 1 1
This example satisfies the constraints for subtask 1.

SAMPLE INPUT:

4
0 1 2 2
0 1 1
0 1
0

SAMPLE OUTPUT:

1 2 3 2
This example satisfies the constraints for subtask 2.

SAMPLE INPUT:

4
0 1 1 2
0 0 2
0 2
0

SAMPLE OUTPUT:

1 2 2 0

SCORING:

Test 5 satisfies r1,N≤1.
Tests 6-8 satisfy ri,i+1=1 for all 1≤i<N.
Tests 9-14 satisfy no additional constraints.
Problem credits: Danny Mittal

USACO2022年12月美国计算机奥赛竞赛银奖组问题二——Circular Barn

Farmer John and his archnemesis Farmer Nhoj are playing a game in a circular barn. There are N (1≤N≤105) rooms in the barn, and the ith room initially contains ai cows (1≤ai≤5⋅106). The game is played as follows:
Both farmers will always be in the same room. After entering a room, each farmer takes exactly one turn, with Farmer John going first. Both farmers initially enter room 1.
If there are zero cows in the current room, then the farmer to go loses. Otherwise, the farmer to go chooses an integer P, where P must either be 1 or a prime number at most the number of cows in the current room, and removes P cows from the current room.
After both farmers have taken turns, both farmers move to the next room in the circular barn. That is, if the farmers are in room i, then they move to room i+1, unless they are in room N, in which case they move to room 1.
Determine the farmer that wins the game if both farmers play optimally.

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

The input contains T test cases. The first line contains T (1≤T≤1000). Each of the T test cases follow.
Each test case starts with a line containing N, followed by a line containing a1,…,aN.

It is guaranteed that the sum of all N is at most 2⋅105.

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

For each test case, output the farmer that wins the game, either "Farmer John" or "Farmer Nhoj."

SAMPLE INPUT:

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

SAMPLE OUTPUT:

Farmer Nhoj
Farmer John
Farmer John
Farmer John
Farmer Nhoj

For the first test case, Farmer John can remove 1, 2, or 3 cows from the first room. Whichever number he removes, Nhoj can remove the remaining cow(s), forcing FJ to lose when they circle back to the first room.

For the second test case, FJ can remove 5 cows, forcing Nhoj to work with only 4 cows remaining. Now, Nhoj can either remove 1, 2, or 3 cows. This is now similar to the first test case.

For the third and fourth test cases, FJ can immediately remove all the cows from the first room, forcing Nhoj to lose.

For the fifth test case, FJ can remove 1, 2, or 3, cows from the first room, and Nhoj can remove the rest right after. When they circle back around to the first room, FJ will lose.

SCORING:

Inputs 2-4 satisfy N=1.
Inputs 1, 2, and 5-7 satisfy ai≤1000.
Inputs 8-20 satisfy no additional constraints.
Problem credits: Chongtian Ma, Jesse Choe, and Yuval Vaknin

USACO2022年12月美国计算机奥赛竞赛银奖组问题一——Barn Tree

Note: the time limit for this problem is 4s, two times the default. The memory limit is also twice the default.
Farmer John's farm has N barns (2≤N≤2⋅105) numbered 1…N. There are N−1 roads, where each road connects two barns and it is possible to get from any barn to any other barn via some sequence of roads. Currently, the jth barn has hj hay bales (1≤hj≤109).

To please his cows, Farmer John would like to move the hay such that each barn has an equal number of bales. He can select any pair of barns connected by a road and order his farmhands to move any positive integer number of bales less than or equal to the number of bales currently at the first barn from the first barn to the second.

Please determine a sequence of orders Farmer John can issue to complete the task in the minimum possible number of orders. It is guaranteed that a sequence of orders exists.

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

The first line of input contains the value of N.

The second line of input contains the space-separated values of hj for j=1…N.

The final N−1 lines of input each contain two space-separated barn numbers ui vi, indicating that there is a bidirectional road connecting ui and vi.

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

Output the minimum possible number of orders, followed a sequence of orders of that length, one per line.
Each order should be formatted as three space-separated positive integers: the source barn, the destination barn, and the third describes the number of hay bales to move from the source to the destination.

If there are multiple solutions, output any.

SAMPLE INPUT:

4
2 1 4 5
1 2
2 3
2 4

SAMPLE OUTPUT:

3
3 2 1
4 2 2
2 1 1

In this example, there are a total of twelve hay bales and four barns, meaning each barn must have three hay bales at the end. The sequence of orders in the sample output can be verbally translated as below:

From barn 3 to barn 2, move 1 bale.
From barn 4 to barn 2, move 2 bales.
From barn 2 to barn 1, move 1 bale.

SCORING:

Test cases 2-8 satisfy N≤5000
Test cases 7-10 satisfy vi=ui+1
Test cases 11-16 satisfy no additional constraints
Problem credits: Aryansh Shrivastava

USACO2022年12月美国计算机奥赛竞赛金奖组问题三——Strongest Friendship Group

Farmer John has N cows (2≤N≤105), conveniently labeled 1…N. There are M (1≤M≤2⋅105) pairs of friends among these cows.
A group of cows is called a "friendship group" if every cow in the group is reachable from every other cow in the group via a chain of friendships that lies solely within the group (friendships connecting to cows outside the group have no impact). The "strength" of a friendship group is the minimum number of friends of any cow in the group within the group times the number of cows in the group (again, note that friendships connecting to cows outside the group do not count for this definition).

Please find the maximum strength over all friendship groups.

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

The first line contains N and M.
The next M lines contain two integers ui and vi denoting that cows ui and vi are friends (1≤ui,vi≤N, ui≠vi). No unordered pair of cows appears more than once.

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

One line containing the maximum strength over all friendship groups.

SAMPLE INPUT:

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

SAMPLE OUTPUT:

12
The maximum strength can be observed to be with the group of cows numbered 1,2,3,4. The minimum number of friends of any cow in this group within the group is 3, so the answer is 4⋅3=12.

SCORING:

For 1≤T≤3, test case T satisfies N≤16.
For 4≤T≤9, test case T satisfies N≤1000.
For 10≤T≤20, test case T satisfies no additional constraints.
Problem credits: Benjamin Qi

USACO2022年12月美国计算机奥赛竞赛金奖组问题二——Mountains

There are NN (1≤N≤20001≤N≤2000) evenly spaced mountains in a row on the edge of Farmer John's farm. These can be expressed as an array of heights h1,h2,…,hNh1,h2,…,hN. For a mountain ii, you can see another mountain jj if there are no mountains strictly higher than the line of sight connecting the tops of mountain jj and ii. Formally, for two mountains i<ji<j, they can see each other if there is no kk such that i<k<ji<k<j and (k,hk)(k,hk) is above the line segment connecting (i,hi)(i,hi) and (j,hj)(j,hj). There are QQ (1≤Q≤20001≤Q≤2000) updates where the height of one mountain increases. Find the total number of unordered pairs of mountains that see each other after each update.

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

Line 11 contains NN.Line 22 contains NN heights h1,h2,…,hNh1,h2,…,hN (for each ii, 0≤hi≤1090≤hi≤109).

Line 33 contains QQ.

Lines 44 to 3+Q3+Q contain xx, yy (1≤x≤N1≤x≤N, 1≤y1≤y) where xx is the index of the mountain and yy is the amount the height increases by. It is guaranteed that the new height of the mountain is at most 109109.

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

QQ lines, with the total number of unordered pairs of mountains that see each other after each update.

SAMPLE INPUT:

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

SAMPLE OUTPUT:

7
10
7

Initially, the following pairs of mountains can see each other: (1,2)(1,2), (2,3)(2,3), (2,5)(2,5), (3,4)(3,4), (3,5)(3,5), (4,5)(4,5), a total of 66.

After the first update, mountain 44 has a height of 44, which doesn't block any visibility but does make it so that 44 can now see 22, making the new answer 77.

After the second update, mountain 11 has a height of 55, which doesn't block any visibility but does make it so that 11 can now see 33, 44, and 55, making the new answer 1010.

After the third update, mountain 33 has a height of 55, which blocks mountain 11 from seeing mountain 44, blocks mountain 22 from seeing mountains 44 and 55, and doesn't allow itself to see any more mountains since it can already see all of them, making the new answer 77.

SCORING:

Tests 2-5 satisfy N,Q≤100N,Q≤100.

Tests 6-11 satisfy Q≤10Q≤10.

Tests 12-21 have no additional constraints.

Problem credits: Joe Li and Larry Xing

USACO2022年12月美国计算机奥赛竞赛金奖组问题一——Bribing Friends

Bessie wants to watch Bovine Genomics: The Documentary, but she doesn’t want to go alone. Unfortunately, her friends aren’t enthusiastic enough to go with her! Therefore, Bessie needs to bribe her friends to accompany her to the movie theater. She has two tools in her bribery arsenal: mooney and ice cream cones.

Bessie has NN (1≤N≤20001≤N≤2000) friends. However, not all friends are created equal! Friend ii has a popularity score of PiPi (1≤Pi≤20001≤Pi≤2000), and Bessie wants to maximize the sum of the popularity scores of the friends accompanying her. Friend ii is only willing to accompany Bessie if she gives them CiCi (1≤Ci≤20001≤Ci≤2000) moonies. They will also offer her a discount of 11 mooney if she gives them XiXi (1≤Xi≤20001≤Xi≤2000) ice cream cones. Bessie can get as many whole-number discounts as she wants from a friend, as long as the discounts don’t cause the friend to give her mooney.

Bessie has AA moonies and BB ice cream cones at her disposal (0≤A,B≤20000≤A,B≤2000). Help her determine the maximum sum of the popularity scores she can achieve if she spends her mooney and ice cream cones optimally!

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

Line 11 contains three numbers NN, AA, and BB, representing the number of friends, the amount of mooney, and the number of ice cream cones Bessie has respectively.

Each of the next NN lines contains three numbers, PiPi, CiCi, and XiXi, representing popularity (PiPi), mooney needed to bribe friend ii to accompany Bessie (CiCi), and ice cream cones needed to receive a discount of 11 mooney from friend ii (XiXi).

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

Output the maximum sum of the popularity scores of the friends accompanying Bessie, assuming she spends her moonie and ice cream cones optimally.

SAMPLE INPUT:

3 10 8
5 5 4
6 7 3
10 6 3

SAMPLE OUTPUT:

15

Bessie can give 44 moonies and 44 ice cream cones to cow 11, and 66 moonies and 33 ice cream cones to cow 33, in order to get cows 11 and 33 to accompany her for a total popularity of 5+10=155+10=15.

SCORING:

Test cases 2-4 satisfy N≤5N≤5 and Ci=1Ci=1

Test cases 5-7 satisfy B=0B=0

Test cases 8-10 satisfy N,A,B,Pi,Ci,Xi≤50N,A,B,Pi,Ci,Xi≤50

Test cases 11-15 satisfy N,A,B,Pi,Ci,Xi≤200N,A,B,Pi,Ci,Xi≤200

Test cases 16-20 satisfy no further constraints

Problem credits: Timothy Feng, Nathan Wang, and Sam Zhang

USACO2022年12月美国计算机奥赛竞赛白金组问题三——Palindromes

The United Cows of Farmer John (UCFJ) are at the annual hoofball championships! UCFJ's team of NN(1≤N≤7500)(1≤N≤7500) cows won a gold medal in hoofball, narrowly beating out Farmer Nhoj's team.

The cows have already lined up for the awards ceremony. They want FJ to take N(N+1)2N(N+1)2 group photos, one for each contiguous subsequence of the lineup.

However, FJ, as the coach of the team, is very particular about how the cows should be lined up. Specifically, he refuses to take a picture of a subsequence unless it forms a *palindrome,* meaning that the breed of the iith cow from the left end of the subsequence must be the same as the breed of the iith cow from the right end of the subsequence for all positive integers iiless than or equal to the length of the subsequence. Each cow's breed is either Guernsey or Holstein.

For each of the N(N+1)2N(N+1)2 contiguous subsequences of the lineup, count the minimum number of transpositions necessary to rearrange that subsequence into a palindrome (or −1−1if it is impossible to do so). A single transposition consists of taking two adjacent cows in the subsequence and swapping them. Output the sum of all these counts.

Note that the number of transpositions needed is calculated independently for each contiguous subsequence (the cows return to their initial positions between photos).

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

The lineup, represented by a string of Gs and Hs of length NN.

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

The sum of the aforementioned quantity over all N(N+1)2N(N+1)2contiguous subsequences of the lineup.

SAMPLE INPUT:

GHHGGHHGH

SAMPLE OUTPUT:

The first four contiguous subsequences are G, GH, GHH, and GHHG. Both G and GHHG are already palindromes, so they contribute 00 to the sum. GHH can be rearranged into a palindrome using a single transposition, so it contributes 11 to the sum. GH cannot be rearranged into a palindrome using any number of transpositions, so it contributes −1−1 to the sum.

Another contiguous subsequence that contributes to the sum is HHGG. This can be rearranged into a palindrome using two transpositions.

SCORING:

There are fifteen test cases aside from the sample, one for each of N∈[100,200,500,1000,2000,5000,5000,5000,5000,5000,7500,7500,7500,7500,7500]N∈[100,200,500,1000,2000,5000,5000,5000,5000,5000,7500,7500,7500,7500,7500].

Problem credits: Mythreya Dharani and Benjamin Qi

USACO2022年12月美国计算机奥赛竞赛白金组问题二——Making Friends

Note: the time limit for this problem is 3s, 50% larger than the default. The memory limit is twice the default.

There are initially MM (1≤M≤2⋅1051≤M≤2⋅105) pairs of friends among FJ's NN (2≤N≤2⋅1052≤N≤2⋅105) cows labeled 1…N1…N. The cows are leaving the farm for vacation one by one. On day ii, the ii-th cow leaves the farm, and all pairs of the ii-th cow's friends still present on the farm become friends. How many new friendships are formed in total?

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

The first line contains NN and MM.The next MM lines contain two integers uiui and vivi denoting that cows uiui and vivi are friends (1≤ui,vi≤N1≤ui,vi≤N, ui≠viui≠vi). No unordered pair of cows appears more than once.

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

One line containing the total number of new friendships formed. Do not include pairs of cows that were already friends at the beginning.

SAMPLE INPUT:

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

SAMPLE OUTPUT:

5

On day 11, three new friendships are formed: (3,4)(3,4), (3,7)(3,7), and (4,7)(4,7).

On day 33, two new friendships are formed: (4,5)(4,5) and (5,7)(5,7).

SCORING:

Test cases 2-3 satisfy N≤500N≤500.

Test cases 4-7 satisfy N≤104N≤104.

Test cases 8-17 satisfy no additional constraints.
Problem credits: Benjamin Qi

USACO2022年12月美国计算机奥赛竞赛白金组问题一——Breakdown

Note: the time limit for this problem is 3s, 50% larger than the default.

Farmer John's farm can be represented as a directed weighted graph, with roads (edges) connecting different nodes, and the weight of each edge being the time required to travel along the road. Every day, Bessie likes to travel from the barn (located at node 11) to the fields (located at node NN) traveling along exactly KK roads, and wants to reach the fields as quickly as possible under this constraint. However, at some point, the roads stop being maintained, and one by one, they start breaking down, becoming impassable. Help Bessie find the shortest path from the barn to the fields at all moments in time!

Formally, we start with a complete weighted directed graph on NN vertices (1≤N≤3001≤N≤300) with N2N2 edges: one edge for every pair (i,j)(i,j) for 1≤i,j≤N1≤i,j≤N (note that there are NN self loops). After each removal, output the minimum weight of any path from 11 to NN that passes through exactly KK (not necessarily distinct) edges (2≤K≤82≤K≤8). Note that after the ii-th removal, the graph has N2−iN2−i edges left.

The weight of a path is defined as the sum of the weights of all of the edges on the path. Note that a path can contain multiple of the same edge and multiple of the same vertex, including vertices 11 and NN.

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

The first line contains NN and KK.The next NN lines contain NN integers each. The jj-th integer of ii-th line is wijwij (1≤wij≤1081≤wij≤108).

Then N2N2 additional lines follow, each containing two integers ii and jj (1≤i,j≤N1≤i,j≤N). Every pair of integers appears exactly once.

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

Exactly N2N2 lines, the minimum weight KK-path after each removal. If no KK-path exists then output −1−1.

SAMPLE INPUT:

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

SAMPLE OUTPUT:

11
18
22
22
22
-1
-1
-1
-1

After the first removal, the shortest 44-path is:
1 -> 2 -> 3 -> 2 -> 3
After the second removal, the shortest 44-path is:
1 -> 3 -> 2 -> 1 -> 3

After the third removal, the shortest 44-path is:
1 -> 3 -> 3 -> 3 -> 3

After six removals, there is no longer a 44-path.

SCORING:

For 2≤T≤142≤T≤14, test case TT satisfies K=⌊(T+3)/2⌋K=⌊(T+3)/2⌋.

Problem credits: Benjamin Qi

国内外学生参加USACO竞赛有何优势?USACO竞赛语言怎么选?

对于未来想要出国同学,尤其是想要在计算机专业方面有所深造的同学来说,参加一个高含金量的学术活动是尤为必要的。对于刚接触USACO学术活动的同学来说,如何选择一门适合自己的编程语言是尤为重要的。

USACO学术活动中使用的语言包括C++、Java、Python、C和Pascal。

2022年USACO公开赛使用语言统计

从上图中可以看出:同类语言合并之后,C++语言的使用人数最多,接下来使用人数比较多的语言就是Java语言,再者就是Python语言,最后就是C语言。

按照使用人数排名为: C++ > Java > Python > C

其中,C++是最受欢迎的语言。这一结果并非偶然,因为USACO学术活动注重考察选手在程序中如何高效地使用时间和空间。而C++语言则是高效且非常方便的一种语言,尤其在USACO的高级问题中更是展现出了强大的优势。此外,C++还引入了面向对象的概念,使用数据结构和算法库也更加便捷,从而使编写代码更加简单。

Java语言在USACO学术活动中排名第二,尽管其效率比C++略低,但USACO考试为Java编写的程序留出了更多时间来弥补其效率不足的缺点。此外,Java是一门面向对象的综合性语言设计,摆脱了C++中较难的指针等概念,易于学习和使用。作为AP学生,Java 是AP计算机课程指定的编程语言,对于准备出国留学的AP学生来说是非常不错的选择。

Python语言在USACO学术活动中排名第三,其效率甚至比Java还低。不过,USACO考试为Python的执行留出了更多的时间。Python是一种脚本语言,其优势不在于效率,而在于方便。这种语言对于掌握者来说很容易上手。

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

咨询报名注意事项+预约试听体验课

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

总结

国内外学生参加USACO学术活动有何优势?

在USACO学术活动的高级别题目中,C++ 的优势就会特别明显,从长远的应用上来看,C++ 确实是更具有优势一些。这几种语言本身并没有好坏之分,对于参加USACO比赛来说,也并非只有C++才是最佳选择。相反,如果你擅长其他语言,那么使用其他语言也同样可行。

对国内学生

USACO是一项非常可以检验并提升实力的比赛,特别是对于参加国内信奥学术活动的同学来说。通过参加USACO比赛,不仅可以在荣誉册上增添自己的成就,还可以提高自己在计算机科学领域的实力。

国外学生

对于计划申请出国留学的学生来说,获得USACO金或白金级别的奖项绝对是一笔价值千金的宝藏。特别是对于那些热爱计算机科学,未来计划申请计算机专业的同学而言,参加USACO比赛更是必不可少的活动之一。