由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Algorithm Problem
相关主题
问个算法题2Algorithm in C++大家怎么准备?
Newest coding problemAlgorithms的书
Some coding problems from Amazon面试的时候可以用STL吗
一个疑惑A Video algorithm job from a headhunter (转载)
assignment problem 这个有人考到过吗?问一下,google 面试
再来一道题Algorithm in C完整版下载
第一次onsite感想Best C++ book
epi 还是 The Algorithm Design Manual本版mj pdf合集
相关话题的讨论汇总
话题: dp话题: int话题: dpelement
进入JobHunting版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
In an exam you have n problems to work on. i=0, 1, ..., n-1.
The i-th problem takes you x[i] minutes, and give you a score s[i].
You have 60 minutes to work on them. How can you get the best score?
c**********e
发帖数: 2007
2
Assumptions:
1. You do not get partial credit on a problem.
2. If you spend x[i] minutes on i-th problem, you will get the s[i] points.
p*****2
发帖数: 21240
3
DP吧。
x*********n
发帖数: 46
4
Knapsack.
s******n
发帖数: 3946
D********g
发帖数: 650
6
/**
* In an exam you have n problems to work on. i=0, 1, ..., n-1.
The i-th problem takes you x[i] minutes, and give you a score s[i].
You have 60 minutes to work on them. How can you get the best score?
*/
static class DPElement {
int value;
int prevCostIdx; // 0-based cost idx
int takenThisElement; // 0/1, indicating whether taking current
element, idx 0 based.
public DPElement() {
value = 0;
prevCostIdx = 0;
takenThisElement = 0;
}

}
static void knapSackProblemSolving(final int[] x, final int[] s, final
int t) {
if (x == null || s == null || x.length == 0 || s.length == 0 || x.
length != s.length || t <= 0) {
throw new IllegalArgumentException();
}
DPElement[][] dp = new DPElement[x.length + 1][t + 1];
for (int i = 0; i < dp.length; ++i) {
for (int j = 0; j < dp[0].length; ++j) {
dp[i][j] = new DPElement();
}
}

for (int i = 1; i <= x.length; ++i) {
for (int j = 1; j <= t; ++j) {
int val = j >= x[i-1] ? s[i - 1] + dp[i-1][j-x[i-1]].value :
0;
if (x[i-1] <= j && val > dp[i-1][j].value) {
dp[i][j].value = val;
dp[i][j].prevCostIdx = j - x[i-1];
dp[i][j].takenThisElement = 1;
} else {
dp[i][j].value = dp[i-1][j].value;
dp[i][j].prevCostIdx = j;
dp[i][j].takenThisElement = 0;
}
}
}
System.out.println("Best score is " + dp[x.length][t].value);
System.out.print("Taken elements: {");
int time = t;
for (int i = x.length; i > 0; --i) {
if (dp[i][time].takenThisElement == 1) {
System.out.print("<" + (i - 1) + "," + x[i - 1] + ">");
}
time = dp[i][time].prevCostIdx;
}
System.out.println("}");
}

static void testKnapSackProblemSolving() {
int[] x = new int[] {59, 50, 10, 5, 2, 3, 58, 57, 1};
int[] s = new int[] {117, 99, 20, 10, 3, 5, 115, 114, 1};
knapSackProblemSolving(x, s, 60);
}

【在 x*********n 的大作中提到】
: Knapsack.
1 (共1页)
进入JobHunting版参与讨论
相关主题
本版mj pdf合集assignment problem 这个有人考到过吗?
买书给点意见再来一道题
请推荐算法的书第一次onsite感想
问个G的intern面试epi 还是 The Algorithm Design Manual
问个算法题2Algorithm in C++大家怎么准备?
Newest coding problemAlgorithms的书
Some coding problems from Amazon面试的时候可以用STL吗
一个疑惑A Video algorithm job from a headhunter (转载)
相关话题的讨论汇总
话题: dp话题: int话题: dpelement