当前位置:网站首页>D2. Sage‘s Birthday (hard version)
D2. Sage‘s Birthday (hard version)
2022-08-08 16:38:00 【秦小咩】
D2. Sage's Birthday (hard version)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
This is the hard version of the problem. The difference between the versions is that in the easy version all prices aiai 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.
Input
The first line contains a single integer nn (1≤n≤105)(1≤n≤105) — the number of ice spheres in the shop.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109) — the prices of ice spheres.
Output
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.
Example
input
Copy
7 1 3 2 2 4 5 4
output
Copy
3 3 1 4 2 4 2 5
Note
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.
=========================================================================
最尴尬的不是D1会做而D2不会做,而是用可以过D2的方法先做了D1,完事之后不敢用这个代码再去交D2.....
其实这两个题完全是一个题目,就是一个基本的贪心....不明白为什么分得分成两种情况
#include<iostream>
# include<algorithm>
# include<unordered_map>
using namespace std;
typedef long long int ll;
int a[100000+10];
int ans[100000+10];
int main ()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
int len=1;
for(int i=2;i<=n;i+=2)
{
ans[i]=a[len];
len++;
}
for(int i=1;i<=n;i+=2)
{
ans[i]=a[len];
len++;
}
int cnt=0;
for(int i=2;i<n;i++)
{
if(ans[i]<ans[i-1]&&ans[i]<ans[i+1])
cnt++;
}
cout<<cnt<<endl;
for(int i=1;i<=n;i++)
{
cout<<ans[i]<<" ";
}
return 0;
}
边栏推荐
- DASCTF部分复现
- 使用 Pygame 构建和可视化数独游戏
- Solve the inexplicable problem of MySQL violently - restart the service!
- Subject: Ordered Queue
- Spark cluster environment construction
- Digital image processing (6) -- image compression
- laravel - query builder 2
- Nervegrowold: machine advanced learning advice
- ASP.NET Core依赖注入之旅:4.体验服务的注册和消费
- 在 Fedora 上使用 SSH 端口转发
猜你喜欢
EMQ畅谈IoT数据基础软件开源版图,引领本土开源走向全球
智能指针学习笔记
【8.7】代码源 - 【抽卡】【LCM与GCD】
ESP8266-Arduino编程实例-ADXL345三轴加速计驱动
OpenAI怎么写作「谷歌小发猫写作」
DASCTF部分复现
【论文阅读】RAL 2022: Receding Moving Object Segmentation in 3D LiDAR Data Using Sparse 4D Convolutions
【 8.7 】 source code - card to LCM with GCD 】 【 】
[uniapp applet] view container cover-view
ASP.NET Core依赖注入之旅:4.体验服务的注册和消费
随机推荐
力扣207,课程表
【8.7】代码源 - 【抽卡】【LCM与GCD】
Solve the inexplicable problem of MySQL violently - restart the service!
ctfshow七夕杯复现
laravel-practice
Photoshop2021安装教程
ASP.NET Core依赖注入之旅:4.体验服务的注册和消费
Understanding of redis slice cluster
MySQL 数据库
我分析30w条数据后发现,西安新房公摊最低的竟是这里?
小米产品使用体验,问题分析及建议
spark集群环境搭建
laravel-实践
3dsmax2021软件安装教程
2022年中国全民健身发展白皮书
MySQL数据库的简介及select语句的执行流程
pdf导出工具类
科创人·优锘科技COO孙岗:错误问题找不到正确答案,求索万物可视的大美未来
高数_证明_基本初等函数的导数公式
mysql进阶(二十九)常用函数汇总