博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二分搜索 2015百度之星初赛1 HDOJ 5248 序列变换
阅读量:6974 次
发布时间:2019-06-27

本文共 1230 字,大约阅读时间需要 4 分钟。

 

1 /* 2     二分搜索:在0~1e6的范围找到最小的max (ai - bi),也就是使得p + 1 <= a[i] + c or a[i] - c 3     比赛时以为是贪心,榨干智商也想不出来:( 4 */ 5 #include 
6 #include
7 #include
8 #include
9 #include
10 using namespace std;11 12 const int MAXN = 1e5 + 10;13 const int INF = 0x3f3f3f3f;14 int a[MAXN];15 int n;16 17 bool check(int c)18 {19 int p = -1e6;20 for (int i=1; i<=n; ++i)21 {22 int now = max (p + 1, a[i] - c);23 if (now > a[i] + c) return false;24 p = now;25 }26 27 return true;28 }29 30 int main(void) //2015百度之星初赛1 1003 序列变换31 {32 int t, cas = 0;33 scanf ("%d", &t);34 while (t--)35 {36 scanf ("%d", &n);37 for (int i=1; i<=n; ++i) scanf ("%d", &a[i]);38 39 int l = 0, r = 1e6;40 while (l < r)41 {42 int mid = (l + r) >> 1;43 if (check (mid)) r = mid;44 else l = mid + 1;45 }46 47 printf ("Case #%d:\n", ++cas);48 printf ("%d\n", l);49 }50 51 return 0;52 }53 54 /*55 256 257 1 1058 359 2 5 460 */

 

转载于:https://www.cnblogs.com/Running-Time/p/4541596.html

你可能感兴趣的文章
1034. 有理数四则运算(20)
查看>>
Pdf Convert Image 的解决方案
查看>>
仿绝地求生官网 jsp+css
查看>>
【Delphi】Base64加解密模块
查看>>
邻接表(网)
查看>>
HDU-1087-Super Jumping! Jumping! Jumping!(线性DP, 最大上升子列和)
查看>>
bash script 常用技能备忘
查看>>
Django框架简介
查看>>
java基础回顾
查看>>
CUDA开发存储器运用(包括存储器之间的转存)
查看>>
config模块
查看>>
做一个项目时遇到中文乱码,于是在入口文件加了个header("Content-type:text/html;charset=utf-8");结果一刷新网页就自动下载本网页文件;...
查看>>
entityframework
查看>>
嵌入式学习第一步:环境搭建
查看>>
【巧妙消维DP】【HDU2059】龟兔赛跑
查看>>
C#中索引器与属性的联系和区别
查看>>
图的定义与术语2 - 数据结构和算法55
查看>>
赛马问题
查看>>
Linux基础入门
查看>>
Nginx 配置文件 nginx.conf 详解
查看>>