codevs 1553 互斥的数
有这样的一个集合,集合中的元素个数由给定的N决定,集合的元素为N个不同的正整数,一旦集合中的两个数x,y满足y = P*x,那么就认为x,y这两个数是互斥的,现在想知道给定的一个集合的最大子集满足两两之间不互斥。
输入有多组数据,每组第一行给定两个数N和P(1<=N<=10^5, 1<=P<=10^9)。接下来一行包含N个不同正整数ai(1<=ai<=10^9)。
输出一行表示最大的满足要求的子集的元素个数。
4 2
1 2 3 4
3
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
int s[];
map<int,bool>d;
int main()
{
int h,n,p;
cin>>n>>p;
h=;
for(int i=;i<=n;++i)
cin>>s[i];
sort(s+,s++n);
for(int i=;i<=n;++i)
{
if(!d[s[i]])
{
d[s[i]*p]=;
h++;
}
}
cout<<h;
}
哈希 做法 21ms 876KB 1213B
#include <algorithm>
#include <cstdio> using namespace std; struct node
{
int to,next,cs;
bool vis;
}edge[];
int head[],n,p,a[],tot;
int qr()
{
int x=,f=;
char ch=getchar();
while(ch>''||ch<''){if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+(int)ch-;ch=getchar();}
return x*f;
}
int get_hash1(int k)
{
return k*%;
}
int get_hash2(int k)
{
return k*%;
}
void add(int from,int to)
{
tot++;
edge[tot].next=head[from];
edge[tot].to=to;
head[from]=tot;
}
void change(int u,int v)
{
for(int j=head[u];j;j=edge[j].next)
{
if(edge[j].to==v)
{
edge[j].vis=;
}
}
}
int pd(int u,int v)
{
for(int k=head[u];k;k=edge[k].next)
{
if(edge[k].to==v&&edge[k].vis==)
return true;
}
return false;
}
int main()
{
n=qr();
p=qr();
for(int i=;i<n;++i)
{
a[i]=qr();
int x=get_hash1(a[i]);
int y=get_hash2(a[i]);
add(x,y);
}
sort(a,a+n);
int h=,ans=n;
for(int i=;i<n;++i)
{
int x=get_hash1(a[i]),y=get_hash2(a[i]);
if(pd(x,y))
{
int t=a[i]*p;
int x=get_hash1(t),y=get_hash2(t);
change(x,y);
h++;
}
}
printf("%d\n",h);
return ;
}
codevs 1553 互斥的数的更多相关文章
- codevs——1553 互斥的数
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定,集合的元素为N个不同 ...
- Codevs No.1553 互斥的数
2016-05-31 21:34:15 题目链接: 互斥的数 (Codevs No.1553) 题目大意: 给N个数,如果其中两个数满足一个数是另一个的P倍,则称它俩互斥,求一个不互斥集合的最大容量 ...
- 【wikioi】1553 互斥的数(hash+set)
http://wikioi.com/problem/1553/ 一开始我也知道用set来判a[i]/p是否在集合中,在的话就直接删掉. 但是我没有想到要排序,也没有想到当存在a,b使得a/p==b时到 ...
- codevs1553 互斥的数
1553 互斥的数
- 互斥的数(hash)
1553 互斥的数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定, ...
- 互斥的数(codevs 1553)
题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定,集合的元素为N个不同的正整数,一旦集合中的两个数x,y满足y = P*x,那么就认为x,y这两个数是互斥的,现在想知 ...
- codevs 1166 矩阵取数游戏
二次联通门 : codevs 1166 矩阵取数游戏 /* codevs 1166 矩阵取数游戏 SB区间dp dp[l][r] = max (dp[l + 1][r] + number[l], dp ...
- T1553 互斥的数 codevs
http://codevs.cn/problem/1553/ 题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定,集合的元素为N个不同的正整数,一旦集合中的两个数x,y ...
- 【CodeVS 1037】取数游戏
http://codevs.cn/problem/1037/ Alice必胜是Alice将硬币移向边权为0的一端并且把经过的边变为0,让BoB无路可走. 这样只要起点到两个方向最近的0边权的端点的边数 ...
随机推荐
- sort()的升降序函数操作
sort()函数包含在头文件#include <algorithm>中,是常用的排序函数. functional提供了一堆基于模板的比较函数对象.它们是:equal_to<Type& ...
- vim opencv
http://blog.csdn.net/fdl19881/article/details/7275203 ctags .vim: http://www.vim.org/scripts/script. ...
- POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- MacBook外置显卡eGPU折腾笔记
MacBook外置显卡eGPU折腾笔记 硬件选购 当今市场上个人电脑的独立显卡,基本上能选的只有NVIDIA和AMD了,如果你想买外置显卡来打游戏的话,NVIDIA和AMD的都可以,但如果是像我一样准 ...
- 原生JS操作 table object HTMLTableSectionElement 对象,获取行数
<tbody id="infoTab"> <tr class="fomat"> <td class="blank&quo ...
- mysql使用小结
一.修改 mysql 的 root 密码 mysql> use mysql; mysql> update user set password=password('123456') whe ...
- HDOJ1874最短路【spfa】
//不知道切过这道题目几次了,这次又wa了...双向路啊....这波简直无奈了,今晚又是浪成狗!!!! #include<cstdio> #include<vector> #i ...
- String.Format 大全
0.0的格式化 string.Format("{0:8D8}", 3)//第一个8表示空8个位置,后一个8表示用0填写最多8位数据 1.格式化货币(跟系统的环境有关,中文系统默认格 ...
- 2012 Noip提高组 Day2
1265. [NOIP2012] 同余方程 ★☆ 输入文件:mod.in 输出文件:mod.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述] 求关于 x 的同余方 ...
- CentOS7-MySQL8安装-使用yum库安装
# Enable to use MySQL 5.5 [mysql55-community] name=MySQL 5.5 Community Server baseurl/$basearch/ ena ...