QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100 Hackable ✓

#13969. 楼梯计数

Statistics

松崎海(Umi Matsuzaki)正在升起信号旗,祝航海者们好运。她有 $N$ 面高度不同的旗帜。她的 $N$ 面旗帜由互不相同的整数 $a_1, a_2, a_3, \dots, a_N$ 表示,其中 $a_i$ 对应第 $i$ 面旗帜的高度。

Umi 的旗帜

海发现,当她的信号旗排成被称为“楼梯”(staircases)的图案时,特别容易被看清。海知道一个特殊的宽度 $M$。然后,将“楼梯”定义为由 $2M - 1$ 面旗帜组成的序列,使得前 $M$ 面旗帜的高度构成一个递增序列,后 $M$ 面旗帜的高度构成一个递减序列。形式化地,楼梯是 $2M - 1$ 面旗帜的序列,其高度为 $b_1, b_2, \dots, b_{2M-1}$,满足 $b_1 < b_2 < \dots < b_M$ 且 $b_M > b_{M+1} > b_{M+2} > \dots > b_{2M-1}$。

海可以按任意顺序重新排列她的 $N$ 面旗帜。她注意到,在她的排列中,可能会有连续的 $2M - 1$ 面旗帜组成的小组构成楼梯。计算在考虑了她 $N$ 面旗帜的所有重新排列后,可能得到的最大楼梯数量。同时,给出一种能够产生该最大楼梯数量的旗帜排列方式。

输入格式

输入的第一行包含两个空格分隔的整数 $N$($3 \le N \le 200\,000$),表示海拥有的旗帜数量,以及 $M$($2 \le M \le \lceil \frac{N}{2} \rceil$),表示一个楼梯必须由 $2M - 1$ 面旗帜组成。

输入的第二行包含 $N$ 个空格分隔的整数 $a_1, a_2, a_3, \dots, a_N$,其中 $-10^5 \le a_i \le 10^5$。每个 $a_i$ 表示第 $i$ 面旗帜的高度,且所有的 $a_i$ 互不相同。

输出格式

输出的第一行应包含在海的 $N$ 面旗帜的某种排列中可能得到的最大楼梯数量。

输出的第二行应包含 $N$ 个空格分隔的整数,其中第 $i$ 个整数对应海的旗帜在某种最优排列中第 $i$ 面旗帜的高度。如果存在多个最优排列,你可以输出其中任意一种。

样例

样例输入 1

8 2
-5 7 1 4 -3 6 2 11

样例输出 1

3
-5 7 1 6 -3 4 2 11

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.