[二分]codeforces 274A k-Multiple Free Set
2 seconds
256 megabytes
standard input
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.
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
In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}.
题意:给你n个数,找出在这n个数中最多有多少个x使得所选出的数中比x大的数不是x的k倍
注意:先选择所有的数,若x的k倍在原序列存在,则看x的k倍的k倍是否存在,若存在则删去x的k倍,因为这样就可以选x和x的k倍的k倍,若x的k倍的k倍不存在,则删x的k倍
排序后二分查找,k和a[i]最大是1e9,所以要注意k*k*a[i]是否小于1e18,为防止溢出可用if(k*a[i]<=(1e18)/k)
自己的测试数据:
2 1000000000
1000000000 1000000000
4 2
2 3 4 8
5 1
1 2 3 4 5
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int amn=1e5+;
const ll inf=1e18; int n,k,mid;
ll a[amn]; ll fd(ll b){
int l=,r=n;
mid=(l+r)/;
while(l<r){
if(a[mid]<b)l=mid+;
else if(a[mid]>b) r=mid-;
else break;
mid=(l+r)/;
}
return a[mid];
} int main(){
ios::sync_with_stdio();
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>a[i];
}
sort(a+,a++n);
int ans=n,pos,jg,jg1;
if(k!=)
for(int i=;i<n;i++){
if(!a[i])continue;
jg=fd(k*a[i]);
if(k*a[i]==jg){
pos=mid;
if(k*a[i]<=inf/k){
jg1=fd(k*k*a[i]);
if(k*k*a[i]==jg1){
a[pos]=;
}
else a[i]=;
}
else a[i]=;
ans--;
}
}
printf("%d\n",ans);
}
[二分]codeforces 274A k-Multiple Free Set的更多相关文章
- 模板—算法—整体二分(区间k小值)
模板—算法—整体二分(区间k小值) Code: #include <cstdio> #include <algorithm> using namespace std; #def ...
- 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)
M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...
- hihoCoder 1133 二分·二分查找之k小数(TOP K算法)
#1133 : 二分·二分查找之k小数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回里我们知道Nettle在玩<艦これ>,Nettle的镇守府有很 ...
- hiho week 37 P1 : 二分·二分查找之k小数
P1 : 二分·二分查找之k小数 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上一回里我们知道Nettle在玩&l ...
- codeforces 1269E K Integers (二分+树状数组)
链接:https://codeforces.com/contest/1269/problem/E 题意:给一个序列P1,P2,P3,P4....Pi,每次可以交换两个相邻的元素,执行最小次数的交换移动 ...
- POJ 3294 Life Forms 后缀数组+二分 求至少k个字符串中包含的最长子串
Life Forms Description You may have wondered why most extraterrestrial life forms resemble humans, ...
- POJ3294--Life Forms 后缀数组+二分答案 大于k个字符串的最长公共子串
Life Forms Time Limit: 500 ...
- Codeforces gym102152 K.Subarrays OR
传送:http://codeforces.com/gym/102152/problem/K 题意:给定$n(n\le10^5)$个数$a_i(a_i\le10^9)$,对于任一个子数组中的数进行或操作 ...
- POJ 3579 3685(二分-查找第k大的值)
POJ 3579 题意 双重二分搜索:对列数X计算∣Xi – Xj∣组成新数列的中位数 思路 对X排序后,与X_i的差大于mid(也就是某个数大于X_i + mid)的那些数的个数如果小于N / 2的 ...
随机推荐
- Python scan查找Redis集群中的key
import redis import sys from rediscluster import StrictRedisCluster #host = "172.17.155.118&quo ...
- 京东Y事业部打造一体化质量管理平台
互联网企业质量管理的困惑 作为互联网时代的互联网企业,我们的研发模式和传统模式相比,最显著的不同在于发布节奏加快了,这个加快不是快了10%,20%,50%,而是加快了几倍,甚至几十倍,上百倍.面对加快 ...
- 【快速上手】Git的使用教程
创建Git仓库 git init 查看当前仓库情况 git status 添加修改 git add (file) or git add . 查看未提交的修改 git diff 撤销提交操作 git r ...
- 设计模式-09装饰模式(Decorator Pattern)
1.模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制:使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是 ...
- ado.net DataSet
一.概念 DataSet是ADO.NET的中心概念.可以把DataSet当成内存中的数据库,DataSet是不依赖于数据库的独立数据集合.所谓独立,就是说,即使断开数据链路,或者关闭数据库,DataS ...
- 将mysql数据库集成到idea中
将mysql数据库集成到idea中
- Apple App签名机制
概览 数字签名 签名机制与验证过程 操作流程 数字签名 摘要算法 将任意长度文本通过一个算法得到一个固定长度的文本. 源文本不同,计算结果必然不同 无法从结果反推源 例如,MD5和SHA算法 非对称加 ...
- 【WPF学习】第五十六章 基于帧的动画
除基于属性的动画系统外,WPF提供了一种创建基于帧的动画的方法,这种方法只使用代码.需要做的全部工作是响应静态的CompositionTarge.Rendering事件,触发该事件是为了给每帧获取内容 ...
- VueX状态管理器 的应用
VueX状态管理器 cnpm i vuex axios -S 1 创建Vuex 仓库 import Vue from 'vue' import Vuex from 'vuex' vue.use(Vue ...
- iview中遇到table的坑(已经修改了table的数据,但是界面没有更新)
https://blog.csdn.net/bigdargon/article/details/89381466 https://blog.csdn.net/qiuyan_f/article/deta ...