CodeFores 665D Simple Subset(贪心)
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("%dn",num);
for(int i=1;i<=n;i++)
{
if(flag[i])
{
if(i==n)
printf("%dn",b[i]);
else
printf("%d ",b[i]);
}
}
}
return 0;
}
- 谷歌开放的TensorFlow Object Detection API 效果如何?对业界有什么影响?
- 生信编程直播课程优秀学员作业展示1
- ID转换大全
- 走在专家的路上,每天一条SQL优化(3)
- 表现层设计模式
- MongoDB模拟多文档事务操作
- 表达矩阵可视化大全
- .NET MongoDB Driver GridFS 2.2原理及使用示例
- .NET MongoDB Driver 2.2使用示例
- 设计模式(行为模式)——观察者模式
- 机器学习实战 | 第二章:线性回归模型
- C#要点补充
- 【直播】我的基因组63:wegene芯片跟二代测序的简单比较
- .Net多线程编程—System.Threading.Tasks.Parallel
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 盘一盘 Python 特别篇 20 - SciPy 稀疏矩阵
- Spark 3.0.0-SNAPSHOT Access Kerberized HDFS
- Spark Nightly Builds
- R语言入门之基本统计量
- K8S 生态周报| Istio 已修复导致 Pod 崩溃的 bug
- Spark Kubernetes 的源码分析系列 - submit
- 如何交互可视化 Roam Research 局部笔记网络?
- Spark Kubernetes 的源码分析系列 - features
- Spark Kubernetes 的源码分析系列 - scheduler
- 简易Ramdisk 镜像制作(基于Centos7+)
- 在 K8S 部署一个 Spark History Server - 篇1
- R语言入门之非参数假设检验
- 在 K8S 部署一个 Spark History Server - 篇2
- UEFI/LEGACY 可启动ISO制作
- Kerberized HDFS with Kubernetes