先上题目

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. oc50--@class1

    // // main.m #import <Foundation/Foundation.h> #import "Person.h" int main(int argc, ...

  2. Android热更新实现原理

    最近Android社区的氛围很不错嘛,连续放出一系列的android动态加载插件和热更新库,这篇文章就来介绍一下Android中实现热更新的原理. ClassLoader 我们知道Java在运行时加载 ...

  3. Python 36 GIL全局解释器锁 、vs自定义互斥锁

    一:GIL全局解释器锁介绍 在CPython中,全局解释器锁(或GIL)是一个互斥锁, 它阻止多个本机线程同时执行Python字节码.译文:之所以需要这个锁, 主要是因为CPython的内存管理不是线 ...

  4. MessagePack 新型序列化反序列化方案

    进入在学习redis的时候,在文中看到了关于MessagePack的简介,发现非常有意思,于是就花了点时间大致了解了下. MessagePack介绍: MessagePack is an effici ...

  5. Redis 链表结构 和 常用命令

    Redis 数据结构 --链表(linked-list) 命令 说明 备注 lpush key node1 [node2 ...] 把节点 node1 加入到 链表最左边 如果是 node1.node ...

  6. 关于改变安卓Button样式,这里有一个好方法。

    首先,在drawable下创建一个新的xml文件(例如我创建的为button.xml).然后在里面输入以下代码. <item> <shape> <gradient and ...

  7. OpenVX

    OpenVX openvx  1. 编译 尝试编译openvx_sample,下载相关代码. 下载的sample code直接使用make可以生成libopenvx.so. 使用python Buil ...

  8. 关于php初学者的理解!请大家浏览并指出不足!谢谢!

    昨天开始学习php,由于之前是学习.NET的,刚接触php,就关于语法就是各种不适应,什么js,jq在脑子里一团浆糊..过了一天感觉好了点,现在有点想法,大家欢迎交流批评! 今天用php做了个登录,判 ...

  9. C#遍历/反射 属性/字段

    public static string SortParam<T>(T t) { string tStr = string.Empty; if (t == null) { return s ...

  10. (转)Bootstrap 之 Metronic 模板的学习之路 - (4)源码分析之脚本部分

    https://segmentfault.com/a/1190000006709967 上篇我们将 body 标签主体部分进行了简单总览,下面看看最后的脚本部门. 页面结尾部分(Javascripts ...