
二分搜索
stdwal
天演在化,功成在学。知海无涯,见花问道。
展开
-
POJ3258-River Hopscotch
经典的二分,理解起来稍难....#include #include using namespace std;const int maxn = 50000+10;int rock[maxn];bool judge(int x, int n, int m) { int last = 0; for (int i = 1; i < n - m; i++) {原创 2016-07-23 13:16:45 · 274 阅读 · 0 评论 -
POJ2112-Optimal Milking
二分最大流答案,注意上界是INF而不是200.#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;const int maxk = 30 + 5;const int maxc = 200 + 5;const int maxv =原创 2016-10-31 22:33:22 · 358 阅读 · 0 评论 -
LA3635-Pie
二分#include <cstdio>#include <cmath>const int maxn = 10000 + 10;const double pi = acos(-1.0);double sz[maxn];int n, f;bool check(double mid) { int cnt = 0; for (int i = 0; i < n; i++) {原创 2016-09-27 09:13:04 · 372 阅读 · 0 评论 -
LA3971&&HDU2333&&POJ3497-Assemble
二分加贪心求最小值最大。 以下代码UVa用时49ms,HDU390ms,但在POJ就TLE了。 而且这个题实在让人费解,主要有两个疑惑: 1.使用lower_bound超时,遍历整个却不超时了。 2.mid用(lb + ub) / 2就会wa,必须要用mid = lb + (ub - lb + 1) / 2; 实在是难以理解…. 我真是太菜了….更新: 疑惑1:不能用lower_bou原创 2016-09-26 20:15:06 · 357 阅读 · 0 评论 -
POJ3662-Telephone Lines
这是一道二分搜索题,在判断条件上使用Dijkstra求最少的花费,令mid为所需的答案,那么长度大于mid即为需要免费的电线,这些电线的数量与k比较后确定答案的位置。#include <cstdio>#include <vector>#include <queue>#include <algorithm>using namespace std;const int inf = 1000 + 10原创 2016-08-06 20:32:47 · 520 阅读 · 0 评论 -
POJ1759-Garland
二分好题,只要把判断在地上的灯的数量是否大于0即可#include <cstdio>const int maxn = 1000 + 5;double h[maxn];int n;double a;//x为第二个灯的高度//cnt表示在地上的灯的数量bool test(double x) { h[0] = a; h[1] = x; int cnt = 0; fo原创 2016-08-06 11:55:45 · 226 阅读 · 0 评论 -
POJ3685-Matrix
观察此题给出的条件 i^2 + 100000 × i + j^2 - 100000 × j + i × j可知,这个函数值随i的增大而增大,因此在写二分条件的时候可以从每一列开始寻找小于等于mid的值是在该列的第几个,由此得出该列小于等于mid的个数,将所有列加起来就是矩阵中比mid小的所有个数,再与m比较即可。#include <cstdio>long long f(long long i, lo原创 2016-08-02 11:39:05 · 293 阅读 · 0 评论 -
POJ3579-Median
这是一道很难的二分搜索题,稀里糊涂的就AC了还没有弄懂为什么….#include <cstdio>#include <algorithm>using namespace std;const int maxn = 100000+5;int x[maxn];bool judge(int mid, int n, long long c) { int cnt = 0; for (int i原创 2016-07-29 16:57:44 · 281 阅读 · 0 评论 -
POJ3111-K Best
这道题与POJ2976类似,但是数据量貌似很大....二分最大化平均值#include #include #include using namespace std;const int maxn = 100000;const int inf = 10e6;int v[maxn];int w[maxn];struct rec { double y; in原创 2016-07-27 19:45:37 · 321 阅读 · 0 评论 -
POJ2976-Dropping tests
http://poj.org/showmessage?message_id=53194在while条件判断时要注意类似1 0的测试样例。#include #include #include using namespace std;const int maxn = 1000+5;const int inf = 10e9;int a[maxn];int b[maxn];d原创 2016-07-27 15:49:51 · 294 阅读 · 0 评论 -
POJ3104-Drying
首先很难想到用二分,其次判断时要用long long....#include #include using namespace std;const int maxn = 10e5+10;const int inf = 10e9+10;int a[maxn];bool judge(int mid, int n, int k) { long long minute =原创 2016-07-26 10:01:23 · 441 阅读 · 0 评论 -
POJ3273-Monthly Expense
二分搜索的思想比较容易了解,但是实现起来却经常有些细节方面的小错误导致wa#include #include using namespace std;const int maxn = 100000+10;int fajo[maxn];bool judge(int x, int n, int m) { int group = 1; int sum = 0;原创 2016-07-25 12:06:15 · 228 阅读 · 0 评论 -
POJ1064-Cable master
题目出的也是十分的坑。数据范围从1meter到100kilometers....解题的思路大致就是二分搜索,次数大概在100次左右。答案输出需要floor(ans*100)/100来避免最后答案四舍五入。#include #include const int maxn = 10000;double cab[maxn];bool judge(double a, int原创 2016-07-22 19:59:12 · 438 阅读 · 0 评论 -
POJ2456-Aggressive cows
通过二分搜索贪心的判断条件是否成立即可。#include #include using namespace std;const int maxn = 100000 + 5;const int inf = 10e9 + 10;int stall[maxn];bool judge(int d, int n, int c) { int last = 0; for原创 2016-07-22 20:53:24 · 280 阅读 · 0 评论 -
Ural1521-War Games 2
给定n和k求约瑟夫环中每次出圈的人的编号。 利用树状数组可以得到每个人的相对编号(在他之前有几个人)。 每次通过二分查找找到对应编号出圈的人,然后在他们的编号位置上-1.#include <cstdio>#include <vector>using namespace std;const int maxn = 100000+5;int fw[maxn];int n, k;inline int原创 2016-11-22 15:56:17 · 627 阅读 · 0 评论