UVALive - 6442
题目链接: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的更多相关文章
- UVALive - 6442 (思维题)
题目链接:https://vjudge.net/contest/241341#problem/I 题目大意:给你一个有N个点等距的环,编号[0,N-1],然后有些点上有一个或多个硬币,通过移动这些硬币 ...
- UESTC 2016 Summer Training #6 Div.2
我好菜啊.. UVALive 6434 给出 n 个数,分成m组,每组的价值为最大值减去最小值,每组至少有1个,如果这一组只有一个数的话,价值为0 问 最小的价值是多少 dp[i][j] 表示将 前 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
随机推荐
- kafka之一:Windows上搭建Kafka运行环境
搭建环境 1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-213315 ...
- CRT Library Features
CRT Library Features The new home for Visual Studio documentation is Visual Studio 2017 Documentatio ...
- stm32之UCOS-III
一.UCOS-III 学习UCOS-III,一般会学习以下内容: 任务创建.删除.挂起.恢复等: 临界区:独占CPU,尽量少用,否则会降低效率: 时间管理:时钟节拍(基于硬件定时器).软件定时器: 互 ...
- Qt 按顺序保存多个文件
void MainWindow::on_pushButtonSnap_clicked() { ]; sprintf(image_name, "%s%d%s", "C:/i ...
- 常见的CSS和HTML面试题
1. 常用那几种浏览器测试?有哪些内核(Layout Engine)? 浏览器:IE,Chrome,FireFox,Safari,Opera. 内核:Trident,Gecko,Presto,Webk ...
- .Net下RabbitMQ发布订阅模式实践
一.概念AMQP,即Advanced Message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计.消息中间件主要用于组件之间的解耦,消息的发 ...
- php学习笔记-php中把浮点数转化为整数
在php中有时候会遇到比如 14.6%3这种操作,php是会先把14.6转化为整数再做其它的操作,那么这个转化为整数的操作是floor(14.6)还是ceil(14.6)还是round(14.6)呢? ...
- 怀旧系列(1)----FBasic
小时候,老爸斥巨资给我买了一台小霸王学习机.玩遍了所有游戏后,里面有个F-Basic语言,黑乎乎的,一点也不好玩.直到杰兄从学校带回一本BASIC语言,才知道这玩意儿还可以编辑**图案.由于没有人指导 ...
- sublime插件-OmniMarkupPreviewer浏览器打开报404解决办法
Sublime Text > Preferences > Package Settings > OmniMarkupPreviewer > Settings - Default ...
- 使用VS Code配合Remote Development插件连接远程服务器(Mac/Linux+Windows) | Using VS Code with Remote Development Connect to Remote Server (Mac/Linux+Windows)
最新版VS Code(2019年6月)出了一系列新的插件,包括Remote Development,Remote SSH等,使得用户可以使用VS Code远程连接服务器写代码,方便了协同工作.具体配置 ...