D. Simple Subset
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

A tuple of positive integers {x1, x2, ..., xk} is
called simple if for all pairs of positive integers (i,  j) (1  ≤ i  <  j ≤ k), xi  +  xj is
a prime.

You are given an array a with n positive
integers a1,  a2,  ...,  an (not
necessary distinct). You want to find a simple subset of the array awith the maximum size.

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and
itself.

Let's define a subset of the array a as a tuple that can be obtained from a by
removing some (possibly all) elements of it.

Input

The first line contains integer n (1 ≤ n ≤ 1000)
— the number of integers in the array a.

The second line contains n integers ai (1 ≤ ai ≤ 106)
— the elements of the array a.

Output

On the first line print integer m — the maximum possible size of simple subset of a.

On the second line print m integers bl —
the elements of the simple subset of the array a with the maximum size.

If there is more than one solution you can print any of them. You can print the elements of the subset in any order.

Examples
input
  1. 2
  2. 2 3
output
  1. 2
  2. 3 2
input
  1. 2
  2. 2 2
output
  1. 1
  2. 2
input
  1. 3
  2. 2 1 1
output
  1. 3
  2. 1 1 2
input
  1. 2
  2. 83 14
output
  1. 2
  2.  
  3. 14 83
  4.  
  5. 首先对于每个数,找出来和它的和不是素数的数,并统计个数。
  6.  
  7. 然后贪心的把个数最大的那个数删除,同时,和它相关的那些数的个数就少了1
  8.  
  9. 然后再选取个数最大的,直到所有数的个数为0.用了优先队列来实现。
  10.  
  11. 一开始的时候要把重复的数字合并起来,要不然这个方法会超时。
  12. #include <iostream>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <algorithm>
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include <vector>
  19. #include <queue>
  20. using namespace std;
  21. #define MAX 1000
  22. typedef long long int LL;
  23. vector<int> a[MAX+5];
  24. int b[MAX+5];
  25. int c[MAX+5];
  26. int tag[MAX+5];
  27. bool flag[MAX+5];
  28. bool t[MAX*1000+5];
  29. int n;
  30. struct Node
  31. {
  32.     int pos;
  33.     int value;
  34.     Node(){};
  35.     Node(int pos,int value){this->pos=pos;this->value=value;}
  36.     friend bool operator<(Node a,Node b){return a.value<b.value;}
  37. };
  38. priority_queue<Node>q;
  39. bool isPrime(LL x)
  40. {
  41.     if(x==1) return 0;
  42.     if(x==2) return 1;
  43.     for(int i=2;i*i<=x;i++)
  44.     {
  45.         if(x%i==0)
  46.             return 0;
  47.     }
  48.     return 1;
  49. }
  50. int main()
  51. {
  52.    while(scanf("%d",&n)!=EOF)
  53.    {
  54.    for(int i=1;i<=n;i++)
  55. 	   scanf("%d",&c[i]);
  56.    memset(t,false,sizeof(t));
  57.    int cnt=0;
  58.    for(int i=1;i<=n;i++)
  59.    {
  60.        if(!t[c[i]]||c[i]==1)
  61. 	   {
  62.           b[++cnt]=c[i];
  63. 		  t[c[i]]=true;
  64. 	   }
  65.    }
  66.    n=cnt;
  67.     memset(tag,0,sizeof(tag));
  68.     for(int i=1;i<=n;i++)
  69.     {
  70.         for(int j=1;j<=n;j++)
  71.         {
  72.             if(i==j) continue;
  73.             if(!isPrime(b[i]+b[j]))
  74.             {a[i].push_back(j);tag[i]++;}
  75.         }
  76.     }
  77.     for(int i=1;i<=n;i++)
  78.         q.push(Node(i,tag[i]));
  79.     memset(flag,true,sizeof(flag));
  80.     int num=n;
  81.     while(!q.empty())
  82.     {
  83.         Node term=q.top();
  84.         q.pop();
  85.         if(term.value!=tag[term.pos]) continue;
  86.         if(term.value==0)
  87.             break;
  88.         flag[term.pos]=false;num--;
  89.         for(int i=0;i<a[term.pos].size();i++)
  90.         {
  91.             if(flag[a[term.pos][i]]==false) continue;
  92.             tag[a[term.pos][i]]--;
  93.             q.push(Node(a[term.pos][i], tag[a[term.pos][i]]));
  94.         }
  95.     }
  96.     printf("%d\n",num);
  97.     for(int i=1;i<=n;i++)
  98.     {
  99.         if(flag[i])
  100.         {
  101.             if(i==n)
  102.             printf("%d\n",b[i]);
  103.             else
  104.                 printf("%d ",b[i]);
  105.         }
  106.     }
  107.    }
  108.     return 0;
  109. }
  110.  

