Codeforces Round #556 (Div. 2) C. Prefix Sum Primes

题目大意让你改变数组的排序,使前缀和的素数最多;

这是一道模拟题,让你通过判断1和2的个数来解决,

只要特判一下1的个数是零的时候,2的个数是零的时候,或者1只有一个而2的个数又不是0个的时候,剩下的情况我们只有把1的个数分奇数和偶数来考虑,大致思路是这样,接下来就看代码吧

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 200010;
int main()
{
    int a[maxn];
    int n;
    cin >> n;
    int ans1 = 0, ans2 = 0;
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        if (a[i] == 1)ans1++;
        else ans2++;
    }

     if (ans1 == 1 && ans2 !=0)
    {
        printf("2 1");
        for (int i = 1;i < ans2;i++)printf(" 2");
    }
    else if (ans1 == 0 && ans2 != 0)
    {
        for (int i = 0;i < ans2;i++)printf("2 ");
    }
    else if (ans2 == 0 && ans1 != 0)
    {
        for (int i = 0;i < ans1;i++)printf("1 ");
    }
    else
     {
         printf("2 1");
         if (ans1 % 2 == 1)
         {
             for (int i = 1;i < ans1;i++)printf(" 1");
             for (int i = 1;i < ans2;i++)printf(" 2");
         }
         else
         {
             for (int i = 1;i < ans1-1;i++)printf(" 1");
             for (int i = 1;i < ans2;i++)printf(" 2");
             printf(" 1");
         }
     }
}

原文地址:https://www.cnblogs.com/csxaxx/p/10801378.html

时间: 2024-07-30 16:42:37

Codeforces Round #556 (Div. 2) C. Prefix Sum Primes的相关文章

Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Description Input Output Sample Input 51 2 1 2 1 Sample Output 1 1 1 2 2 题解:这个题有做慢了,这种题做慢了和没做出来区别不大... 读题的时候脑子里还意识到素数除了2都是奇数,读完之后就脑子里就只剩欧拉筛了,贪心地构造使得前缀和是连续的素数,那

Codeforces Round #556 (Div. 2) - D. Three Religions(动态规划)

Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 3000 mSec Problem Description Input Output Sample Input 6 8abdabc+ 1 a+ 1 d+ 2 b+ 2 c+ 3 a+ 3 b+ 1 c- 2 Sample Output YESYESYESYESYESYESNOYES 题解:动态规划,意识到这个题是动态规划之后难点在于要优化什么东西,本题

Codeforces Round #344 (Div. 2) E. Product Sum 二分斜率优化DP

E. Product Sum Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special. You are given an array a of

Codeforces Round #319 (Div. 2) B Modulo Sum

直接O(n*m)的dp也可以直接跑过. 因为上最多跑到m就终止了,因为sum[i]取余数,i = 0,1,2,3...,m,会有m+1种可能,m的余数只有m种必然有两个相同. #include<bits/stdc++.h> using namespace std; const int maxn = 1e3+5; int cnt[maxn]; bool dp[maxn][maxn]; #define Y { puts("YES"); return 0; } int main(

Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)

B. Odd Sum Segments time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given an array a consisting of n integers a1,a2,-,an. You want to split it into exactly k non-empty non-intersecting

Codeforces Round #475 (Div. 2) C - Alternating Sum

等比数列求和一定要分类讨论!!!!!!!!!!!! 1 #include<bits/stdc++.h> 2 #define LL long long 3 #define fi first 4 #define se second 5 #define mk make_pair 6 #define pii pair<int,int> 7 #define ull unsigned long long 8 using namespace std; 9 10 const int N=1e6+7

Codeforces Round #556 (Div. 2)

没想到平成年代最后一场cf居然是手速场,幸好拿了个小号娱乐不然掉分预定 (逃 A: 傻题. 1 /* basic header */ 2 #include <iostream> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <string> 6 #include <cstring> 7 #include <cmath> 8 #include <cstdint> 9

Codeforces Round #581 (Div. 2)-E. Natasha, Sasha and the Prefix Sums-动态规划+组合数学

Codeforces Round #581 (Div. 2)-E. Natasha, Sasha and the Prefix Sums-动态规划+组合数学 [Problem Description] ? 给你\(n\)个\(1\),\(m\)个\(-1\),他们任意排列有\(\frac{(n+m)!}{n!\cdot m!}\)中排列,每种排列都有一个最大前缀和(可能为\(0\)),求所有排列的最大前缀和之和为多少. [Solution] ? 定义\(dp[i][j]\)表示有\(i\)个\(

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i