Problem  Codeforces Round #556 (Div. 2) - D. Three Religions

Time Limit: 1000 mSec

Problem Description

Input

Output

Sample Input

5
1 2 1 2 1

Sample Output

1 1 1 2 2

题解:这个题有做慢了,这种题做慢了和没做出来区别不大。。。

  读题的时候脑子里还意识到素数除了2都是奇数,读完之后就脑子里就只剩欧拉筛了,贪心地构造使得前缀和是连续的素数,那实现就很简单了,将素数序列的差分序列求出来,不断凑出差分序列的每个数即可,但是之后想想,除了2 和 3,每个的间隔不都是偶数么,肯定是连续的2呀,费劲算差分序列干什么,直接先放2再放1不就行了(特殊处理一下2 和 3 即可),写着写着还误以为要输出下标,临时改了改,等到测样例的时候发现是输出1、2,暴风哭泣。

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define REP(i, n) for (int i = 1; i <= (n); i++)
  6. #define sqr(x) ((x) * (x))
  7.  
  8. const int maxn = + ;
  9. const int maxm = + ;
  10. const int maxs = + ;
  11.  
  12. typedef long long LL;
  13. typedef pair<int, int> pii;
  14. typedef pair<double, double> pdd;
  15.  
  16. const LL unit = 1LL;
  17. const int INF = 0x3f3f3f3f;
  18. const double eps = 1e-;
  19. const double inf = 1e15;
  20. const double pi = acos(-1.0);
  21. const int SIZE = + ;
  22. const LL MOD = ;
  23.  
  24. LL n;
  25. LL a[maxn];
  26. int cnt[];
  27. LL cnt1, cnt2;
  28. LL tot, prime[maxn];
  29. bool is_prime[maxn];
  30.  
  31. void Euler()
  32. {
  33. memset(is_prime, true, sizeof(is_prime));
  34. is_prime[] = is_prime[] = false;
  35. for (LL i = ; i < maxn; i++)
  36. {
  37. if (is_prime[i])
  38. {
  39. prime[tot++] = i;
  40. }
  41. for (LL j = ; j < tot && i * prime[j] < maxn; j++)
  42. {
  43. is_prime[prime[j] * i] = false;
  44. if (i % prime[j] == )
  45. {
  46. break;
  47. }
  48. }
  49. }
  50. }
  51.  
  52. vector<int> ans;
  53. queue<int> que[];
  54.  
  55. int main()
  56. {
  57. ios::sync_with_stdio(false);
  58. cin.tie();
  59. //freopen("input.txt", "r", stdin);
  60. //freopen("output.txt", "w", stdout);
  61. Euler();
  62. cin >> n;
  63. int x;
  64. LL sum = ;
  65. for (int i = ; i <= n; i++)
  66. {
  67. cin >> x;
  68. que[x].push(i);
  69. cnt[x]++;
  70. sum += x;
  71. }
  72. cnt1 = cnt[], cnt2 = cnt[];
  73. LL pre = ;
  74. for (int i = ; i < tot && prime[i] <= sum; i++)
  75. {
  76. LL tmp = prime[i] - pre;
  77. LL x = tmp / ;
  78. x = min(x, cnt2);
  79. if (tmp - x * <= cnt1)
  80. {
  81. pre = prime[i];
  82. for (int j = ; j < x; j++)
  83. {
  84. ans.push_back();
  85. }
  86. for (int j = ; j < tmp - x * ; j++)
  87. {
  88. ans.push_back();
  89. }
  90. cnt2 -= x;
  91. cnt1 -= (tmp - x * );
  92. }
  93. }
  94. for(int i = ; i < ans.size(); i++)
  95. {
  96. cout << ans[i] << " ";
  97. }
  98. while(cnt1--)
  99. {
  100. cout << << " ";
  101. }
  102. while(cnt2--)
  103. {
  104. cout << << " ";
  105. }
  106. return ;
  107. }

Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)的更多相关文章

  1. Codeforces Round #556 (Div. 1)

    Codeforces Round #556 (Div. 1) A. Prefix Sum Primes 给你一堆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 Descripti ...

  3. Codeforces Round #556 (Div. 2)-ABC(这次的题前三题真心水)

    A. Stock Arbitraging 直接上代码: #include<cstdio> #include<cstring> #include<iostream> ...

  4. Codeforces Round #521 (Div. 3) E. Thematic Contests(思维)

    Codeforces Round #521 (Div. 3)  E. Thematic Contests 题目传送门 题意: 现在有n个题目,每种题目有自己的类型要举办一次考试,考试的原则是每天只有一 ...

  5. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

  6. Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳

    E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...

  7. Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)

     传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...

  8. Codeforces Round #232 (Div. 2) D. On Sum of Fractions

    D. On Sum of Fractions Let's assume that v(n) is the largest prime number, that does not exceed n; u ...

  9. Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜

    题目链接: 题目 D. Toy Sum time limit per test:1 second memory limit per test:256 megabytes 问题描述 Little Chr ...

随机推荐

  1. 洛谷 P2569[SCOI2010]股票交易(动规+单调队列)

    //只能写出裸的动规,为什么会有人能想到用单调队列优化Orz 题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测 ...

  2. [SoapUI] 在某个测试步骤下面增加Script Assertion,运用 messageExchange 获取response content

    import com.eviware.soapui.support.GroovyUtils import com.eviware.soapui.support.XmlHolder import org ...

  3. jdk1.7 环境变量配置

    Windows系统中设置环境变量如下图右击“我的电脑”,选择“属性”. 点击“高级”选项卡,选择“环境变量”.  在“系统环境变量”中设置上面提到的3个环境变量,如果变量已经存在就选择“编辑”,否则选 ...

  4. 线段树 - 3264 Balanced Lineup

    大略的的看了线段树的定义然后就开始练手, 可能很多地方可以优化, 仅供参考. 徘徊在AC的边缘, 提交代码要把cout/cin 缓存 printf/scanf 刚好能过. #include <i ...

  5. git 进阶操作

    1.blame git blame +文件名,可以查看到某个文件每一行最近一次是由谁编辑修改的.-L 22,33 选项可以制定 2.bisect 开始git bisect:   $ git bisec ...

  6. centos7用fdisk进行分区

    1.查看分区信息:fdisk -l 从上面可以看到,/dev/sdb分区还没有使用,现在将其划分成2个10G的分区. 2.执行:fdisk /dev/sdb 1):fdisk命令参数 p:打印分区表. ...

  7. git hook 自动部署

    1. 当前虚拟站点根目录的 .git/ 权限 2. 当前项目裸仓库创建 hooks/post-receive 文件,并给予x 的权限 3. 复制如下内容 #!/bin/sh unset $(git r ...

  8. CoderForces 518D Ilya and Escalator (期望DP)

    题意:给定 n 个人,在每一时刻一个人进入地铁的概率是 p,站着不动的概率是 1-p,然后问你 t 时间地铁里有多少人. 析:很明显这是一个期望DP,用d[i][j]表示 i 时刻 j 个人进入地铁的 ...

  9. Linux内核版本

    在Linux中找到/usr/src/kernels/目录,该目录下面有一个系统内核文件 例如:2.6.32-431.11.2.el6.x86_64 Linux内核版本规则 Linux内核版本有三组数字 ...

  10. STS 闪退

    # # A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ILLEGAL_INSTRUCTI ...