先上题目

k-Multiple Free Set

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = x·k.

You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-multiple free subset.

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).

All the numbers in the lines are separated by single spaces.

Output

On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}.

Sample test(s)
Input
6 2
2 3 6 5 4 10
Output
3
Note

In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}.

  题意是给你一堆数,每个数只出现了一次,以及一个正整数倍数k,定义一种子集里面的x<y且x*k!=y,这种子集元素最多的时候个数有多大。

  这一题的分类暂时也不知道该分为什么,这种题好像曾经遇到过,可是当时好像也没有做出来,今天这一题感觉有点像是水过去的。

  做法是先对这些数进行排序,然后找出每一个数的k被在不在这些数里面,如果在,将那个数的位置记录下来,然后就从头开始扫描,遇到一个数,如果没有访问过,就访问它,看看的k倍,k*k倍,k*k*k倍···存不存在,并记录下最终从这个数按照访问它的这些倍数的个数,同时没访问完一个数,标记它已经被访问完,下次就不需要被访问了。

  那么为什么要这样做呢,解释如下:当一个数它不是某个数的k倍,同时它的k倍也不存在,那么在构成子集的时候,这个数就一定要选上;如果这个数是某个数的k倍,或者它的k倍在这些数里面的话,那它们的关系就可以用一条链来形容,因为每一个数至于它的k倍和它除以k的那个数有关,在这条链上面的其他数都与它没有关系,那我们只需要取这条链中相隔的数,就可以达到取最多数的目的。那么如果这条链的长度是偶数,那就去一半,如果是奇数,那就去平分后+1这么多的数目。

  这一题的数据量不大,可以开一个布尔数组标记某个值访问了没有,如果数据更加大,应该就要用set了。

  同时这一题的每一个数都只会出现一次,所以可以用这种方法贪心,如果是这个数出现不止一次的话,就不可以这样做了,那需要用dp。

上代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#define LL long long
#define MAX (100000+10)
using namespace std; typedef struct
{
LL d;
int m;
}S; S s[MAX];
int c[MAX];
bool f[MAX]; bool cmp(S x,S y){return x.d<y.d;} int finds(int l,int r,LL t)
{
while(l<=r)
{
int mid=(l+r)>>;
if(s[mid].d<t) l=mid+;
else r=mid-;
}
return l;
} void deal(int t)
{
int i;
c[t]=;
for(i=s[t].m;i!=-;i=s[i].m)
{
c[t]++;
f[i]=;
}
} void check(int n)
{
int i;
memset(c,,sizeof(c));
memset(f,,sizeof(f));
for(i=;i<n;i++)
{
if(!f[i]) deal(i);
}
} int main()
{
int i,n;
LL k,tot,M;
//freopen("data.txt","r",stdin);
scanf("%d %lld",&n,&k);
M=-;
for(i=;i<n;i++)
{
scanf("%lld",&s[i].d);
M=s[i].d>M ? s[i].d : M;
}
sort(s,s+n,cmp);
//s[n].d=M+2;
//s[n].m=-1;
for(i=;i<n;i++)
{
LL t=s[i].d*k;
s[i].m=finds(i+,n,t);
if(t!=s[s[i].m].d) s[i].m=-;
}
check(n);
tot=;
for(i=;i<n;i++)
{
if(!f[i]) tot+=((c[i]+)>>);
}
printf("%lld",tot);
return ;
}

274A

