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 ...
随机推荐
- svn for vs
现在官方下载需要注册一堆的东西,为方便群众使用在这里提供一个新版的下载. http://files.cnblogs.com/wfcfan/AnkhSvn-2.5.12266.rar
- 3.2 手机中的数据库——SQLite
http://www.sqlite.org/download.html 截至我安装SQLite数据库为止的时间,最新的版本可以下载sqlite-dll-win64-x64-3200000.zip和sq ...
- thinkphp 日志记录
日志记录\ThinkPHP\Lib\Think\Core\Log.class.php 1.可以在config.php中进行设置,默认为关闭状态. 'APP_DEBUG' => true 打开\T ...
- linux shell 编程笔记
重定向和管道 输出重定向:把在终端输出的内容保存到文件上 输入重定向:通过文件的格式输入内容到终端 管道:把上一条命令的输出作为下一条命令的输入,如同管道一样,所有命令同时进行,同时处理数据,不 ...
- .net core2.0 读取appsettings.json
一.在start.up中添加注入 二.使用
- SQLServer2008 关于CASE WHEN
CASE WHEN的两种格式 1.简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END 2.Case搜索函数 CASE ...
- Application windows are expected to have a root view controller at the end of application launch
今天把Xcode升级了,模拟器 用的12.1的系统,运行时发现项目总是崩溃,采用9.3系统的测试机发现错误日志如下: Application windows are expected to have ...
- MTK刷机工具Flash_Tool部分4032错误解决办法
MTK刷机工具Flash_Tool部分4032错误解决办法 先说明一点,这个办法不是万能的,我测试解决了以下两种情况下的4032: 1.本来正常的开发板,因为一次刷机失败后就一直变4032了 2.新开 ...
- Android学习——数据存储之文件存储
将数据存储到文件中并读取数据 1.新建FilePersistenceTest项目,并修改activity_main.xml中的代码,如下:(只加入了EditText,用于输入文本内容,不管输入什么按下 ...
- Android studio 添加引用新建类库
1.新建一个工程包 2.修改AndroidManifest.xml 将AndroidManifest.xml 修改为 <manifest xmlns:android="http://s ...