QOJ.ac

QOJ

Time Limit: 0.5 s Memory Limit: 128 MB Total points: 100

#14478. 旅行

الإحصائيات

在遥远的 HX 国,住着一个旅行家小 L,他希望骑着他的自行车游遍全国。在这个国家中,每个城市都有一个编号,共有 $n$ 个城市,编号从 $1$ 到 $n$。有的城市没有小 L 想去的景点,而有的城市有且仅有一个小 L 想去的景点,所有的城市都是这两种情况之一,小 L 非常热爱信息学,他编写程序给他的旅行安排了一条最短路线以到达所有他想去的景点(所有的通知旅行线路上城市编号是乱序的):他第 $1$ 个到达的城市编号为 $a_1$,第 $i$ 个到达的城市编号为 $a_i$,最后到达城市 $a_n$ 结束这次旅行。小L希望用恰好的 $m$ 个月($m < n$)的时间完成这次旅行,所以他需要制定一个理性的旅游计划。

当他抵达一个城市时,如果这个城市有他想要去的景点,他会因此获得 $1$ 点快乐值;但是若到达的城市没有他想去的景点,他会因旅途的疲惫得到 $1$ 点的疲劳值:一个月的时间足够他游玩任意多个城市,但他也希望拿出一点时间来休息。他每个月总是在本月所到达的最后一个城市休息(但如果这个城市有景点,那么小 L 总会游玩这个景点再休息)。当然,小 L 希望每个月都能有一定的旅行任务。即便这个月他所到达的城市中并没有他想去的的景点,换句话说,每个月他都会至少到达一个新的城市。

小 L 无法自己安排旅行计划,所以求助于你。你需要告诉他一个序列:

$$x_1,x_2,\ldots,x_m$$

$x_i$ 表示小 L 第 $i$ 个月休息时,他所在的城市编号。由于他最后一个月必须完成他的旅行,所以 $x_m$ 肯定等于 $a_n$,例如,设 $n=5$,$m=3$,$(a_1,a_2,a_3,a_4,a_5)=(3,2,4,1,5)$,$(x_1,x_2,x_3)=(2,1,5)$,这意味着:第 $1$ 个月先后到达 $3$ 号和 $2$ 号城市,并在 $2$ 号城市休息:第 $2$ 个月先后到达 $4$ 号和 $1$ 号城市,并在 $1$ 号城市休息:第 $3$ 个月到达 $5$ 号城市,并在 $5$ 号城市休息。

这样的方案序列有很多种,设每种方案序列中第 $i$ 个月旅行中当月获得的快乐值与疲劳值的差绝对值为 $d_i$,设第 $k$ 种方案序列中求出的 $d_1,d_2,\ldots,d_m$ 这个 $m$ 值的最大值为 $c_k$,小 L 希望所选择的方案序列的 $c_k$ 在所有方案序列中是最小的。

事实上,可能有多个方案序列的 $c_k$ 达到并列最小值。由于小 L 喜爱编程,他患上了一定的强迫症(虽然他自己认为他的强迫症让他炫的发黄),他希望给他的序列是这多个方案中字典序最小的。。

Tips:比较两个序列字典序即比较第一个不相同数字的大小,如 $(1,2,3,4)<(1,2,4,3)$。

输入格式

第一行为两个空格隔开的正整数 $n, m$,表示旅行的城市数与旅行所花的月数。

接下来 $n$ 行,其中第 $i$ 行包含两个空格隔开的整数 $a_i$ 和 $b_i$,$a_i$ 表示他第 $i$ 个去的城市编号,$b_i$ 为 $0$ 或 $1$。如果 $b_i=0$ 则表示城市 $a_i$ 没有小 L 想去的景点,如果 $b_i=1$ 则表示城市 $a_i$ 有小 L 想去的景点。

$a_i$ 两两不同且有 $1\leq a_i \leq N$,即 $\{a_i\}$ 为 $1,2,\ldots,n$ 的一个排列,例如 $\{2, 1, 3, 4, …, n\}$。

输出格式

样例数据

样例 1 输入

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

样例 1 输出

1 6 8

样例 1 解释

第 1 个月得到 2 点快乐值与 2 点疲劳值,第 2 个月得到 1 点快乐值与 1 点疲劳值,第 3 个月得到 1 点快乐值与 1 点疲劳值。3 个月中疲劳值与快乐值差的最大值为 0,达到所有方案最小值。

可行方案有:

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

其中 1 6 8 字典序最小。

样例 2 输入

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

样例 2 输出

2 1 5 6 7 8

样例 2 解释

6 个月中每个月获得的疲劳值与快乐值差均为 1,所以差的最大值为 1,达到所有方案最小值。

子任务

  • 对于 $10\%$ 的数据,$n \le 10$;
  • 对于 $25\%$ 的数据,$m \le 10$;
  • 对于 $30\%$ 的数据,$n \le 100$;
  • 对于 $40\%$ 的数据,$m \le 100$;
  • 对于 $40\%$ 的数据,$n \le 1\,000$;
  • 对于 $55\%$ 的数据,$n \le 180\,000$;
  • 对于 $100\%$ 的数据,$n \le 500\,000$,$m \le 200\,000$。
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.