CF1419D2.Sage's Birthday (hard version)

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The difference between the versions is that in the easy version all prices aia_i are different. You can make hacks if and only if you solved both versions of the problem.

Today is Sage's birthday, and she will go shopping to buy ice spheres. All nn ice spheres are placed in a row and they are numbered from 11 to nn from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal.

An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.

You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.

这是该问题的困难版本。两个版本的区别在于:在简单版本中,所有价格 aia_i 均互不相同;当且仅当你同时解决了该问题的两个版本时,你才可以进行 hack。

今天是 Sage 的生日,她将去商店购买冰球。全部 nn 个冰球排成一行,从左到右依次编号为 11 到 nn。每个冰球都有一个正整数价格。在本版本中,部分价格可以相等。

若一个冰球的价格严格小于其左右两个相邻冰球(即紧邻左侧与紧邻右侧的冰球)的价格,则称该冰球为“便宜的”。最左侧和最右侧的冰球不可能是便宜的。Sage 将选出所有便宜的冰球,并且只购买它们。

你可以在 Sage 到来之前先去商店,并按任意顺序重新排列这些冰球。请找出 Sage 最多能买多少个冰球,并给出一种达到该最大值的冰球重排方案。

输入格式

The first line contains a single integer nn (1≤n≤105)(1 \le n \le 10^5) — the number of ice spheres in the shop.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤109)(1 \le a_i \le 10^9) — the prices of ice spheres.

第一行包含一个整数 nn (1≤n≤105)(1 \le n \le 10^5) —— 商店中冰球的数量。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤109)(1 \le a_i \le 10^9) —— 冰球的价格。

输出格式

In the first line print the maximum number of ice spheres that Sage can buy.

In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.

第一行输出 Sage 最多能购买的冰球数量。

第二行按最优顺序输出冰球的价格。如果存在多个正确答案,输出任意一个即可。

输入输出样例

  • 输入#1

    7
    1 3 2 2 4 5 4

    输出#1

    3
    3 1 4 2 4 2 5

说明/提示

In the sample it's not possible to place the ice spheres in any order so that Sage would buy 44 of them. If the spheres are placed in the order (3,1,4,2,4,2,5)(3, 1, 4, 2, 4, 2, 5), then Sage will buy one sphere for 11 and two spheres for 22 each.

在样例中,无法以任何顺序摆放冰球,使得 Sage 购买其中的 44 个。若冰球按顺序 (3,1,4,2,4,2,5)(3, 1, 4, 2, 4, 2, 5) 排列,则 Sage 将以单价 11 购买一个冰球,以单价 22 购买两个冰球。

输入解题思路,AI测评打分。不知道怎么写?

首页