• A. Prefix Sum Primes


    A. Prefix Sum Primes

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    We're giving away nice huge bags containing number tiles! A bag we want to present to you contains nn tiles. Each of them has a single number written on it — either 11 or 22.

    However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get.

    Can you win the prize? Hurry up, the bags are waiting!

    Input

    The first line of the input contains a single integer nn (1≤n≤2000001≤n≤200000) — the number of number tiles in the bag. The following line contains nn space-separated integers a1,a2,…,ana1,a2,…,an (ai∈{1,2}ai∈{1,2}) — the values written on the tiles.

    Output

    Output a permutation b1,b2,…,bnb1,b2,…,bn of the input sequence (a1,a2,…,an)(a1,a2,…,an) maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any.

    Examples

    input

    Copy

    5
    1 2 1 2 1
    

    output

    Copy

    1 1 1 2 2
    

    input

    Copy

    9
    1 1 2 1 1 1 2 1 1
    

    output

    Copy

    1 1 1 2 1 1 1 2 1
    

    Note

    The first solution produces the prefix sums 1,2,3,5,71,2,3,5,7 (four primes constructed), while the prefix sums in the second solution are 1,2,3,5,6,7,8,10,111,2,3,5,6,7,8,10,11 (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.

    =========================================================================

    首先显然是先选2比较好,因为相对于2,1会更加灵活,如果耗费了1再选2,那么2就会变得“愚钝”,也就是会忽略很多质数,但是如果我们配合着1,2就变得灵活起来,是贪心的思想

    另外本题非常考验细节,比如中途全部或者某个用完,最终没用完的情况都需要考虑到

    1. #include
    2. # include
    3. using namespace std;
    4. int prime[200000*2+10];
    5. bool not_prime[200000*2+10];
    6. int tot;
    7. void init()
    8. {
    9. for(int i=2;i<=200000*2;i++)
    10. {
    11. if(!not_prime[i])
    12. {
    13. tot++;
    14. prime[tot]=i;
    15. }
    16. for(int j=1;j<=tot&&prime[j]*i<=200000*2;j++)
    17. {
    18. not_prime[i*prime[j]]=1;
    19. if(i%prime[j]==0)
    20. break;
    21. }
    22. }
    23. }
    24. vector<int>v;
    25. int main()
    26. {
    27. //若是差别为奇数
    28. //若干2和1,或者奇数个1
    29. //差别是偶数,上2
    30. //无法构成的情况,差偶数时,奇数个1,2还不够 差奇数时,1不够
    31. init();
    32. int n;
    33. cin>>n;
    34. int cnt1=0,cnt2=0;
    35. for(int i=1;i<=n;i++)
    36. {
    37. int x;
    38. cin>>x;
    39. if(x==1)
    40. cnt1++;
    41. else
    42. cnt2++;
    43. }
    44. int now=1;
    45. int pre=0;
    46. for(int i=1;i<=tot;i++)
    47. {
    48. pre=prime[i-1];
    49. if(prime[i]-pre>cnt1+cnt2*2)
    50. {
    51. for(int j=1;j<=cnt1;j++)
    52. {
    53. cout<<1<<" ";
    54. }
    55. for(int j=1;j<=cnt2;j++)
    56. {
    57. cout<<2<<" ";
    58. }
    59. return 0;
    60. }
    61. int temp=prime[i]-pre;
    62. if(temp/2>=cnt2)
    63. {
    64. for(int j=1;j<=cnt2;j++)
    65. {
    66. cout<<2<<" ";
    67. }
    68. int nex=temp-cnt2*2;
    69. for(int j=1;j<=min(cnt1,nex);j++)
    70. {
    71. cout<<1<<" ";
    72. }
    73. cnt1-=nex;
    74. cnt2=0;
    75. }
    76. else
    77. {
    78. cnt2-=temp/2;
    79. for(int j=1;j<=temp/2;j++)
    80. {
    81. cout<<2<<" ";
    82. }
    83. int nex=temp%2;
    84. for(int j=1;j<=min(cnt1,nex);j++)
    85. {
    86. cout<<1<<" ";
    87. }
    88. cnt1-=nex;
    89. }
    90. }
    91. for(int i=1;i<=cnt1;i++)
    92. {
    93. cout<<1<<" ";
    94. }
    95. for(int i=1;i<=cnt2;i++)
    96. {
    97. cout<<2<<" ";
    98. }
    99. return 0;
    100. }

  • 相关阅读:
    SpringBoot3快速入门
    【16】c++11新特性 —>独占智能指针unique_ptr
    C,C++中原生数组索引的奇怪写法
    如何把arguments转换为数组
    [Linux/初学者]用户组管理
    Linux 基金会执行董事 Jim Zemlin:开源如何成为创新的关键推动力
    日常开发中的图片处理系列工具
    计算属性与watch的区别,fetch与axios在vue中的异步请求,单文本组件使用,使用vite创建vue项目,组件的使用方法
    链表小题.Play Train AtCoder - abc225_d
    windows下一键安装JDK1.8
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126345293