CF 303C——Minimum Modular——————【剪枝】
2 seconds
256 megabytes
standard input
standard output
You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the minimum modular m (m > 0), so that for every pair of the remaining integers(ai, aj), the following unequality holds:
.
The first line contains two integers n and k (1 ≤ n ≤ 5000, 0 ≤ k ≤ 4), which we have mentioned above.
The second line contains n distinct integers a1, a2, ..., an (0 ≤ ai ≤ 106).
Print a single positive integer — the minimum m.
7 0
0 2 3 6 7 12 18
13
7 1
0 2 3 6 7 12 18
7
参考:http://www.cnblogs.com/Lyush/archive/2013/05/14/3077258.html
题目大意:给你n个数,你可以从中删除最多k个数,使得剩余的所有数对m取余没有同余的,求最小的m。
解题思路:首先想到暴力,从小到大枚举m,然后判断n个数中对m取模同余个数有多少,如果超出k就枚举更大的m。然而这样的话,时间复杂度为O(n*1e6)。然后在网上找了博客看,但是有些地方当时自己感觉很不好理解的,这里做下自己的解释。1.首先这里用了一个剪枝,这个剪枝能节省大量时间。因为如果有k+1个数都是对m取模同余,那么只需删除k个数,就可以让剩下的数(只剩下一个数)不同余,那么从k+1个同余的数中取出2个数组成同余对的组合数就有C(2,k+1)种,即k*k+1/2种,那么如果对m取模同余的同余对的组合数大于k*k+1/2种,说明无法删除k个数使得剩下的数不同余。2.然后暴力判断此时满足1步骤的m作为模是否能满足同余的数小于k个。
#include<bits/stdc++.h>
using namespace std;
#define max(a,b) (a)>(b)?(a):(b)
const int maxn=1e6+100;
int num[maxn];
int a[5500];
bool flag[maxn];
int dif(int a,int b){
return a>b? a-b:b-a;
}
int main(){
int n,k,i,j,maxa,m,mark,sum,cn,mod;
maxa=-1;
while(scanf("%d%d",&n,&k)!=EOF){
memset(num,0,sizeof(num));
for(i=0;i<n;i++){
scanf("%d",&a[i]);
maxa=max(a[i],maxa);
}
//首先应知道a%m==b%m --> |a-b|%m==0
for(i=1;i<n;i++){
for(j=0;j<i;j++){
//不同类型的同余对各有多少
num[dif(a[i],a[j])]++;
}
}
for(m=1;m<=maxa;m++){
sum=0;
for(i=m;i<=maxa;i+=m){
//这里i+=m的原因是,这样能保证同余对 对于对之间也都是同余的,即这样挑出的组合中所有数都是同余的。
sum+=num[i]; //
if(sum>k*(k+1)/2){ //剪枝
break;
}
}
if(sum>k*(k+1)/2){
continue;
}
cn=0,mark=0;
for(j=0;j<n&&(!mark);j++){ //暴力判断m是否满足题目的要求
mod=a[j]%m;
if(!flag[mod]){
flag[mod]=1;
}else{
cn++;
if(cn>k){
mark=1;
}
}
}
for(j=0;j<n;j++){ //还原
flag[a[j]%m]=0;
}
if(!mark){
mark=m;
break;
}
}
printf("%d\n",mark);
}
return 0;
}
CF 303C——Minimum Modular——————【剪枝】的更多相关文章
- codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)
You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the ...
- 51nod 1217 Minimum Modular
N个不同的数a[1],a[2]...a[n],你可以从中去掉K个数,并且找到一个正整数M,使得剩下的N - K个数,Mod M的结果各不相同,求M的最小值. Input 第1行:2个数N, K,中间用 ...
- 51nod 1217 Minimum Modular(数论+暴力)
根据抽屉原理显然m>=(n-K) 于是在[n-K,max(a1..an)+1]的范围中枚举m 考虑K=0的做法... 如果a[i]≡a[j](mod m),则有m|(a[i]-a[j]),只要O ...
- cf 609E.Minimum spanning tree for each edge
最小生成树,lca(树链剖分(太难搞,不会写)) 问存在这条边的最小生成树,2种情况.1.这条边在原始最小生成树上.2.加上这条半形成一个环(加上),那么就找原来这条边2端点间的最大边就好(减去).( ...
- NOIP2018提高组金牌训练营——数论专题
地址 https://www.51nod.com/live/liveDescription.html#!liveId=23 1187 寻找分数 给出 a,b,c,d, 找一个分数p/q,使得a/b & ...
- codeforce303C-Minimum Modular-剪枝,暴力
Minimum Modular 题意:就是在一堆数字中,每一个数字对m取模不能等于这堆数字中的其他数字,同时给了K个机会可以删除一些数字.求最小的m: 思路:我一开始完全没思路,队长说的并查集什么的不 ...
- Codeforces Round #339 (Div.2)
A. Link/Cut Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- codeforces 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- [CodeForces - 614D] D - Skills
D - Skills Lesha plays the recently published new version of the legendary game hacknet. In this ver ...
随机推荐
- StringUtils常用方法介绍
要使用StringUtils类,首先需要导入:import org.apache.commons.lang.StringUtils;这个包 在maven项目中需要添加下面这个依赖: <depen ...
- Size Assertion
Size Assertion每一个响应包含的字节大小,可以设置大小等于,大于,小于,不等于给定的字节数. Apply to:应用范围,一般勾选Main samle only即可. Response S ...
- 浏览器HTTP_USER_AGENT汇总——Firefox、Chrome、IE9、IE8、IE7、IE6
结论: 浏览器 \ OS XP(IE6) XP(IE7) XP(IE8) Win7 x64(IE9) 猎豹浏览器2.0急速模式 Chrome/21 猎豹浏览器2.0兼容模式 IE6 ...
- Ubuntu16.04搭建各种开发环境的IDE: QT5 , CodeBlocks ,eclipse-cdt, PyCharm
搭建Ubuntu下C/C++以及Python的集成开发环境,采用双系统(Win7+Ubuntu)的Ubuntu16.04-LTS系统, 关于双系统的搭建可以参考下面博客(图文十分详细):https:/ ...
- HTML的相关路径与绝对路径的问题---通过网络搜索整理
问题描述: 在webroot中有个index.jsp 在index.jsp中写个表单. 现在在webroot中有个sub文件夹,sub文件夹中有个submit.jsp想得到index.jsp表单 ...
- SpringMVC中静态文件的引用
1.在WebRoot目录下创建 resources文件,里面可以放入css文件 2.在SpringMVC中的配置文件dispatcherServlet-servlet.xml中加入 <!-- 将 ...
- swift pod 第三方库异常的处理
Xcode8—Swift开发使用Cocoapods引入第三方库异常处理方法 参考: http://www.jianshu.com/p/23f13be525a0 //podfile文件如下 platf ...
- [DEBUG]-[org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:431)] 一直在创建sqlsession工厂原因
今天在做开发项目的时候出现一直在控台输出创建sqlsessionfactory'这个代码, tomcat一直在控制台上输出这个内容无法停止下来,那么到底是什么原因呢, 我们可以在输出信息上看到有个wa ...
- flask_restful
from flask_restful import (Resource, reqparse) # 参数解析对象生成 parser = reqparse.RequestParser() parser.a ...
- Android 多个界面(Activity)
1.介绍 2.相关属性 (1)启动Activity (2)Intent介绍 (3)关闭Activity 3.多个页面之间传递数据(页面1向页面2传递数据,单向传递数据) (1)相关属性 注意:data ...