• D2. Sage‘s Birthday (hard version)


    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.....

    其实这两个题完全是一个题目,就是一个基本的贪心....不明白为什么分得分成两种情况

    1. #include
    2. # include
    3. # include
    4. using namespace std;
    5. typedef long long int ll;
    6. int a[100000+10];
    7. int ans[100000+10];
    8. int main ()
    9. {
    10. int n;
    11. cin>>n;
    12. for(int i=1;i<=n;i++)
    13. {
    14. cin>>a[i];
    15. }
    16. sort(a+1,a+1+n);
    17. int len=1;
    18. for(int i=2;i<=n;i+=2)
    19. {
    20. ans[i]=a[len];
    21. len++;
    22. }
    23. for(int i=1;i<=n;i+=2)
    24. {
    25. ans[i]=a[len];
    26. len++;
    27. }
    28. int cnt=0;
    29. for(int i=2;i
    30. {
    31. if(ans[i]-1]&&ans[i]1])
    32. cnt++;
    33. }
    34. cout<
    35. for(int i=1;i<=n;i++)
    36. {
    37. cout<" ";
    38. }
    39. return 0;
    40. }

  • 相关阅读:
    xcode-工程设置
    进制原理
    323 - 线程的生命周期和常用方法
    【Acwing187】导弹防御系统(LIS+剪枝+贪心+dfs+迭代加深)
    vue2中,使用sortablejs组件和vuedraggable实现拖拽排序功能
    如何免费将XPS转换为PDF格式
    铭飞MCms不建议使用
    多线程---synchronized特性+原理
    基于 AdaFace 提供适合低质量人脸识别的人脸特征向量输出服务
    【节能学院】剩余电流动作继电器在浴室中的应用
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126227077