C. Prefix Sum Primes

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
We’re giving away nice huge bags containing number tiles! A bag we want to present to you contains n tiles. Each of them has a single number written on it — either 1 or 2.

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 n (1≤n≤200000) — the number of number tiles in the bag. The following line contains n space-separated integers a1,a2,…,an (ai∈{1,2}) — the values written on the tiles.

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

Examples
inputCopy
5
1 2 1 2 1
outputCopy
1 1 1 2 2
inputCopy
9
1 1 2 1 1 1 2 1 1
outputCopy
1 1 1 2 1 1 1 2 1
Note
The first solution produces the prefix sums 1,2,3,5,7 (four primes constructed), while the prefix sums in the second solution are 1,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.

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<vector>
#define ll long long
#define dd double
using namespace std;

ll b[400000];

ll sushu(ll x) {
	if (x == 1) {//1不是质数
		return 1;
	}
	if (x == 2) {//2是质数
		return 2;
	}
	ll flag1 = 0;
	for (ll i = 2; i <= sqrt(x); i++) {
		if (x % i == 0) {//如果这个数不是质数,返回1,是质数返回2
			flag1 = 1;
			break;
		}
	}
	if (flag1 == 1) {
		return 1;
	}
	else {
		return 2;
	}
}

int main() {
	ll n;
	cin >> n;//输入一个数n代表n个数字
	ll q;
	ll count1 = 0;
	ll count2 = 0;
	for (ll i = 0; i < n; i++) {
		cin >> q;
		if (q == 1) {//存储输入的n个数字种1的数量和2的数量
			count1++;
		}
		else {
			count2++;
		}
	}
	ll sum = 0;
	ll z;
	ll v = 0;
	if (n == 2&&count1==1&&count2==1) {
		cout << "2 1" << endl;
	}
	else {
		for (ll i = 0; i < n; i++) {
			z = sushu(sum + 1);//判断sum+1是否是素数,如果是的话+1.不是的话,
								//看2的数量够不够,如果够就+2,不够继续+1
			if (z == 1) {
				if (count2 != 0) {//如果2的数量不为0进入循环
					b[v++] = 2;//将2存入数组b
					count2--;//将2的数量-1
					sum += 2;//整体数量+2
				}
				else {
					b[v++] = 1;//如果2的数量不够用了,就+1
					count1--;//1的数量-1
				}
			}
			else {
				if (count1 != 0) {//同上
					b[v++] = 1;
					sum += 1;
					count1--;
				}
				else {
					b[v++] = 2;
					sum += 2;
					count2--;
				}
			}
		}
		for (ll i = 0; i < v; i++) {//输出序列即为可以得到最多的素数
			cout << b[i] << " ";
		}
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值