CodeFores 665D Simple Subset(贪心)的更多相关文章

  1. Codeforces 665D Simple Subset [简单数学]

    题意: 给你n个数,让你从中选一个子集要求子集中的任何两个数相加都是质数. 思路: 一开始把自己坑了,各种想,后来发现一个简单的性质,那就是两个数相加的必要条件是这两个数之中必定一个奇数一个偶数,(除 ...

  2. codeforces 665D Simple Subset

    题目链接 给一个数列, 让你选出其中的m个数, 使得选出的数中任意两个数之和都为质数, m尽可能的大. 首先, 除了1以外的任意两个相同的数相加结果都不是质数. 然后, 不考虑1的话, 选出的数的个数 ...

  3. CodeForces - 665D Simple Subset 想法题

    //题意:给你n个数(可能有重复),问你最多可以取出多少个数使得任意两个数之和为质数.//题解:以为是个C(2,n)复杂度,结果手摸几组,发现从奇偶性考虑,只有两种情况:有1,可以取出所有的1,并可以 ...

  4. Codeforces 665D Simple Subset【构造】

    题目链接: http://codeforces.com/problemset/problem/665/D 题意: 给定序列,从中找出最大的子集,使得子集中的数两两相加均为质数. 分析: 貌似有用最大团 ...

  5. coeforces 665D D. Simple Subset(最大团orsb题)

    题目链接: D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. Educational Codeforces Round 12 D. Simple Subset 最大团

    D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...

  7. Educational Codeforces Round 12 C. Simple Strings 贪心

    C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...

  8. Codeforces Round #316 (Div. 2) B Simple Game 贪心

    贪心,如果m分成的两个区间长度不相等,那么选长的那个区间最接近m的位置,否则选m-1位置,特判一下n等于1的情况 #include<bits/stdc++.h> using namespa ...

  9. Codeforces 1249F Maximum Weight Subset (贪心)

    题意 在一颗有点权的树上,选若干个点,使得这些点两两距离大于k,且点权和最大 思路 贪心的取比较大的值即可 将所有点按照深度从大到小排序,如果当前点点权\(a[i]\)大于0,则将距离为k以内的所有点 ...

随机推荐

  1. AHM ---301重定向

    使用amh.conf 或重新创建一个test.conf配置文件 .保存目录 /usr/local/nginx/conf/rewrite 例如跳到 www.shuaixingkeji.com if ($ ...

  2. jquery.zclip实现点击拷贝文字功能

    <script type="text/javascript" src="http://www.sitejs.cn/templets/skins/js/jquery- ...

  3. mysql创建账号对应的数据库方法

    增加一个用户mydb密码为123450, 让他只可以在(localhost/%)%表示可以支持远程上登录,并可以对数据库mydata5_db进行查询.插入.修改.删除的操作. grant select ...

  4. Java中的synthetic

    有synthetic标记的field和method是class内部使用的,正常的源代码里不会出现synthetic field.小颖编译工具用的就是jad.所有反编译工具都不能保证完全正确地反编译cl ...

  5. vim语法高亮插件编写

    # vim语法高亮插件编写 编写vim语法高亮插件很简单,只需要编写两个文件.vim放到vim的安装目录下的目录就可以了. ## 输出------------------------------ sy ...

  6. 1.2.2 Loaders - 加载器

    Loaders从Android 3.0引入,它使得在activity或是fragment里进行异步数据加载变得非常简单.Loaders有如下的特性: 它在每个 Activity  和 Fragment ...

  7. 分享 stormzhang的Andoid学习之路

    硬件 电脑–推荐Mac 首先声明我不是果粉,个人Windows,Linux,Mac OX系统均用过, 只能说Windows上面的开发工具简直难以恭维,尤其命令行超级难用,而Linux自己必须得花不少时 ...

  8. PHP——smarty模板(第一天)

    smarty.class.php 主要的东西放在类里面templates 放模板templates_c 放缓存 类里面 $smarty->assign("author",&q ...

  9. 再来一个expect脚本

    [root@jenkins scripts]# cat expect_test1205.sh #!/usr/bin/expect ################################### ...

  10. vim -- 查找和替换

    %s/foo/bar/g 在所有行中寻找‘foo’,并且用‘bar’替换 :s/foo/bar/g 在当前行寻找‘foo’,并且用‘foo’替换 :%s/foo/bar/gc 将每一个‘foo',并用 ...