[二分答案]HDU4004 The Frog's Games
来源:互联网 发布:剑灵灵剑捏脸数据导入 编辑:程序博客网 时间:2024/06/04 18:14
也不知道怎么回事,这几天遇到好几道二分答案的题,百度之星也有一道二分水题,于是趁此机会再搞了一下二分。
之前写二分都很纠结,不知道二分结束后指向的值是比目标大一个还是小一个,总之特判特判,搞的代码臭长。。
在数列里面二分都用STL的二分函数搞,然后二分答案的就手写,是r-1还是l+1似乎很有讲究啊,可惜我还没研究透彻。
这题没啥可以说的,就一个二分答案的水题,判断当前的MID是否合法用贪心,每次跳到最远就是最优的。
不过注意R范围,还有记得排序,总复杂度贪心O(N),二分O(logL),总复杂度O(N*logL)。
二分效率确实高啊,1e9的范围都不超过二分40次。
贴代码。
#include<cstdio>#include<algorithm>using namespace std;int stn[500005], n, m, rive;bool ok(int ma){ int now = stn[0],t = 0; for(int i = 1; i <= n+1; t += 1){ if(stn[i] - now > ma || t >= m) return 0; int p = upper_bound(stn, stn+n+2, now+ma) - stn; now = stn[p-1]; i = p; } return 1;}int main(){ while( scanf("%d%d%d", &rive, &n, &m)!=EOF ){ for(int i = 1; i <= n; ++i) scanf("%d", &stn[i]); sort(stn+1, stn+n+1); stn[0] = 0, stn[n+1] = rive; int l = 0, r = rive+5, mid; while( l < r ){ mid = (l + r) >> 1; if( ok(mid) ) r = mid; else l = mid + 1; } printf("%d\n", l); }}
0 0
- [二分答案]HDU4004 The Frog's Games
- hdu4004 The Frog's Games 二分
- HDU4004 The Frog's Games(二分)
- hdu4004 The Frog's Games
- hdu4004 The Frog's Games 二分查找+贪心 TWT Tokyo Olympic 1COMBO-2
- HDU 4004(The Frog's Games) 二分查找求答案
- The Frog's Games 二分
- The Frog's Games(二分)
- HDU 4004 THE FROG’S GAMES(二分)
- HDOJ 4004 The Frog's Games 二分
- HDU 4004 The Frog's Games(二分)
- HDU 4004 The Frog's Games(二分)
- hdu 4004 The Frog's Games【二分】
- hdu 4004 The Frog's Games 【二分】
- The Frog's Games 4004 (二分)***
- HUD The Frog's Games(二分)
- HDU 4004 The Frog's Games (二分)
- 4004 The Frog's Games --二分+贪心
- mysql 触发器
- 共享内存system v(未编译)
- 文章标题
- iserver集群实践——受控集群
- 深入理解计算机系统(笔记):计算机系统漫游
- [二分答案]HDU4004 The Frog's Games
- maven 教程一 入门
- LeetCode OJ 之 Reverse Linked List (链表逆序)
- YT02-简单数学课后题-1006 抱歉 -(5.31日-烟台大学ACM预备队解题报告)
- IP地址的三种表示格式 及 在Socket编程中的应用
- jquery正则表达式匹配
- Linux安装配置php
- C变参数函数demo
- Android NDK: How to Reduce Binaries Size