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
2
2 3
output
2
3 2
input
2
2 2
output
1
2
input
3
2 1 1
output
3
1 1 2
input
2
83 14
output
2

14 83

首先对于每个数,找出来和它的和不是素数的数,并统计个数。

然后贪心的把个数最大的那个数删除,同时,和它相关的那些数的个数就少了1,

然后再选取个数最大的,直到所有数的个数为0.用了优先队列来实现。

一开始的时候要把重复的数字合并起来,要不然这个方法会超时。
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <vector>
#include <queue> using namespace std;
#define MAX 1000
typedef long long int LL;
vector<int> a[MAX+5];
int b[MAX+5];
int c[MAX+5];
int tag[MAX+5];
bool flag[MAX+5];
bool t[MAX*1000+5];
int n;
struct Node
{
int pos;
int value;
Node(){};
Node(int pos,int value){this->pos=pos;this->value=value;}
friend bool operator<(Node a,Node b){return a.value<b.value;}
};
priority_queue<Node>q;
bool isPrime(LL x)
{
if(x==1) return 0;
if(x==2) return 1;
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
return 0;
}
return 1;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{ for(int i=1;i<=n;i++)
scanf("%d",&c[i]);
memset(t,false,sizeof(t));
int cnt=0;
for(int i=1;i<=n;i++)
{
if(!t[c[i]]||c[i]==1)
{
b[++cnt]=c[i];
t[c[i]]=true;
}
}
n=cnt;
memset(tag,0,sizeof(tag));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(!isPrime(b[i]+b[j]))
{a[i].push_back(j);tag[i]++;}
}
}
for(int i=1;i<=n;i++)
q.push(Node(i,tag[i]));
memset(flag,true,sizeof(flag));
int num=n;
while(!q.empty())
{
Node term=q.top();
q.pop();
if(term.value!=tag[term.pos]) continue;
if(term.value==0)
break;
flag[term.pos]=false;num--;
for(int i=0;i<a[term.pos].size();i++)
{
if(flag[a[term.pos][i]]==false) continue;
tag[a[term.pos][i]]--;
q.push(Node(a[term.pos][i], tag[a[term.pos][i]]));
}
}
printf("%d\n",num);
for(int i=1;i<=n;i++)
{
if(flag[i])
{
if(i==n)
printf("%d\n",b[i]);
else
printf("%d ",b[i]);
}
}
}
return 0;
}

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. Daemon,Jos,定时器

    --> FileSystemWatcher--> EventWaitHandle / AutoResetEvent / ManualResetEvent--> Mutex--> ...

  2. Unity3D发布安卓报错permisson denied的解决

    没有打开SD卡的写入权限 unity提供了sd卡权限修改的参数:

  3. lantin1

    Latin1是ISO-8859-1的别名,有些环境下写作Latin-1. ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCI ...

  4. sscanf及sprintf

    在程序中,我们肯定会遇到许多处理字符串的操作,当然C++中的string类已经做了很好了,但是也不要忘了C中的sscanf和sprintf 这两个函数用法跟printf和scanf用法很相似,只不过数 ...

  5. jquery 操作input radio 单选框

    1.jquery选中单选框 2.jquery 取消单选框 3.判断是否选中 4.设置不可编辑

  6. Flask系列:数据库

    这个系列是学习<Flask Web开发:基于Python的Web应用开发实战>的部分笔记 对于用户提交的信息,包括 账号.文章 等,需要能够将这些数据保存下来 持久存储的三种方法: 文件: ...

  7. Log4j1.x初识

    初识log4j1.x 研究源码首先要对项目要有整体的认识,这一章节主要让大家对log4j1.x有一个整体的认识,并以此为切入点,认识log4j1.x的真个框架 1 整体认识 先整体上对log4j1有一 ...

  8. linux学习笔记29---命令watch

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...

  9. 在Linux系统上查看Apache服务器的错误日志

    错误日志和访问日志文件为系统管理员提供了有用的信息,比如,为 Web 服务器排障,保护系统不受各种各样的恶意活动侵犯,或者只是进行各种各样的分析以监控 HTTP 服务器.根据你 Web 服务器配置的不 ...

  10. 在一个千万级的数据库查寻中,如何提高查询效率?分别说出在数据库设计、SQL语句、java等层面的解决方案。

    在一个千万级的数据库查寻中,如何提高查询效率?分别说出在数据库设计.SQL语句.java等层面的解决方案. 解答: 1)数据库设计方面: a. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 whe ...