QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#20859. 午睡排序

Statistics

Bessie 想用自己的算法给整数数组排序。她有一堆 $N$ 个整数 $a_1,a_2,\dots,a_N$,满足 $(1 \leq N \leq 2\cdot 10^5)$ 和 $(1 \leq a_i \leq 10^{11})$,要将它们按有序顺序放入另一个数组。她反复找出这堆整数中的最小值,将其取走并添加到数组末尾。从一堆 $p$ 个整数中找出最小值需要 $p$ 秒。

Farmer John 让农场里其他一些奶牛帮助 Bessie,但她们很懒,于是 Bessie 利用了这一点。她将整数分成 Bessie 堆和助手堆。对于 Bessie 堆中的每个整数,她照常执行自己的算法。助手堆中的每个整数则交给不同的助手奶牛。Farmer John 的农场很大,因此她可以找任意多名助手。如果助手收到整数 $a_i$,Bessie 就让她睡 $a_i$ 秒,醒来后立刻将这个整数添加到数组末尾。如果 Bessie 与助手同时添加整数,因为她是领头的,她的整数会先加入。如果多名助手被分配到同一个整数,她们会同时将该整数的多个副本加入数组。

帮助 Bessie 分配整数,使最终数组有序,并使排序所需时间最短。

输入格式

第一行包含独立测试用例数 $T$($1\le T\le 10$)。

每个测试用例的格式如下:

每个测试用例的第一行包含 Bessie 数组中的整数个数 $N$。

下一行包含要排序的整数 $a_1, a_2, \dots, a_N$。同一整数可以出现多次。

保证所有测试用例的 $N$ 之和不超过 $2\cdot 10^5$。

输出格式

对于每个测试用例,输出最优分配下排序数组所需的最短时间,每个答案占一行。

样例

输入格式 1

4
5
1 2 4 5 100000000000
5
17 53 4 33 44
4
3 5 5 5
6
2 5 100 1 4 5

输出格式 1

6
15
5
6

说明

第一个例子中,Bessie 可以将 $1,2$ 分配给助手,自己留下 $4,5,10^{11}$。

时间 事件
1 助手加入 1
2 助手加入 2
3 Bessie 加入 4
5 Bessie 加入 5
6 Bessie 加入 $10^{11}$

第二个例子中,最优方案是全部由 Bessie 自己排序。一种不可行的分配是让助手处理 $4$,其余留给自己,因为这样她会在助手加入 $4$ 前将 $17$ 加入数组。

第三个例子中,她可以将所有整数分配给助手。

第四个例子中,她可以将 $1,4,5$ 分配给助手,自己留下 $2,5,100$。

时间 事件
1 助手加入 1
3 Bessie 加入 2
4 助手加入 4
5 Bessie 加入 5
5 助手加入 5
6 Bessie 加入 100

子任务

  • 输入 2:$N\le 16$
  • 输入 3–5:$N\le 150$
  • 输入 6–8:$\sum N\le 5000$
  • 输入 9–11:没有额外限制。

题目来源:Suhas Nagar

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.