题目链接:http://codeforces.com/problemset/problem/1301/B

思路:

(1)都是-1的情况

(2)只有一个除-1之外的数

(3)至少有两个除-1之外的不同的数字

对于(3),我们可以得出最大数字和最小数字_max,_min,而我们的答案m和k易得一定是在[_max,_min]中得出,

那么我们当然可以初始化m = (_max-_min)。因为数据范围大,我们可以通过二分来处理这个答案。

我们可以二分mid ∈[L=_min,R=_max],枚举k,然后把k带入原数组,覆盖-1,在这个k的情况下,tmp_m最大是多少,

并且记录tmp_m最大的时候,两个坐标,dx,dy。为什么要记录两个坐标呢,因为我们不知道怎么二分才是最优的,

而我们可以想到,在一个一维坐标上,(x-----------mid--------y),“--"表示距离,dis(max(mid-x,y-mid)) = m,我们想的是

可不可以让m变小,那么我们可以让这个距离尽可能的平分,那么m就最小了。

显然:如果tmp_m<m那么更新m = tmp_m,k = mid。

这里有一种特殊情况(  -1 -1 -1 40 35 -1 35 ),m = (_max-_min)就是答案,所以一开始对k也要初始化一下,显然k = _max or _min都可。

 #include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std; const int N = (int)2e5+,inf = (int)1e9+;
int a[N];
int m,k,l,r,n,mid; void fun(int mid){
int dx,dy,inx_x,inx_y,tmp_m = -;
for(int i = ; i < n; ++i){
if(a[i-] == - && a[i] == -) continue;
dy = a[i] == -? mid : a[i];
dx = a[i-] == -? mid : a[i-];
if(abs(dx-dy) > tmp_m){//tmp_m最大化
tmp_m = abs(dx-dy);
inx_x = i-; inx_y = i;//坐标
}
}
int v = a[inx_x] == - ? a[inx_y] : a[inx_x];
if(v >= mid) l = mid + ;// x-------mid-----------v
else r = mid - ; // v-----------mid------x if(tmp_m < m){//m最小化
m = tmp_m; k = mid;
}
} int main(){ ios::sync_with_stdio(false);
cin.tie(); cout.tie(); int T;
cin >> T;
while(T--){
cin >> n;
int _min = inf,_max = -;
for(int i = ; i < n; ++i){
cin >> a[i];
if(a[i] == -) continue;
_min = min(_min,a[i]);
_max = max(_max,a[i]);
} if(_min == inf && _max == -) cout << << " " << << endl;
else if(_max == _min) cout << << " " << _min << endl;
else{
l = _min,r = _max,mid,k = _min;
m = r-l;
while(l <= r){
mid = (l+r)>>;
fun(mid);
}
cout << m << " " << k << endl;
}
} return ;
}

Codeforces 1301B Motarack's Birthday(二分)的更多相关文章

  1. CodeForces 377B---Preparing for the Contest(二分+贪心)

    C - Preparing for the Contest Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  2. Codeforces 484B Maximum Value(高效+二分)

    题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...

  3. Codeforces 607A - Chain Reaction - [DP+二分]

    题目链接:https://codeforces.com/problemset/problem/607/A 题意: 有 $n$ 个塔排成一行,第 $i$ 个激光塔的位置为 $a_i$,伤害范围是 $b_ ...

  4. Codeforces 825D Suitable Replacement - 贪心 - 二分答案

    You are given two strings s and t consisting of small Latin letters, string s can also contain '?' c ...

  5. Codeforces 749D. Leaving Auction set+二分

    D. Leaving Auction time limit per test: 2 seconds memory limit per test:256 megabytes input:standard ...

  6. Educational Codeforces Round 60 C 思维 + 二分

    https://codeforces.com/contest/1117/problem/C 题意 在一个二维坐标轴上给你一个起点一个终点(x,y<=1e9),然后给你一串字符串代表每一秒的风向, ...

  7. Codeforces 672D Robin Hood(二分好题)

    D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. Codeforces 553D Nudist Beach(二分答案 + BFS)

    题目链接 Nudist Beach 来源  Codeforces Round #309 (Div. 1) Problem D 题目大意: 给定一篇森林(共$n$个点),你可以在$n$个点中选择若干个构 ...

  9. Codeforces 660C Hard Process【二分 Or 尺取】

    题目链接: http://codeforces.com/problemset/problem/660/C 题意: 给定0.1组成的数组,可以改变k个0使其为1,问最终可以得到的连续的1的最大长度. 分 ...

随机推荐

  1. 第一章001-003课程介绍、计算机网络概述、Internet概述

    计算机网络概述 课程安排: 第一章:概述 第二章:物理层 第三章:数据链路层 第四章:网络层 第五章:运输层 第六章:应用层 第七章:网络安全 第八章:因特网上的音频/视频服务 第九章:无线网络 第十 ...

  2. 内部类、final与垃圾回收,面试时你一说,面试官就知道

    内部类并不常用,而且使用起来有一定的定式,比如在下面的InnterDemoByTrhead.java里,我们通过内部类的形式创建线程. 1 public class InnerDemoByThread ...

  3. 使用dlib基于CNN(卷积神经网络)的人脸检测器来检测人脸

    基于机器学习CNN方法来检测人脸比之前介绍的效率要慢很多 需要先下载一个训练好的模型数据: 地址点击下载 // dlib_cnn_facedetect.cpp: 定义控制台应用程序的入口点. // # ...

  4. mac电脑下使用fcrackzip破解zip压缩文件密码

    fcrackzip简介 fcrackzip是一款专门破解zip类型压缩文件密码的工具,工具小巧方便.破解速度快,能使用字典和指定字符集破解,适用于linux.mac osx 系统 fcrackzip安 ...

  5. PKU 1185-炮兵阵地(状压DP)

    炮兵阵地 题目链接 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34827 Accepted: 13353 Descripti ...

  6. Centos 7 最小化Gitlab部署操作

    Gitlab的介绍 gitlab是一个版本控制的集群软件,集成了git.postgresql.Ruby.nginx.redis等重要应用工具: gitlab分为ce和ee版本:CE是社区版,EE是企业 ...

  7. 提醒你一下, 你真的很垃圾, 创建一个maven 的web 项目都忘记了

    1. 创建项目 2. 选择maven 项目 3  然后选择创建 web 项目的模板  (结尾是webapp 的) 4. 选择自己的maven 的配置文件 setting.xml  以及自己的maven ...

  8. spring cloud oauth2搭建认证中心与资源中心

    一 认证中心搭建 添加依赖,如果使用spring cloud的话,不管哪个服务都只需要这一个封装好的依赖即可 <dependency> <groupId>org.springf ...

  9. e.detail.value 获取input的值

    inputId(e) { this.setData({ inputId: e.detail.value }) },

  10. js笔记(5)--location的用法

    !DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"&g ...