CodeFores 665D Simple Subset(贪心)
1 second
256 megabytes
standard input
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.
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.
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.
2 2 3
2 3 2
2 2 2
1 2
3 2 1 1
3 1 1 2
2 83 14
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]);elseprintf("%d ",b[i]);}}}return 0;
}
转载于:https://www.cnblogs.com/dacc123/p/8228675.html
CodeFores 665D Simple Subset(贪心)相关推荐
- coeforces 665D D. Simple Subset(最大团orsb题)
题目链接: D. Simple Subset time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Educational Codeforces Round 12 D. Simple Subset 最大团
D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...
- CF-1249 F.Maximum Weight Subset(贪心)
CF-1249 F.Maximum Weight Subset(贪心) 题目链接 题意 在一棵树上选一些点构成一个集合,满足集合内任意两点的距离大于kkk,求集合的最大权值和 思路 一共200个点,可 ...
- Educational Codeforces Round 12 C. Simple Strings 贪心
C. Simple Strings 题目连接: http://www.codeforces.com/contest/665/problem/C Description zscoder loves si ...
- Codefores 460C-Present(二分+贪心)
在打哈尔滨理工大学个人训练赛做到的题目. 觉得挺不错的,挺锻炼思维的. 题目大意:给定n朵花,每朵花的高度不一样,还有m天,有一种神奇的水可以使相邻的w朵花每天长高1,每天只能浇一次,问最矮的花最后最 ...
- 2016区域赛前冲刺训练
UPD 2016.10.23 shift-and (2题) Codeforces 训练 现在已经完成了: 191 [Codeforces Round #377] (6/6) Div 2 A Buy a ...
- OpenID Connect:OAuth 2.0协议之上的简单身份层
OpenID Connect是什么?OpenID Connect(目前版本是1.0)是OAuth 2.0协议(可参考本人此篇:OAuth 2.0 / RCF6749 协议解读)之上的简单身份层,用 A ...
- 【HDU - 6237】A Simple Stone Game(贪心,思维,素因子分解,数学)
题干: After he has learned how to play Nim game, Bob begins to try another stone game which seems much ...
- 论文读书笔记-Submodular meets Spectral: Greedy Algorithms for Subset Selection, Sparse Approximation and D
标题:Submodular meets Spectral: Greedy Algorithms for Subset Selection, Sparse Approximation and Dicti ...
最新文章
- Team Task:DP
- 用钉钉接收zabbix告警
- jQuery的操作css的几种方法和位置,尺寸以及scrolltop方法
- LeetCode 410. 分割数组的最大值
- [No0000123]WPF DataGrid Columns Visibility的绑定
- erlang 分布式数据库Mnesia 实现及应用
- Microsoft Sql Server 2008 R2 Express 下载地址推荐
- 接口测试及服务器性能压测,接口测试及服务器性能压测
- chap10 图像分割(数字图像处理/冈萨雷斯)
- 线性回归(线性拟合)与非线性回归(非线性拟合)原理、推导与算法实现(一)
- cahrt框架 ios_iOS-Charts介绍
- java小游戏大鱼吃小鱼入门(音乐添加+鱼头转向+背景移动+背景泡泡效果)
- linux系统有哪些
- 华为机考108题(c++)(101-108)
- 离谱:火狐浏览器重启后插件,书签,浏览记录全部没了???
- JointJS中文文档
- log4j2测试用例
- springioc注解详解
- 2000-2020年全国31省对外开放度数据
- Rust actix aiohttp_如何看待 Rust 社区关于 Actix 框架的讨论事件?