CodeForces - 274A - k-Multiple Free Set
先上题目
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}.
题意是给你一堆数,每个数只出现了一次,以及一个正整数倍数k,定义一种子集里面的x<y且x*k!=y,这种子集元素最多的时候个数有多大。
这一题的分类暂时也不知道该分为什么,这种题好像曾经遇到过,可是当时好像也没有做出来,今天这一题感觉有点像是水过去的。
做法是先对这些数进行排序,然后找出每一个数的k被在不在这些数里面,如果在,将那个数的位置记录下来,然后就从头开始扫描,遇到一个数,如果没有访问过,就访问它,看看的k倍,k*k倍,k*k*k倍···存不存在,并记录下最终从这个数按照访问它的这些倍数的个数,同时没访问完一个数,标记它已经被访问完,下次就不需要被访问了。
那么为什么要这样做呢,解释如下:当一个数它不是某个数的k倍,同时它的k倍也不存在,那么在构成子集的时候,这个数就一定要选上;如果这个数是某个数的k倍,或者它的k倍在这些数里面的话,那它们的关系就可以用一条链来形容,因为每一个数至于它的k倍和它除以k的那个数有关,在这条链上面的其他数都与它没有关系,那我们只需要取这条链中相隔的数,就可以达到取最多数的目的。那么如果这条链的长度是偶数,那就去一半,如果是奇数,那就去平分后+1这么多的数目。
这一题的数据量不大,可以开一个布尔数组标记某个值访问了没有,如果数据更加大,应该就要用set了。
同时这一题的每一个数都只会出现一次,所以可以用这种方法贪心,如果是这个数出现不止一次的话,就不可以这样做了,那需要用dp。
上代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define LL long long
#define MAX (100000+10)
using namespace std; typedef struct
{
LL d;
int m;
}S; S s[MAX];
int c[MAX];
bool f[MAX]; bool cmp(S x,S y){return x.d<y.d;} int finds(int l,int r,LL t)
{
while(l<=r)
{
int mid=(l+r)>>;
if(s[mid].d<t) l=mid+;
else r=mid-;
}
return l;
} void deal(int t)
{
int i;
c[t]=;
for(i=s[t].m;i!=-;i=s[i].m)
{
c[t]++;
f[i]=;
}
} void check(int n)
{
int i;
memset(c,,sizeof(c));
memset(f,,sizeof(f));
for(i=;i<n;i++)
{
if(!f[i]) deal(i);
}
} int main()
{
int i,n;
LL k,tot,M;
//freopen("data.txt","r",stdin);
scanf("%d %lld",&n,&k);
M=-;
for(i=;i<n;i++)
{
scanf("%lld",&s[i].d);
M=s[i].d>M ? s[i].d : M;
}
sort(s,s+n,cmp);
//s[n].d=M+2;
//s[n].m=-1;
for(i=;i<n;i++)
{
LL t=s[i].d*k;
s[i].m=finds(i+,n,t);
if(t!=s[s[i].m].d) s[i].m=-;
}
check(n);
tot=;
for(i=;i<n;i++)
{
if(!f[i]) tot+=((c[i]+)>>);
}
printf("%lld",tot);
return ;
}
274A
CodeForces - 274A - k-Multiple Free Set的更多相关文章
- Codeforces gym102152 K.Subarrays OR
传送:http://codeforces.com/gym/102152/problem/K 题意:给定$n(n\le10^5)$个数$a_i(a_i\le10^9)$,对于任一个子数组中的数进行或操作 ...
- codeforces 1133E K Balanced Teams
题目链接:http://codeforces.com/contest/1133/problem/E 题目大意: 在n个人中找到k个队伍.每个队伍必须满足最大值减最小值不超过5.求满足条件k个队伍人数的 ...
- Codeforces 1133E - K Balanced Teams - [DP]
题目链接:https://codeforces.com/contest/1133/problem/C 题意: 给出 $n$ 个数,选取其中若干个数分别组成 $k$ 组,要求每组内最大值与最小值的差值不 ...
- codeforces 1269E K Integers (二分+树状数组)
链接:https://codeforces.com/contest/1269/problem/E 题意:给一个序列P1,P2,P3,P4....Pi,每次可以交换两个相邻的元素,执行最小次数的交换移动 ...
- codeforces 1282B2. K for the Price of One (Hard Version) (dp)
链接 https://codeforces.com/contest/1282/problem/B2 题意: 商店买东西,商店有n个物品,每个物品有自己的价格,商店有个优惠活动,当你买恰好k个东西时可以 ...
- Codeforces 544E K Balanced Teams (DP)
题目: You are a coach at your local university. There are nn students under your supervision, the prog ...
- Codeforces Gym101502 K.Malek and Summer Semester
K. Malek and Summer Semester time limit per test 1.0 s memory limit per test 256 MB input standard ...
- Codeforces 987 K预处理BFS 3n,7n+1随机结论题/不动点逆序对 X&Y=0连边DFS求连通块数目
A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...
- [二分]codeforces 274A k-Multiple Free Set
k-Multiple Free Set time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- spring拦截器和注解处理日志操作
整体思想:通过拦截器拦截所有的请求,处理含有自定义注解的方法,通过request得到需要的参数. 拦截器代码: package com.zktx.platform.log2; import java. ...
- C#可定制的数据库备份和恢复程序 (讲解流程)
可定制的数据库备份和恢复程序 tashanzhishi [原作] 关键字 数据库 备份 恢复 出处 在我们做数据库系统的程序时,经常需要为客户做一个数据库的备份和恢复程序,特别是对于一些非专业的数据库 ...
- B1800 [Ahoi2009]fly 飞行棋 数学模拟
20分钟一遍AC,大水题,我的算法比较复杂,但是好理解,就是找可以凑出来一半周长的点来暴力枚举就行了. 题干: Description 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依 ...
- 97. ExtJS之EditorGridPanel afteredit属性
转自:https://zccst.iteye.com/blog/1328869 1. 之前大多用Ext.grid.GridPanel,现在需要可编辑功能,发现比以前稍复杂一些. 就是需要对指定列进行可 ...
- 第11课 Git GUI程序的基本功能
11-1 Git GUI程序的基本操作
- Candies(差分约束系统)
http://poj.org/problem?id=3159 思路:用O(V+ElogV)的Dijkstra算法求1到n的最短路.即用优先队列优化Dijkstra算法. #include <st ...
- 0420-mysql关键词/错误提示关键词
操作关键词: 1.show //查看.展示 2.use //选择(库/表) 3.database/s //库/所有库 4.table/s //表/所有表 5.primary key //主键:不可重复 ...
- jorgchart,帮助你生成组织结构图的
下载地址: http://yunpan.cn/c6pfenkmmFV2q 访问密码 8e29 演示链接: http://www.gbtags.com/gb/share/546.htm jstree. ...
- Java常见错误整理(一)
1.配置完Java环境变量之后,仍然不能使用java命令. 解决方法: 如果是Windows10系统出现此问题,是因为个别Windows10系统不识别“JAVA_HOME”环境变量,将path中所有的 ...
- STL之map篇
度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每一个字符串,也都是这个人的名字.例如, ...