01 背包问题的另类解法
2013-04-01 10:16
417 查看
百度百科和大部分网站的解释背包问题,
http://baike.baidu.com/view/1731915.htm
感觉时间复杂度O(m*n);
刚好又别人解看到腾讯问题的文章:
http://baike.baidu.com/view/1731915.htm
感觉背包问题可以只是求最终结果的话,可以用权重来解决;
时间复杂度O(n);
用java编写的如下代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Backpack {
public static void main(String[] args) {
Backpack test = new Backpack();
List<Rely> relyList = new ArrayList<Rely>();
relyList.add(test.new Rely(1, 1));
relyList.add(test.new Rely(3, 3));
relyList.add(test.new Rely(5, 3));
relyList.add(test.new Rely(10, 3));
relyList.add(test.new Rely(6, 8));
relyList.add(test.new Rely(7, 5));
relyList = test.calcWeight(relyList);
for (Rely rely : relyList) {
System.out.println(rely);
}
System.out.println("Max: "+test.anyazer(6, relyList));
}
public int anyazer(int limit, List<Rely> relyList) {
int max = 0;
int remainder;
int mark = 0;
for (Rely rely : relyList) {
if (limit / rely.engry >= 1) {
max = limit / rely.engry * rely.happiness;
remainder = limit % rely.engry;
mark += max;
if (remainder != 0) {
limit = remainder;
continue;
}
break;
}
}
return mark;
}
public List<Rely> calcWeight(List<Rely> relyList) {
List<Rely> weightList = new ArrayList<Rely>();
for (Rely rely : relyList) {
Double weight = (double) rely.happiness / (double) rely.engry;
rely.setWeight(weight);
weightList.add(rely);
}
Collections.sort(weightList, Collections.reverseOrder());
return weightList;
}
public class Rely implements Comparable<Rely> {
private int happiness;
private int engry;
private double weight;
public Rely() {
}
public Rely(int happiness, int engry) {
this.happiness = happiness;
this.engry = engry;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getHappiness() {
return this.happiness;
}
public int getEngry() {
return this.engry;
}
@Override
public String toString() {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append(happiness).append("\t").append(engry)
.append("\t").append(weight);
return strBuilder.toString();
}
@Override
public int compareTo(Rely o) {
int flag;
flag = weight > o.weight ? 1 : (weight == o.weight ? 0 : -1);
if (flag == 0) {
flag = engry > o.engry ? 1 : (engry == o.engry ? 0 : -1);
}
return flag;
}
}
}
http://baike.baidu.com/view/1731915.htm
感觉时间复杂度O(m*n);
刚好又别人解看到腾讯问题的文章:
http://baike.baidu.com/view/1731915.htm
感觉背包问题可以只是求最终结果的话,可以用权重来解决;
时间复杂度O(n);
用java编写的如下代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Backpack {
public static void main(String[] args) {
Backpack test = new Backpack();
List<Rely> relyList = new ArrayList<Rely>();
relyList.add(test.new Rely(1, 1));
relyList.add(test.new Rely(3, 3));
relyList.add(test.new Rely(5, 3));
relyList.add(test.new Rely(10, 3));
relyList.add(test.new Rely(6, 8));
relyList.add(test.new Rely(7, 5));
relyList = test.calcWeight(relyList);
for (Rely rely : relyList) {
System.out.println(rely);
}
System.out.println("Max: "+test.anyazer(6, relyList));
}
public int anyazer(int limit, List<Rely> relyList) {
int max = 0;
int remainder;
int mark = 0;
for (Rely rely : relyList) {
if (limit / rely.engry >= 1) {
max = limit / rely.engry * rely.happiness;
remainder = limit % rely.engry;
mark += max;
if (remainder != 0) {
limit = remainder;
continue;
}
break;
}
}
return mark;
}
public List<Rely> calcWeight(List<Rely> relyList) {
List<Rely> weightList = new ArrayList<Rely>();
for (Rely rely : relyList) {
Double weight = (double) rely.happiness / (double) rely.engry;
rely.setWeight(weight);
weightList.add(rely);
}
Collections.sort(weightList, Collections.reverseOrder());
return weightList;
}
public class Rely implements Comparable<Rely> {
private int happiness;
private int engry;
private double weight;
public Rely() {
}
public Rely(int happiness, int engry) {
this.happiness = happiness;
this.engry = engry;
}
public void setWeight(double weight) {
this.weight = weight;
}
public int getHappiness() {
return this.happiness;
}
public int getEngry() {
return this.engry;
}
@Override
public String toString() {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append(happiness).append("\t").append(engry)
.append("\t").append(weight);
return strBuilder.toString();
}
@Override
public int compareTo(Rely o) {
int flag;
flag = weight > o.weight ? 1 : (weight == o.weight ? 0 : -1);
if (flag == 0) {
flag = engry > o.engry ? 1 : (engry == o.engry ? 0 : -1);
}
return flag;
}
}
}
相关文章推荐
- 01背包问题的优化解法
- 01背包问题的动态规划解法
- 01背包问题的一维数组解法
- 又见01背包。(另类的01背包问题)
- 01背包问题,dp和贪心解法(c++11)
- 01背包问题 动态规划解法
- Exercise(4):01背包(简单动规)问题的两种解法
- 01背包问题的动态规划算法、蛮力法和空间优化算法
- 基础01背包问题
- hdu 1226(背包问题解法)
- 01背包问题
- 背包问题(01背包,完全背包,多重背包)
- 动态规划7:砝码和种类优化成01背包问题
- 0-1背包问题的两种解法(回溯法和动态规划)
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
- 01背包问题
- 背包问题-背包01-zb的生日
- 01背包问题
- 背包问题---01背包
- 01背包问题