1257 背包问题 V3

3 秒 131,072 KB 80 分 5 级题

题意 :

从n个物品中选出k个,使单位体积价值最大

思路:

一开始正面想,试过很多种,排序什么的、、总是结果不对,最后想到二分答案


二分的规则是使index的前接近0即可

ps:blocks[i].w物体的价值 block[i].p物体的体积 p二分答案

假设p是我们要的答案,那么block[i].p*pblock[i]应该占有的价值

blocks[i].w - block[i].p * p 为现在与目标价值的差

这个差约小说明越接近答案,根据这一位进行排序,只要前k个。记录当前总体积和总价值即可。

import java.util.Arrays;
import java.util.Scanner; public class Main {
static int n, k;
static int ansA = 0, tA = 0;
static int ansB = 0, tB = 0;
static Block[] blocks = null; public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
n = cin.nextInt();
k = cin.nextInt();
blocks = new Block[n];
for (int i = 0; i < n; i++) {
int a = cin.nextInt();
int b = cin.nextInt();
blocks[i] = new Block(a, b, 0);
}
cin.close();
double left = 0, right = 50000.0;
for (int i = 1; i < 100; i++) {
double mid = (left + right) / 2.0;
if (check(mid)) {
ansA = tA;ansB = tB;tA = tB = 0;
left = mid;
} else {
ansA = tA;ansB = tB;tA = tB = 0;
right = mid;
}
}
int x = gcd(ansA, ansB);
System.out.println(ansB / x + "/" + ansA / x);
} public static boolean check(double p) {
for (int i = 0; i < n; i++) {
blocks[i].dis = 1.0 * blocks[i].w - blocks[i].p * p;
}
Arrays.sort(blocks);
double sum = 0;
for (int i = 0; i < k; i++) {
tA += blocks[i].w;
tB += blocks[i].p;
sum += blocks[i].dis;
}
if (sum > 0) return true;
return false;
} static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
} static class Block implements Comparable<Block> {
int w;
int p;
double dis; public Block(int w, int p, double dis) {
this.w = w;
this.p = p;
this.dis = dis;
} @Override
public int compareTo(Block o) {
if (dis > o.dis) return 1;
return -1;
}
}
}

1257 背包问题 V3(二分)的更多相关文章

  1. 51nod 1257 背包问题 V3

    1257 背包问题 V3 基准时间限制:3 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2.. ...

  2. 51nod 1257 背包问题 V3(这不是背包问题是二分)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1257 题解:不能按照单位价值贪心,不然连样例都过不了 要求的 ...

  3. 51nod 1257 背包问题 V3(分数规划)

    显然是分数规划...主要是不会求分数的形式,看了题解发现自己好傻逼QAQ 还是二分L值算出d[]降序选K个,顺便记录选择时候的p之和与w之和就可以输出分数形式了... #include<iost ...

  4. 1257 背包问题 V3——分数规划

    N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi为整数),从中选出K件物品(K <= N),使得单位体积的价值最大. Input 第1 ...

  5. 51nod1257 背包问题 V3

    分数规划经典.开始精度1e-3/1e-4都不行,1e-5就A了 #include<cstdio> #include<cstring> #include<cctype> ...

  6. 51nod——1086、1257背包问题V2(多重背包二进制拆分转01) V3(分数规划+二分贪心)

    V3其实和dp关系不大,思想挂标题上了,丑陋的代码不想放了.

  7. 51nod 1257 01分数规划/二分

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1257 1257 背包问题 V3 基准时间限制:3 秒 空间限制:1310 ...

  8. hdu 1025LIS思路同1257 二分求LIS

    题目: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  9. CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

随机推荐

  1. SQL Server 日期转换成字符串

    参考网址:http://wenku.baidu.com/view/970c6c1655270722192ef70e.html 下面是常用的几个 --返回06-27-13 ), ) --2013-06- ...

  2. 0 Java实现 一篇文章说尽设计模式之六大原则

    我们知道,设计模式很有用,学好设计模式不但能让你写出更简洁,优雅的代码,还能使得代码的结构更清晰,也更有利于扩展 当然设计模式也不是万能的,一成不变的.设计模式只是前人总结出来的一种经验,一种特定问题 ...

  3. 洛谷P4364 [九省联考2018]IIIDX(线段树)

    传送门 题解看得……很……迷? 因为取完一个数后,它的子树中只能取权值小于等于它的数.我们先把权值从大到小排序,然后记$a_i$为他左边(包括自己)所有取完他还能取的数的个数.那么当取完一个点$x$的 ...

  4. UVA - 10859 Placing Lampposts 放置街灯

    Placing Lampposts 传送门:https://vjudge.net/problem/UVA-10859 题目大意:给你一片森林,要求你在一些节点上放上灯,一个点放灯能照亮与之相连的所有的 ...

  5. 【原创】《从0开始学RocketMQ》—单机搭建

    内容目录 1. RocketMQ是什么? 2. 下载并解压 3. 启动NameServer 4. 启动 Broker 5. 关闭消息队列 1. RocketMQ是什么? RocketMQ是一种消息队列 ...

  6. 使用Docker Compose编排微服务

    一般微服务架构会包含若干个微服务,而每个微服务可以有多个实例,如果每个微服务都有手动启停,那么效率就很低.维护量比较大. 所以我们可以使用Docker Compose来轻松.高效地管理容器. 一.安装 ...

  7. [POI2009]Tab

    Description 2个n\(\times\)m矩阵,保证同一个矩阵中元素两两不同.问能否通过若干次交换两行或交换两列把第一个矩阵变成第二个. Input 第一行正整数T(1≤T≤10)表示数据组 ...

  8. hdu 1044 Collect More Jewels

    题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...

  9. 题解报告:poj 2503 Babelfish(map)

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  10. 题解报告:hdu 1075 What Are You Talking About

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 Problem Description Ignatius is so lucky that he ...