51Nod-1257-背包问题 V3
2017-04-14 13:05
246 查看
ACM模版
![](http://img.blog.csdn.net/20170414125711904?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZl96eWo=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
描述
题解
典型的0-1分数规划,二分单位体积价值即可。0-1分数规划属于较简单易学的算法,本以为
背包问题 V3一定是一个更难的动态规划,谁知道只是一个二分。不懂
0-1分数规划的可以看看参考一栏的 blog,其实很简单的,只要知道
0-1分数规划,这个题可以算是5级题比较简单的问题了。
代码
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespace std; const int MAXN = 5e4 + 10; const double ESP = 1e-6; int N, K; struct article { int W, P; double unit; } atc[MAXN]; int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } int cmp(article a, article b) { return a.unit > b.unit; } int charge(int &x, int &y, double m) { for (int i = 0; i < N; i++) { atc[i].unit = atc[i].P - atc[i].W * m; } sort(atc, atc + N, cmp); x = y = 0; double temp = 0; for (int i = 0; i < K; i++) { x += atc[i].P; y += atc[i].W; temp += atc[i].unit; } if (temp >= 0) { return 1; } else { return 0; } } int main(int argc, const char * argv[]) { cin >> N >> K; for (int i = 0; i < N; i++) { scanf("%d%d", &atc[i].W, &atc[i].P); } double left = 0, right = MAXN, mid; int z = 0, m = 0, x, y; while (fabs(right - left) > ESP) { mid = (left + right) / 2; if (charge(x, y, mid)) { left = mid; z = x; m = y; } else { right = mid; } } int temp = gcd(z, m); z /= temp; m /= temp; printf("%d/%d\n", z, m); return 0; }
参考
《0-1分数规划》相关文章推荐
- 51nod 1257 背包问题V3
- 51nod 1257 背包问题 V3
- 51nod 1257 背包问题 V3(分数规划)
- [51nod] 1257 背包问题 V3
- 51nod 1257 背包问题 V3
- 51nod 1257 背包问题 V3(二分)
- 51 nod oj 1257 背包问题 V3【01分数规划】
- 1257 背包问题 V3——分数规划
- 51Nod 1085 背包问题
- 51nod 1086 背包问题V2(巧妙拆分多重背包)
- 51Nod 有限背包计数问题 题解报告
- 51nod 1086 背包问题 V2
- 51nod-0-1背包问题
- 51nod-【多重背包问题】
- 51Nod 1085 背包问题(01背包 )
- 51nod 1086 背包问题 V2(多重背包)
- 51NOD 背包问题v2(动态规划)
- 51nod-1086 背包问题(多重背包)
- 51Nod-1085-背包问题
- 51nod 1085 背包问题