#P3229. [HNOI2013] 旅行

    ID: 2278 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>数学2013单调队列湖南高斯消元

[HNOI2013] 旅行

Description

In the distant country of HX, there lives a traveler named Xiao L. He wants to ride his bicycle across the entire country. There are nn cities in this country, numbered from 11 to nn.

Some cities have no attraction that Xiao L wants to visit, while some cities have exactly one such attraction. Every city is one of these two types. As an information science enthusiast, he wrote a program that arranged a shortest route to reach all the attractions he wants to visit (the city numbers along the route are in an arbitrary order). He reaches city a1a_1 first, city aia_i as the ii-th city, and finally ends the trip at city ana_n. Xiao L wants to finish this trip in exactly mm months (m<nm < n), so he needs a sensible plan.

When he arrives at a city, if that city has an attraction he wants to visit, he gains 11 point of happiness; otherwise, he gains 11 point of fatigue due to the tiring journey. One month is enough for him to travel through any number of cities, but he also wants to take some time to rest. He always rests in the last city he reaches in that month (if this city has an attraction, Xiao L will always visit the attraction before resting). Of course, Xiao L wants to have some travel tasks every month. Even if none of the cities he reaches that month has an attraction, he will still arrive at least one new city in that month.

Xiao L cannot arrange the travel plan by himself, so he asks for your help. You need to provide a sequence x1,x2,,xmx_1, x_2, \ldots, x_m, where xix_i is the city number where Xiao L rests in the ii-th month. Since he must finish his trip in the last month, xmx_m must equal ana_n. For example, let n=5n = 5, m=3m = 3, (a1,a2,a3,a4,a5)=(3,2,4,1,5)(a_1, a_2, a_3, a_4, a_5) = (3, 2, 4, 1, 5), and (x1,x2,x3)=(2,1,5)(x_1, x_2, x_3) = (2, 1, 5). This means: in month 11 he reaches cities 33 and 22 in order and rests in city 22; in month 22 he reaches cities 44 and 11 in order and rests in city 11; in month 33 he reaches city 55 and rests in city 55.

There are many such feasible sequences. Let did_i be the absolute value of the difference between the happiness and fatigue obtained in month ii for a given plan. For the kk-th feasible plan, let ckc_k be the maximum of d1,d2,,dmd_1, d_2, \ldots, d_m. Xiao L wants to choose a plan that minimizes ckc_k among all feasible plans.

In fact, there may be multiple plans achieving the same minimal ckc_k. Since Xiao L loves programming, he has a bit of OCD (and believes that it makes him look cool). Among all the plans with minimal ckc_k, he wants the lexicographically smallest sequence.

Tips: To compare two sequences lexicographically, compare the first position where they differ, e.g., (1,2,3,4)<(1,2,4,3)(1, 2, 3, 4) < (1, 2, 4, 3).

Input Format

The first line contains two space-separated positive integers n,mn, m, denoting the number of cities and the number of months.

The next nn lines each contain two space-separated integers AiA_i and BiB_i. Here AiA_i is the city number he reaches in the ii-th step, and BiB_i is 00 or 11. If Bi=0B_i = 0, then city AiA_i has no attraction Xiao L wants to visit; if Bi=1B_i = 1, then city AiA_i has such an attraction. The AiA_i are pairwise distinct and satisfy 1Ain1 \leq A_i \leq n, i.e., {Ai}\{A_i\} is a permutation of 1,2,,n1, 2, \ldots, n.

Output Format

Output exactly one line containing mm space-separated positive integers X1,X2,,XmX_1, X_2, \ldots, X_m, which is the planned sequence of rest-city numbers for Xiao L’s trip.

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

Hint

In the first month, he gains 22 points of happiness and 22 points of fatigue. In the second month, he gains 11 point of happiness and 11 point of fatigue. In the third month, he gains 11 point of happiness and 11 point of fatigue. The maximum difference between fatigue and happiness over the 33 months is 00, which is the minimal possible value among all plans.

Feasible plans include:

  • 1 6 8
  • 3 6 8
  • 3 1 8

Among these, 1 6 8 is lexicographically smallest.

Constraints: n5×105n \leq 5 \times 10^5, m2×105m \leq 2 \times 10^5.

Translated by ChatGPT 5