题目链接: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. js 字符串方法 和 数组方法总览

    字符串方法        search()             方法搜索特定值的字符串,并返回匹配的位置.         相比于indexOf(),search()可以设置更强大的搜索值(正则表 ...

  2. CSDN

    链接:https://blog.csdn.net/shaoyedeboke

  3. 批量解析域名为IP地址的python脚本

    脚本如下: #!/usr/bin/env python #coding:utf- import os,sys from socket import gethostbyname DOMAIN= &quo ...

  4. OpenCV3入门(四)图像的基础操作

    1.访问图像像素 1)灰度图像 2)彩色图像 OpenCV中的颜色顺序是BGR而不是RGB. 访问图像的像素在OpenCV中就是访问Mat矩阵,常用的有三种方法. at定位符访问 Mat数据结构,操作 ...

  5. solaris系统磁盘镜像

    查看磁盘分区 查看系统的磁盘数据与容量: 用format查看一下磁盘的情况,0号盘是c1t0d0,系统源磁盘,1号盘是c1t1d0,新增加磁盘,作为镜像盘使用. 注意:两块硬盘的容量最好相等,如果镜像 ...

  6. https搭建(自签名证书)

    博客搬家: https搭建(自签名证书) 上一篇博客探究了https(ssl)的原理,为了贯彻理论落实于实践的宗旨,本文将记录我搭建https的实操流程,使用Apache2+ubuntu+openss ...

  7. It is possible and safe to monitor a table DML history on sqlserver

    He is my test step: In a test enviroument, I make a table "test"/ demo table:create table ...

  8. 高精度模板(Vector实现更加方便)

    计算的数long long 甚至更大的数据类型的都存不下的时候,应该怎么办 ? 解决方法 :我们可以把一个很大的数当做字符串进行处理,这时候就需要用到高精度. 话不多说,咱们边看代码边处理 : 加法 ...

  9. MySQL中遍历查询结果的常用API(c)

    本中所使用的table: MySQL中的错误处理函数 unsigned int mysql_errno(MYSQL *mysql) const char *mysql_error(MYSQL *mys ...

  10. 日志框架 log4j2 全解析

    概述 logging翻译为日志记录 那问题是什么是日志? 日志实际上是日记的一种,用于记录某个时间点发生了什么事情,比如大学老师的教学日志,工作日志等 为什么要记录日志? 在实际生活中记录日志主要为了 ...