[二分]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的 ...
随机推荐
- [开源福利] Arithmetic Generator
Arithmetic Generator Built with ❤︎ by Simon Ma ✨ A powerful arithmetic generator
- 压力测试(八)-多节点JMeter分布式压测实战
1.Jmeter4.0分布式压测准备工作 简介:讲解Linux服务器上jmeter进行分布式压测的相关准备工作 1.压测注意事项 the firewalls on the systems are tu ...
- Metaploit-永恒之蓝漏洞利用
目录 Metaploit介绍 实验环境 漏洞利用过程 Metaploit介绍 本次测试主要是利用永恒之蓝漏洞对windows7进行控制利用,掌握Metaploit工具的使用,知道永恒之蓝的漏洞利用原理 ...
- python中if __name__ == '__main__'是什么?
__name__和__main__认识 作用:一般用于测试程序的功能,if __name__ == '__main__':下面的代码会被执行,但当前.py文件被当做模块导入的时候,main下面的代码就 ...
- 先导篇:SpringCloud介绍篇
1.什么是SpringCloud Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载 ...
- CSS 图像拼合技术(雪碧图)
1.css 图像拼合 图像拼合就是单个图像的集合. 有许多图像的网页可能需要很长的时间来加载和生成多个服务器的请求. 使用图像拼合会降低服务器的请求数量,并节省带宽. 代码如下: <!docty ...
- Python爬虫 - UserAgent列表
PC端: PC_USER_AGENT = [ 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', 'Mozilla/4.0 (compatibl ...
- idea 2018.1激活方法
之前用的idea都是2017版本的,现在已经四月份了,对于2018年1月份的版本应该可以放心的用了. 在这里,仅提供2018版本的激活码. 至于安装步骤,这里省略一千个字...... 下面是具体的激活 ...
- php Rabbitmq扩展安装坑点
pecl install amqp 出现 checking for amqp using pkg-config... configure: error: librabbitmq not found 这 ...
- vue中犯下的小错误(一)
在开发采筑平台SRM的移动项目中:一个页面,感觉没啥错误,但是页面报错如下: 页面中的data或者mothods都没有任何问题,但是这个报错很是让人纠结,后来发现,在使用子组件时候: 此tabShow ...