2023-2024年USACO竞赛金级真题解析!

前两天我们针对2023-24赛季USACO竞赛第一场晋级赛铜级别和银级别真题进行了详细的解析。

今天为大家带来的是12月USACO竞赛第一次晋级赛Gold级别的试题解析。

USACO竞赛最新试题

 

P1Flight Routes

Bessie recently discovered that her favorite pop artist, Elsie Swift, is performing in her new Eras Tour! Unfortunately, tickets are selling out fast, so Bessie is thinking of flying to another city to attend the concert. The Eras tour is happening in N(2≤N≤750) cities labelled 1…N, and for each pair of cities (i,j) with i<j there either exists a single direct flight from i to j or not.

A flight route from city a to city b (a<b) is a sequence of k≥2 cities a=c1<c2<⋯<ck=b such that for each 1≤i<k, there is a direct flight from city ci to city ci+1. For every pair of cities (i,j) with i<j, you are given the parity of the number of flight routes between them (0 for even, 1 for odd).

While planning her travel itinerary, Bessie got distracted and now wants to know how many pairs of cities have direct flights between them. It can be shown that the answer is uniquely determined.

INPUT FORMAT (pipe stdin):

The first line contains N. Then follow N−1 lines. The i-th line contains N−i integers. The j-th integer of the i-th line is equal to the parity of the number of flight routes from i to i+j.

OUTPUT FORMAT (pipe stdout):

Output the number of pairs of cities with direct flights between them.

SAMPLE INPUT:3111SAMPLE OUTPUT:2There are two direct flights: 1→2 and 2→3. There is one flight route from 1 to 2 and 2 to 3, each consisting of a single direct flight. There is one flight route from 1 to 3 (1→2→3).SAMPLE INPUT:51111101011SAMPLE OUTPUT:6

There are six direct flights 1→2,1→4,1→5,2→3,3→5,4→5. These result in the following numbers of flight routes:

Flight Route Counts: dest 1 2 3 4 5   1 0 1 1 1 3 2 0 0 1 0 1source 3 0 0 0 0 1 4 0 0 0 0 1 5 0 0 0 0 0which is equivalent to the sample input after taking all the numbers(mod2).SCORING:Inputs 3-4: N≤6Inputs 5-12: N≤100Inputs 13-22: No additional constraints.

⭐机构解析

从相邻位置来考虑比较简单,如果i到i+1路径为奇数,说明i到i+1有边,反之没有。

考虑任意点i和j的时候,只需要利用区间dp的思想计算出i到j除了直接到达的路径有多少条,看一下是否满足奇偶性即可。

时间复杂度: O(n^3)

知识点:区间dp

P2Minimum Longest Trip

Bessie is going on a trip in Cowland, which has N (2≤N≤2⋅10^5) towns numbered from 1 to N and M (1≤M≤4⋅10^5) one-way roads. The i-th road runs from town ai to town bi and has label li

(1≤ai,bi≤N, 1≤li≤10^9).

A trip of length k starting at town x0 is a sequence of towns x0,x1,…,xk, such that there is a road from town xi to town xi+1 for all 0≤i<k. It is guaranteed that there are no trips of infinite length in Cowland, and that no two roads connect the same pair of towns.

For each town, Bessie wants to know the longest possible trip starting at it. For some starting towns, there are multiple longest trips - out of these, she prefers the trip with the lexicographically minimum sequence of road labels. A sequence is lexicographically smaller than another sequence of the same length if, at the first position in which they differ, the first sequence has a smaller element than the second sequence.

Output the length and sum of road labels of Bessie's preferred trip starting at each town.

INPUT FORMAT (pipe stdin):The first line contains N and M. The next M lines each contain three integers ai, bi, and li, denoting a road from ai to bi with label li. OUTPUT FORMAT (pipe stdout):

Output N lines. The i-th should contain two space-separated integers, the length and sum of road labels of Bessie's preferred trip starting at town i.