CodeForces - 274A - k-Multiple Free Set的更多相关文章

  1. Codeforces gym102152 K.Subarrays OR

    传送:http://codeforces.com/gym/102152/problem/K 题意:给定$n(n\le10^5)$个数$a_i(a_i\le10^9)$,对于任一个子数组中的数进行或操作 ...

  2. codeforces 1133E K Balanced Teams

    题目链接:http://codeforces.com/contest/1133/problem/E 题目大意: 在n个人中找到k个队伍.每个队伍必须满足最大值减最小值不超过5.求满足条件k个队伍人数的 ...

  3. Codeforces 1133E - K Balanced Teams - [DP]

    题目链接:https://codeforces.com/contest/1133/problem/C 题意: 给出 $n$ 个数,选取其中若干个数分别组成 $k$ 组,要求每组内最大值与最小值的差值不 ...

  4. codeforces 1269E K Integers (二分+树状数组)

    链接:https://codeforces.com/contest/1269/problem/E 题意:给一个序列P1,P2,P3,P4....Pi,每次可以交换两个相邻的元素,执行最小次数的交换移动 ...

  5. codeforces 1282B2. K for the Price of One (Hard Version) (dp)

    链接 https://codeforces.com/contest/1282/problem/B2 题意: 商店买东西,商店有n个物品,每个物品有自己的价格,商店有个优惠活动,当你买恰好k个东西时可以 ...

  6. Codeforces 544E K Balanced Teams (DP)

    题目: You are a coach at your local university. There are nn students under your supervision, the prog ...

  7. Codeforces Gym101502 K.Malek and Summer Semester

    K. Malek and Summer Semester   time limit per test 1.0 s memory limit per test 256 MB input standard ...

  8. Codeforces 987 K预处理BFS 3n,7n+1随机结论题/不动点逆序对 X&Y=0连边DFS求连通块数目

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  9. [二分]codeforces 274A k-Multiple Free Set

    k-Multiple Free Set time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. CentOS 安装 MRTG 软件完成后的 403 Forbidden(转载)

    用 yum 安装 MRTG 並设定好之后也把 apache 的 httpd.conf 加上 mrtg 的目录,但 http://server/mrtg 卻一直出現 403 Forbidden.在 ht ...

  2. java 格式化日期

      SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); simpleDat ...

  3. Hardwood Species(map)

    http://poj.org/problem?id=2418 题意:给定一系列字符串,要求按字典序升序输出每个串,并输出每个串出现的百分比. 用map做的,交c++A了,G++ WA..so sad. ...

  4. [Apple开发者帐户帮助]一、开始(1)关于您的开发者帐户

    Apple开发人员网站提供了为Apple平台制作出色应用所需的工具和信息.如果您不熟悉Apple平台上的开发,可以免费使用.只需接受Apple开发者协议,即可为您创建一个帐户.使用此帐户下载测试版软件 ...

  5. es6入门6--数组拓展运算符,Array.from()基本用法

    本文只是作为ES6入门第九章学习笔记,在整理知识点的同时,会加入部分个人思考与解答,若想知道更详细的介绍,还请阅读阮一峰大神的ES6入门 一.拓展运算符 ES6中新增了拓展运算(...)三个点,它的作 ...

  6. Python 42 mysql用户管理 、pymysql模块

    一:mysql用户管理 什么是mysql用户管理 mysql是一个tcp服务器,应用于操作服务器上的文件数据,接收用户端发送的指令,接收指令时需要考虑到安全问题, ATM购物车中的用户认证和mysql ...

  7. CVTE面经

    神一般的面试经历.也算面了不少公司,没见过这种面试. 一面:三个同学对应一个面试官,同一个问题依次作答. 1.为什么投递这个岗位? 答:blablabla... 2.最难忘的成功项目? 答:blabl ...

  8. python请求服务器图片并下载到本地磁盘

    >>> import os >>> os.getcwd() 'C:\\Python33' >>> os.chdir('E:\\python\\mm ...

  9. .net中的WebForm引人MVC的控制器

    当下.net中比较火的模式MVC模式,说实话对于菜鸟的我还没有遇到一个比较健全的MVC模式的项目也是比较遗憾.偶然间在网上看到WebForm实现MVC中的模式(主要是控制器...)就学习了一波,下面是 ...

  10. 有关css的选择器优先级以及父子选择器

    css,又称样式重叠表,如今的网页的样式基本是div+css写出来的,功能十分强大,要想在html文件中引入css文件需要在<head></head>标签内输入一行:<l ...