题目链接: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. mezzanine 历险记

    安装去github下载 mezzanine https://github.com/ganmk/mezzanine 安装出现问题了: grappelli_safe >= 0.4.5 问题好像出在这 ...

  2. 1114 记录一点点吧 RP Axure

  3. 创建自定义路由处理程序(Creating a Custom Route Handler) | 定制路由系统| 高级路由特性 |精通ASP-NET-MVC-5-弗瑞曼

    自定义实现 IRouteHandler

  4. Dynamics 365 CRM Action 和 workflow 的区别

    workflow 总是需要一个record作为起始点(create, update, delete or on-demand) 但是action 不需要. 例如我们需要action来创建一个发送ema ...

  5. HTML超全笔记

    HTML概述 概念:是最基础的网页开发语言 Hyper Text Markup Language 超文本标记语言 超文本: 超文本是用超链接的方法,将各种不同空间的文字信息组织在一起的网状文本. 标记 ...

  6. 解决AS加载gradle时出现的Could not find com.android.tools.build:gradle:3.5.0.的错误

    时间:2019/12/7 最近在做安卓大作业时总是遇到从GitHub上下载下来的demo不能在本地Android studio中运行的问题,感觉真的被安卓中的各种版本给恶心到了,下面记录其中比较典型的 ...

  7. C#系列之占位符的使用方法(二)

    今天,我将简单记录下占位符的使用方法 首先,我们来看不使用占位符的方法来代码输出 int number = 10; int number_1 = 20; int number_2 = 30; Cons ...

  8. Java爬虫框架之WebMagic

    一.介绍 WebMagic是一个简单灵活的Java爬虫框架.基于WebMagic,你可以快速开发出一个高效.易维护的爬虫. 二.如何学习 1.查看官网 官网地址为:http://webmagic.io ...

  9. 配置 Docker 加速器

    Linux curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io ...

  10. Asp.Net Core IdentityServer4 管理面板集成

    前言 IdentityServer4(以下简称 Id4) 是 Asp.Net Core 中一个非常流行的 OpenId Connect 和 OAuth 2.0 框架,可以轻松集成到 Asp.Net C ...