CodeForces 1150C Prefix Sum Primes
程序员文章站
2024-02-15 21:56:10
...
解题思路
题目说给你一个个只有1和2的序列让你重新排列顺序,使前缀和具有更多的素数,每一个素数都是奇数,所以我们的排列过后应该不能放过任何一个奇数的组合可能,
#include <iostream>
using namespace std;
const int MAXN = 1e5 + 5;
int num;
int main()
{
int n;
cin >> n;
int o = 0, t = 0;
for (int i = 0; i < n; ++i)
{
cin >> num;
if (num == 1)
{
o++;
}
else
{
t++;
}
}
if (t == 0)
{
cout << 1;
for (int i = 1; i < n; ++i)
{
cout << " " << 1;
}
cout << endl;
return 0;
}
cout << 2 ;
t--;
if (o)
{
cout << " " << 1;
o--;
}
for (int i = 0; i < t; ++i)
{
cout << " " << 2;
}
for (int i = 0; i < o; ++i)
{
cout << " " << 1;
}
cout << endl;
return 0;
}