POJ3045 Cow Acrobats 二分搜索
2014-07-17 20:51
309 查看
题目大意是N头牛,每头牛重量wi,力量si,现在这些牛以某种顺序踩在其它牛身上堆积成垂直的一个高度。每头牛滑落的风险为在它身上所有其他牛的重量减去它的力量。现在需要计算出最大的一头的风险最小值是多少。
这题可以用二分搜索进行求解。设C(X):=最大一头牛的风险不超过X。问题是如何判断C(X)是否成立。初始时可以保存每头牛驼上其他所有牛的风险。对于X,最底下的牛肯定是选择风险最小的牛,当选择了这头牛作为最底下的牛时,其它所有牛的风险都会减少相同的值,因此,他们的大小关系都不变,所以只需要排一次序,然后每次都选择风险最小的牛从下往上放就可以了。
这题可以用二分搜索进行求解。设C(X):=最大一头牛的风险不超过X。问题是如何判断C(X)是否成立。初始时可以保存每头牛驼上其他所有牛的风险。对于X,最底下的牛肯定是选择风险最小的牛,当选择了这头牛作为最底下的牛时,其它所有牛的风险都会减少相同的值,因此,他们的大小关系都不变,所以只需要排一次序,然后每次都选择风险最小的牛从下往上放就可以了。
#include <stdio.h> #include <vector> #include <math.h> #include <string.h> #include <string> #include <iostream> #include <queue> #include <list> #include <algorithm> #include <stack> #include <map> using namespace std; struct Weight { int weight; int strength; }; int compp(const void* a1, const void* a2) { Weight* w1 = (Weight*)a1; Weight* w2 = (Weight*)a2; if (w1->strength == w2->strength) { return w2->weight - w1->weight; } else return w1->strength - w2->strength; } Weight weight[50001]; bool CC(int x, int n) { int curdis = 0; for (int i = 0; i < n; i++) { if (weight[i].strength - curdis > x) { return false; } else curdis += weight[i].weight; } return true; } int main() { int n; #ifdef _DEBUG freopen("e:\\in.txt", "r", stdin); #endif scanf("%d\n", &n); int totalw = 0; int maxstrength = 0; for (int i = 0; i < n;i++) { scanf("%d %d\n", &weight[i].weight, &weight[i].strength); totalw += weight[i].weight; if (maxstrength < weight[i].strength) { maxstrength = weight[i].strength; } } for (int i = 0; i < n; i++) { weight[i].strength = totalw - weight[i].strength - weight[i].weight; //每头牛驼起其它所有牛的风险 } qsort(weight, n, sizeof(Weight), compp); int l = -(1 + maxstrength); int r = totalw + 1; while (r - l > 1) { int mid = (r + l) / 2; if (CC(mid, n)) { r = mid; } else l = mid; } printf("%d\n", r); return 0; }
相关文章推荐
- poj 3045 Cow Acrobats(二分搜索?)
- poj——1064Cable master(二分搜索 求绳子可以满足的最大长度)
- 关于二分搜索的写法以及正确性证明的思考
- 08-25 USACO 4.1~4.2 搜索,最大流,二分匹配
- 5-4 二分搜索数的查找
- Dfs+二分搜索(台州4310 走迷宫)
- 程序员面试之 二分搜索 旋转数组的最小元素
- poj 3579 Median (二分搜索之查找第k大的值)
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛 Problem E: qwb和李主席(折半搜索+类二分枚举)
- BZOJ 1082 栅栏 (二分 剪枝搜索)
- 二分搜索程序
- Professor GukiZ and Two Arrays CodeForces - 620D (暴力枚举+二分搜索)
- 二分搜索及测试函数
- POJ 1744 Elevator Stopping Plan 以及对二分搜索的思考
- 二分搜索学习笔记 Java实现
- POJ3061 ZOJ3123 Subsequence【前缀和+二分搜索+尺取法】
- “《编程珠玑》(第2版)第2章”:A题(二分搜索)
- UVALive5170 UVA231 POJ1887 Testing the CATCHER【最长下降子序列+DP+二分搜索】
- 二分搜索
- 二分搜索