51Nod-1257-背包问题 V3
来源:互联网 发布:marginnote windows版 编辑:程序博客网 时间:2024/04/29 15:27
ACM模版
描述
题解
典型的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分数规划》
0 0
- 51nod 1257 背包问题 V3
- 51nod 1257 背包问题V3
- 51Nod-1257-背包问题 V3
- 51nod 1257 背包问题 V3
- 51 nod oj 1257 背包问题 V3【01分数规划】
- 51nod 1257 背包问题 V3(二分)
- 背包问题-----51Nod
- 【51Nod1257】背包问题 V3
- 51nod 1085 背包问题
- 51nod多重背包问题
- 51nod 1085 背包问题
- 51nod 多重背包问题
- 51Nod--1085背包问题
- 51nod-【多重背包问题】
- 51nod 1085 背包问题
- 51Nod 1085 背包问题
- 51Nod 1085 背包问题
- 51nod 1085 背包问题
- idea2016_64bit 控制台输出中文乱码
- HttpClient+Jsoup 抓取网页信息(网易贵金属为例)
- webview无法正常加载和点击的bug
- ROS:三个节点相互传递消息,每个节点都可以同时发布和订阅主题
- angular选项卡
- 51Nod-1257-背包问题 V3
- 文本属性2
- HTML实现贪吃蛇
- Redis 命令
- Android设置状态栏为透明
- 读书学习心得
- cocoapods出现的问题
- oracle 存储过程、存储函数、程序包
- jquery.form.js提交form表单 上传文件