博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu5248 序列变换 二分
阅读量:4625 次
发布时间:2019-06-09

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

题目链接:

题意:

题解:

二分

从上一个位置到达这个位置,可以通过上一个位置推出当前位置必须到达的最小值now,如果now小于a[i]就无所谓了,因为肯定行,直接更新下一个now;如果now-a[i]大于了当前二分出的伸展极限就不行。

看了别人代码,还是看了很久,还是太菜啊。。

代码:

1 #include 
2 using namespace std; 3 typedef long long ll; 4 #define MS(a) memset(a,0,sizeof(a)) 5 #define MP make_pair 6 #define PB push_back 7 const int INF = 0x3f3f3f3f; 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; 9 inline ll read(){10 ll x=0,f=1;char ch=getchar();11 while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;ch=getchar();}12 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}13 return x*f;14 }15 //16 const int maxn = 1e5+10;17 18 int n,a[maxn];19 20 bool check(int x){21 int now=-INF; // 可能x很大,对于a[2]可以到的最小值a[1]-x+1可以变得很负22 for(int i=1; i<=n; i++){23 if(now-a[i]>x) return false;24 now = max(now+1,a[i]-x+1); // now表示当前值a[i]可以变到的最小的值+1之后就是a[i+1]的一个必须变到的值,a[i]-x+1也是,取他们的最大值就是a[i+1]必须要到达的最小值 如果a[i+1]>now,就一定可以,更新下一个now的时候,就是a[i+1]-x+1了。25 }26 return true;27 }28 29 int main(){30 int T = read();31 for(int cas=1; cas<=T; cas++){32 n = read();33 34 for(int i=1; i<=n; i++){35 a[i] = read();36 }37 38 int ans = 0;39 int l = 0, r = 1e6;40 while(l <= r){41 int mid = (l+r)/2;42 if(check(mid)) ans=mid,r=mid-1;43 else l=mid+1;44 }45 46 cout << "Case #" << cas << ":\n" << ans << endl;47 }48 49 return 0;50 }

 

转载于:https://www.cnblogs.com/yxg123123/p/6827632.html

你可能感兴趣的文章
Leetcode 80.删除排序数组中的重复项 II By Python
查看>>
常用JS加密编码算法
查看>>
spring boot中常用的配置文件的重写
查看>>
【线性规划和网络流24题】
查看>>
犹抱琵琶半遮面-OC
查看>>
标题颜色
查看>>
Python学习--和 Oracle 交互(2)
查看>>
VxWorks启动过程详解(上) 分类: vxWorks ...
查看>>
GJB150-2009军用装备实验室环境试验方法新版标准
查看>>
js面象对象插件+历史管理
查看>>
Spark学习体系整理(基础篇、中级篇、高级篇所涉及内容)
查看>>
网络编程之进程3
查看>>
Visual Studio Code 工具使用教程
查看>>
linux下shellcode编写入门
查看>>
selenium入门环境之浏览器问题
查看>>
BA--三相异步电机_星三角降压启动
查看>>
VM虚拟机安装后的网络设置
查看>>
jQuery Alert Dialogs (Alert, Confirm, & Prompt代替方案)
查看>>
牛客 109 C 操作数 (组合数学)
查看>>
Linux下通过 rm -f 删除大量文件时报错:Argument list too long
查看>>