Codeforces--274A--k-Multiple Free Set

题目描述:
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.
输入描述:
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.
输出描述:
On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, …, an}.
输入:
6 2
2 3 6 5 4 10
输出:
3
题意:
给你一个含有n个数的集合,现在给一个倍数k,问在这个集合任意两个元素之间不存在k倍关系 的子集中,子集的最大元素个数为多少
题解
二分去找x的k倍
代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn = 100000 + 5;
int n;
ll k;
ll a[maxn];
int vis[maxn];

int main(){
    while(scanf("%d%lld",&n,&k)!=EOF){
        for(int i = 1; i <= n; i ++) scanf("%lld",&a[i]);
        sort(a + 1,a + n + 1);
        int ans = n;
        memset(vis,0,sizeof(vis));
        for(int i = 1; i <= n; i ++){
            if(vis[i]) continue;
            int l = i + 1;
            int r = n;
            ll tmp = a[i] * k;
            while(l <= r){
                //cout<<mid<<endl;
                int mid = (l + r) / 2;
                if(a[mid] > tmp) r = mid - 1;
                else if(a[mid] == tmp){
                    ans --;
                    vis[mid] = 1;
                    break;
                }
                else l = mid + 1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值