SAMPLE INPUT:4 54 3 104 2 103 1 102 1 104 1 10SAMPLE OUTPUT:0 01 101 102 20SAMPLE INPUT:4 54 3 44 2 23 1 52 1 104 1 1SAMPLE OUTPUT:0 01 101 52 12SAMPLE INPUT:4 54 3 24 2 23 1 52 1 104 1 1SAMPLE OUTPUT:0 01 101 52 7SAMPLE INPUT:4 54 3 24 2 23 1 102 1 54 1 1SAMPLE OUTPUT:0 01 51 102 7SCORING:Inputs 5-6: All labels are the same.Inputs 7-8: All labels are distinct.Inputs 9-10: N,M≤5000Inputs 11-20: No additional constraints.

⭐机构解析

首先题目保证给出图是DAG,那么我们要求最长路的话就可以利用拓扑排序/bfs来解决。

难点在于题目要求字典序最小,考虑如何快速比较两条出边的字典序大小

由于题目权值可以相同暴力走下去比较时间复杂度会被卡到$O(n^2)$

为了优化比较过程,我们希望能够快速得到两个最长路相同的点他们之间的字典序关系。

所以我们可以动态地对最长路相同的点的内部做一个排序,比较的时候就只需要拿出之前维护好的排序结果去做判断,判断时间复杂度就可以做到O(1)

总时间复杂度:O(nlogn)

知识点:拓扑排序,字典序

P3Haybale Distribution

Farmer John is distributing haybales across the farm!

Farmer John's farm has N (1≤N≤2⋅10^5) barns, located at integer points x1,…,xN (0≤xi≤10^6) on the number line. Farmer John's plan is to first have N shipments of haybales delivered to some integer point y (0≤y≤10^6) and then distribute one shipment to each barn.

Unfortunately, Farmer John's distribution service is very wasteful. In particular, for some ai and bi

(1≤ai,bi≤10^6), ai haybales are wasted per unit of distance left each shipment is transported, and bi haybales are wasted per unit of distance right each shipment is transported. Formally, for a shipment being transported from point y to a barn at point x, the number of haybales wasted is given by

Given Q (1≤Q≤2⋅10^5) independent queries each consisting of possible values of (ai,bi), please help Farmer John determine the fewest amount of haybales that will be wasted if he chooses y optimally.

INPUT FORMAT(pipe stdin):The first line contains N. The next line contains x1…xN.
The next line contains Q.
The next Q lines each contain two integers ai and bi.OUTPUT FORMAT (pipe stdout):Output Q lines, the i-th line containing the answer for the i-th query.SAMPLE INPUT:5
1 4 2 3 10
4
1 1
2 1
1 2
1 4SAMPLE OUTPUT:11
13
18
30
For example, to answer the second query, it is optimal to select y=2. Then the number of wasted haybales is equal to 2(2−1)+2(2−2)+1(3−2)+1(4−2)+1(10−2)=1+0+1+2+8=13.SCORING:Input 2: N,Q≤10
Input 3: N,Q≤500
Inputs 4-6: N,Q≤5000
Inputs 7-16: No additional constraints.

⭐机构解析

观察1:查询的次数很多,可以考虑预处理,由于不需要频繁修改区间,使用前缀和即可。观察2:题目保证数轴x1...xN上存在一个极小值点y,通过等式变形/反证法可以证明y必定为x1...xN中的一点,同时路径总和f(y)是一个两边往中间递减的单峰函数,可以使用三分法快速逼近y的最优值,三分法模板。

时间复杂度: O(Tlogn)

知识点:三分

【竞赛报名/项目咨询请加微信:mollywei007】

上一篇

早申后还剩多少offer?揭秘名校RD轮表象下的申请难度!

下一篇

考上985还有没有必要出国留学?985高校境外留学率大汇总!

你也可能喜欢

  • 暂无相关文章!

评论已经被关闭。

插入图片
返回顶部
Baidu
map