题目链接:https://vjudge.net/contest/241341#problem/I

题目大意:输入t,t组样例,输入n,m,有n个圆槽,m个硬币,接下来m行代表每个硬币所在的位子,要求你移动硬币使得相邻的硬币距离相等,输出最小的最大移动步数(这里所指的是

一个硬币最大移动的步数)

个人思路:一直在找规律,后来也没有找到,应该是没有规律吧,搜题解也没有搜到有什么规律,全都是用二分来求的。。。这里怎么用二分呢?

因为数据范围是n<=1e6,并且如果通过单点最大k步移动可以是这些点两两间隔n/m,那么对于任意的k’(k’>k)也一定是可以的。所以可以二分查找需要的最小单点最大移动步数。当单点移动最大步数确定为d时如何判断d是否能使得这些点均匀分布呢?
考虑每个点的移动区间,假设第i-1个点的移动区间是[prelow,prehigh],因为第i点最多可移动d步,所以第i个点的移动区间是[data[i]-d,data[i]+d],又因为第i-1个点和第i个点需要间隔step=n/m,所以第i-1个点给第i个点的约束移动区间是[prelow+step,prehigh+step],所以第i个点的实际允许移动区间是

curlow=max(prelow+step,data[i]-d), curhigh=min(prehigh+step,data[i]+d);

只要对每个点判断这个区间是否存在即curlow<=curhigh是否成立,就能判断d是否合法了。

看代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=2e4+;
const int maxk=+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
int a[maxn];
int n,m,step;
bool solve(int mid)
{
int prelow,prehigh,curlow,curhigh;
prelow=a[]-mid;
prehigh=a[]+mid;
for(int i=;i<m;i++)
{
curlow=max(prelow+step,a[i]-mid);
curhigh=min(prehigh+step,a[i]+mid);
if(curlow>curhigh)
return false;
prelow=curlow;
prehigh=curhigh;
}
return true;
}
int main()
{
int t,sum=;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=;i<m;i++)
{
cin>>a[i];
}
sort(a,a+m);
int l=,r=n;
step=n/m;
while(l<r)
{
int mid=(l+r)/;
if(solve(mid))
{
r=mid;
}
else
l=mid+;
}
printf("Case #%d: %d\n",sum++,r);
// cout<<r<<endl;
}
}

UVALive - 6442的更多相关文章

  1. UVALive - 6442 (思维题)

    题目链接:https://vjudge.net/contest/241341#problem/I 题目大意:给你一个有N个点等距的环,编号[0,N-1],然后有些点上有一个或多个硬币,通过移动这些硬币 ...

  2. UESTC 2016 Summer Training #6 Div.2

    我好菜啊.. UVALive 6434 给出 n 个数,分成m组,每组的价值为最大值减去最小值,每组至少有1个,如果这一组只有一个数的话,价值为0 问 最小的价值是多少 dp[i][j] 表示将 前 ...

  3. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  4. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  5. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  6. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  7. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  8. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  9. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

随机推荐

  1. ML 徒手系列 SVM

    在Lagrange乘子法中,介绍了如何构建及如何使用对偶函数,对目标问题进行求解. 这一章利用L乘子法对SVM进行推导. SVM 即支持向量机,是通过求解超平面进行分类的一种算法.所谓的支持向量,是在 ...

  2. IIS7.0(虚拟机)发布MVC5程序出现Http403错误的解决方法.

    近来,用MVC5开发自己的一个小网站.网上租用了一个小空间(虚拟主机),可选.net版本为2.0 3.0 3.5 4.0 ,上传网站 后发现是403错误.不能访问. 经与技术人员联系,把虚拟机更换到高 ...

  3. CentOS 7 配置 mariadb

    一.安装mariadb :  yum groupinstall  mariadb  mariadb-client -y 二.启动(设置开机启动)服务 : systemctl  start (enabl ...

  4. 1、Question: prep_reads.info vs. align_summary.txt

    ###参考:https://www.biostars.org/p/163356/ used TopHat to map my reads against their relative referenc ...

  5. sublime text 侧边栏样式修改

    安装PackageResourceViewer 插件.快捷键 CTRL+SHIFT+P 打开 命令面板,输入 Package Control:Install Package (直接输入PCIP,四个单 ...

  6. 利用StoryBoard编写UITabelViewCell

    举一个炒鸡简单的例子: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPa ...

  7. C#类的成员(字段、属性、方法)

    前面定义的Person的类,里面的成员包括:字段.属性.方法.事件等,此外,前面说的嵌套类也是类的成员. a.类的成员为分:静态成员(static)和非静态成员 b.静态成员用static标识,不标识 ...

  8. Struts2 源码分析-----拦截器源码解析 --- ParametersInterceptor

    ParametersInterceptor拦截器其主要功能是把ActionContext中的请求参数设置到ValueStack中,如果栈顶是当前Action则把请求参数设置到了Action中,如果栈顶 ...

  9. sourcetree 不停的让输入密码,报 password required

    sourcetree 不停的让输入密码,报 password required sourcetree 不停的让输入密码,报 password required1.在终端(terminal)打开你的工程 ...

  10. jQuery之ajax() 参数