[二分]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的 ...
随机推荐
- Git常用的操作指令
修改最后一次提交 有时候我们提交完了才发现漏掉了几个文件没有加,或者提交信息写错了.想要撤消刚才的提交操作,可以使用--amend 选项重新提交: 1 $ git commit --amend -m& ...
- android使用giflib加载gif
转载请标明出处:https:////www.cnblogs.com/tangZH/p/12356915.html 背景不多说,反正ndk加载gif比java上加载gif好很多很多,主要体现在内存占用与 ...
- Haproxy 使用block 阻止域名访问到某个子目录报403
配置教程如下: acl is_https_com hdr_beg(host) www.baidu.com #定义规则域名 acl api_block_url_web url_dir -i /web/ ...
- Golang/Python/PHP带你彻底学会gRPC
目录 一.gRPC是什么? 二.Protocol Buffers是什么? 三.需求:开发健身房服务 四.最佳实践 Golang 1. 安装protoc工具 2. 安装protoc-gen-go 3. ...
- HTTP协议 有这篇文章足够了
HTTP 协议详解 HTTP(HyperText Transfer Protocol)超文本传输协议.其最初的设计目的是为了提供一种发布和接收HTML页面的方法. HTTP是一个客户端(用户)和服务端 ...
- D3.js实现拓扑图
最近写项目需要画出应用程序调用链的网路拓扑图,完全自己写需要花费些时间,那么首先想到的是echarts,但echarts的自定义写法写起来非常麻烦,而且它的文档都是基于配置说明的,对于自定义开发不太方 ...
- 前端面试题(HTML、CSS部分)
HTML.CSS部分: 一.html5有哪些新特性.移除了那些元素?如何处理HTML5新标签的浏览器兼容问题?如何区分 HTML 和 HTML5? 新特性: HTML5 现在已经不是 SGML 的 ...
- python正则表达式之re模块使用
python第一个正则表达式 https://www.imooc.com/learn/550 r'imooc' Pattern Match result In [2]: import re In [ ...
- RNN学习笔记(一):长短时记忆网络(LSTM)
一.前言 在图像处理领域,卷积神经网络(Convolution Nerual Network,CNN)凭借其强大的性能取得了广泛的应用.作为一种前馈网络,CNN中各输入之间是相互独立的,每层神经元的信 ...
- An incompatible version [1.1.33] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
Springboot项目启动出现如下错误信息 解决办法在此地址:http://archive.apache.org/dist/tomcat/tomcat-connectors/native/1.2.1 ...