2023-2024年USACO競賽金級真題解析!

前兩(liang) 天我們(men) 針對2023-24賽季USACO競賽第一場晉級賽銅級別和銀級別真題進行了詳細的解析。

今天為(wei) 大家帶來的是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.

⭐機構解析

從(cong) 相鄰位置來考慮比較簡單,如果i到i+1路徑為(wei) 奇數,說明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,那麽(me) 我們(men) 要求最長路的話就可以利用拓撲排序/bfs來解決(jue) 。

難點在於(yu) 題目要求字典序最小,考慮如何快速比較兩(liang) 條出邊的字典序大小

由於(yu) 題目權值可以相同暴力走下去比較時間複雜度會(hui) 被卡到$O(n^2)$

為(wei) 了優(you) 化比較過程,我們(men) 希望能夠快速得到兩(liang) 個(ge) 最長路相同的點他們(men) 之間的字典序關(guan) 係。

所以我們(men) 可以動態地對最長路相同的點的內(nei) 部做一個(ge) 排序,比較的時候就隻需要拿出之前維護好的排序結果去做判斷,判斷時間複雜度就可以做到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:查詢的次數很多,可以考慮預處理,由於(yu) 不需要頻繁修改區間,使用前綴和即可。觀察2:題目保證數軸x1...xN上存在一個(ge) 極小值點y,通過等式變形/反證法可以證明y必定為(wei) x1...xN中的一點,同時路徑總和f(y)是一個(ge) 兩(liang) 邊往中間遞減的單峰函數,可以使用三分法快速逼近y的最優(you) 值,三分法模板。

時間複雜度: O(Tlogn)

知識點:三分

【競賽報名/項目谘詢+微信:mollywei007】

上一篇

早申後還剩多少offer?揭秘名校RD輪表象下的申請難度!

下一篇

考上985還有沒有必要出國留學?985高校境外留學率大匯總!

你也可能喜歡

  • 暫無相關文章!

評論已經被關(guan) 閉。

插入圖片
返回頂部