codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)
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 ≤ 10^6).
Print a single positive integer — the minimum m.
题目大意:给你n个数,你可以删掉k个,求最小的m,使得对任意i,j(i≠j)均有a[i]、a[j]对m不同余(好像不是这么表述的意思差不多就行了)
思路:从小到大枚举答案,若能成为答案则输出。那么如何判断是否答案呢?只须把n个数都求余映射到0~m的区域中,若重复肯定要删,若要删超过k次则不能成为答案。这样做大体的思路就出来了,但是算法复杂度有点大,这么做目测要跪(其实我翻过别人的TLE代码没有剪枝哈哈哈)。所以我们须要进行剪枝,具体方法为:先算出每对a[i]、a[j]的差值,存入di[]数组中,枚举答案ans的时候,差值为ans的数的余数必然有重复,这些数要删掉,若重复的数目大于k(k+1)/2,那么剪掉,因为k次删除无法得出答案。
剪枝证明:若把abs(a[j]-a[i]) == ans的看成一条边,那么就是给n条边的最小覆盖集(每条边至少有一个点要被删除)。若最多能删k个点,最多能有多少条边呢?任意选k+1个点,最多可以连k(k+1)/2条边,恰好删除k个点。注意到k很小,所以此剪枝效果应该不差。
#include <cstdio>
#include <algorithm> #define MAXN 1000010 int a[MAXN], di[MAXN], vis[MAXN];
int n, k; int check(int m){
int cnt = ;
for(int i = ; i < n; ++i){
if(vis[a[i]%m] == m && ++cnt > k) return false;
vis[a[i]%m] = m;
}
return true;
} int main(){
scanf("%d%d",&n,&k);
for(int i = ; i < n; ++i) scanf("%d",&a[i]);
std::sort(a, a+n);
int m = a[n-] + ;
for(int i = ; i < n; ++i)
for(int j = i+; j < n; ++j)
++di[a[j]-a[i]];
int ans;
for(ans = ; ans <= m; ++ans){
int cnt = ;
for(int j = ans; j < m; j += ans) cnt += di[j];
if(cnt <= k*(k+)/ && check(ans)) break;
}
printf("%d\n", ans);
return ;
}
codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)的更多相关文章
- CF 303C——Minimum Modular——————【剪枝】
Minimum Modular time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心
D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 665C Simple Strings【暴力,贪心】
题目链接: http://codeforces.com/contest/665/problem/C 题意: 改变最少的字符,使得最终序列无相同的连续的字符. 分析: 对每一个与前一个字符相同的字符,枚 ...
- CodeForces 731F Video Cards (数论+暴力)
题意:给定 n 个数,可以对所有的数进行缩小,问你找出和最大的数,使得这些数都能整除这些数中最小的那个数. 析:用前缀和来做,先统计前 i 个数中有有多少数,然后再进行暴力去找最大值,每次都遍历这一段 ...
- 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...
- poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】
题目: http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...
- codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)
题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...
- [CodeForces - 1225C]p-binary 【数论】【二进制】
[CodeForces - 1225C]p-binary [数论][二进制] 标签: 题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 5 ...
- Codeforces B. Minimum Possible LCM(贪心数论)
题目描述: B. Minimum Possible LCM time limit per test 4 seconds memory limit per test 1024 megabytes inp ...
随机推荐
- Nodejs中获取参数以及处理参数
先看题干效果 在这里我们建了一个表单 填入表单需要提交的信息 对两个参数进行获取和一个加法计算 表单html代码 <form action='http://localhost:8080' met ...
- 『C++』Temp_2018_12_26
#include <iostream> #include <string> #include <array> using namespace std; class ...
- CF1066D Boxes Packing(二分答案)
题意描述: 你有$n$个物品,每个物品大小为$a_i$,$m$个盒子,每个盒子的容积为$k$.$Maksim$先生想把物品装入盒子中.对于每个物品,如果能被放入当前的盒子中,则放入当前盒子,否则换一个 ...
- c和c++单链表
c++版 #include<iostream> #include<malloc.h> using namespace std; struct node{ int data; n ...
- WARNING: IPv4 forwarding is disabled. Networking will not work.
1:Test environment [root@docker-node1 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Cor ...
- ComboBox可搜索下拉框的使用注意事项,简单记录以及我遇到的一些奇怪的bug
前几天做一个react的项目的时候需要用一个可搜索的下拉框ComboBox,上代码: <ComboBox // className={comboxClassName} items={storeA ...
- python--模块之sys与python解释器交互模块
作用:sys模块是与python解释器交互的一个接口.它提供了一系列有关python运行环境的变量和函数. 常用函数:import sys sys.argv #命令行参数list,第一个元素是程序本身 ...
- Python 爬虫 招聘信息并存入数据库
新学习了selenium,啪一下腾讯招聘 from lxml import etree from selenium import webdriver import pymysql def Geturl ...
- 插入排序,C语言实现
插入排序是稳定排序,时间复杂度最低为O(n),最高为O(n^2),平均为O(n^2). 插入排序是将数组分为两部分,一部分已经排好序,另一部分未排好序,每次从未排好序的部分取第一个元素插入到已经排好序 ...
- 简单复习一下ArrayList的扩容原理
刚刚跟几个好朋友喝完小酒回家,简单大概复习一下ArrayList的扩容原理,由于头有点小晕,就只大概说一下扩容的原理哈: 首先ArrayList实现了List接口,继承了AbstractList,大家 ...