CodeForces 275C 贪心

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 Input

Input

6 2

2 3 6 5 4 10

Output

3

Hint

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

6}.

题意:给定集合, 就出不存在y = k*x的子集。

贪心:从小到大选过去,如果选到,则他的k *x 不能选,用set标记下来

代码:

#include <stdio.h>
#include <string.h>
#include <set>
#include <algorithm>
using namespace std;

int n, k, i, j;
long long num[100005], ans;
set<long long> save;
int main() {
    ans = 0;
    scanf("%d%d", &n, &k);
    for (i = 0; i < n; i ++) {
        scanf("%lld", &num[i]);
    }
    sort(num, num + n);
    for (i = 0; i < n; i ++) {
        if (save.count(num[i]) == 0) {
            ans ++;
            save.insert(num[i] * k);
        }
    }
    printf("%lld\n", ans);
    return 0;
}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索set
, 贪心法 最小圈基问题
, 贪心
, where
is
codeforces 贪心、codeforces 766c、codeforces758c、codeforces 94c、codeforces 762c,以便于您获取更多的相关知识。

时间: 2024-08-04 11:53:15

CodeForces 275C 贪心的相关文章

codeforces 337C Quiz

点击打开codeforces 思路: 贪心+快速幂 分析: 1 题目要求的是找到这个人的最小的分数 2 题目的数据非常大,很明显是有一个O(1)的算法也就是公式 3 我们考虑n个数分成连续的k个树有几段,设x = n/k , 那么我们就可以知道x段连续的区间里面如果每一段都让它错一个那么我们至少可以答对x*(k-1),现在我们去判断x*(k-1) >= m, 如果是那么答案就是m,否则就去判断剩下的问题就是t = n-x*k 和 剩下的答对问题的个数为z = m-x*(k-1),如果t >=

Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)

Valera has array a, consisting of n integers a0,a1,...,an-1, and function f(x), taking an integer from 0 to 2n-1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number xconta

CodeForces 287B 二分贪心

Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water

codeforces B. Pasha and String(贪心)

题意:给定一个长度为len的字符序列,然后是n个整数,对于每一个整数ai, 将字符序列区间为[ai,len-ai+1]进行反转.求出经过n次反转之后的序列! /* 思路1:将区间为偶数次的直接去掉!对剩下的区间进行反转.超时了,智商上的压制... */ #include<iostream> #include<cstdio> #include<algorithm> #include<stack> #include<cstring> #include

codeforces 158B

http://codeforces.com/problemset/problem/158/B 贪心:4人1车,(3+1)人1车,(2+2)人1车,(2+1+1)人1车,(1*4)人1车 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; int a[5]; int main() { // freopen("1.t

c++-贪心法 区间完全覆盖问题

问题描述 贪心法 区间完全覆盖问题 50C 给定一个长度为m的区间~再给出n条用闭区间表示的线段~用贪心法求出最少的线段能覆盖完m区间~例: 区间长度8,可选的覆盖线段[26][14][36][37][68][24][35] 现需要代码 c++的 过程 : 1将每一个区间按照左端点递增顺序排列,拍完序后为[14],[24],[26],[35],[36],[37],[68] 2设置一个变量表示已经覆盖到的区域.再剩下的线段中找出所有左端点小于等于当前已经覆盖到的区域的右端点的线段中,右端点最大的线

Codeforces Round #157 (Div. 1) C. Little Elephant and LCM (数学、dp)

C. Little Elephant and LCM time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output The Little Elephant loves the LCM (least common multiple) operation of a non-empty set of positive integers. The

SEO莫贪心 否则就是自寻死路

深圳的天气依旧阳光明媚,难得周末,以为可以好好休息,却被一个电话打乱了我的美好周末计划.话说前段时间一直保持良好排名的凤凰古城住宿站点,在昨晚又被度娘给封杀了,老板打来电话:小聪阿,网站昨天晚上被降权了,你给看看还有救吗?要说这个站点被K掉也是我意料之中. 当凤凰古城住宿排名稳定后,老板的心开始不满足,不满足现状,因为凤凰古城两大用户主需求就是旅游和住宿,然后默默的把旅游信息开始放进网站,当初改的时候我就说过,这样不合适.排名好的原因不光是因为做了多少外链和更新多少内容.而在于精,精在内容专业,

一个人的SEO:耐心、专心、不贪心

一个人的站,是一个人的CEO,也是一个人的SEO! 当你决定一个人,做一个站,并且想做一个好站时,你就会发现,传说中的搜索引擎优化SEO就严酷地摆在你的面前,而你毫无经验,因而手足无措,无所适从!因为你的站,将要面对千军万马的厮杀,而现在的你,却如此羸弱!更重要的是,搜索引擎是一台冷酷无情的机器,它只是按着固有的规律办事,你影响不了它,左右不了它!你在它面前,无能为力,就如在上帝面前,唯有听从,唯有等候,唯有期盼! 一个人的SEO,是所有草根站长的宿命! 而宿命意味着,你必须为之而抗争和奋斗,哪