考虑点
这个题,本意上是有选择地进行最优组合,但如果变成全部匹配了之后的最优呢?
似乎变得更简单了?
前者,不需要考虑正负交叉项,后者,只要关注一个变量就好。
具体如下:
前者的循环结构:
while (x < Nc && x < Np && coupon[x] < 0 && product[x] < 0) {
amount += coupon[x] * product[x];
x++;
}
//from the biggest to > 0
x = Nc - 1;
y = Np - 1;
while (x >= 0 && y >= 0 && coupon[x] > 0 && product[y] > 0) {
amount += coupon[x] * product[y];
x--, y--;
}
后者的循环结构
while (x < Nc && x < Np && coupon[x] < 0) {
amount += coupon[x] * product[x];
x++;
}
//from the biggest to > 0
x = Nc - 1;
y = Np - 1;
while (x >= 0 && y >= 0 && coupon[x] > 0) {
amount += coupon[x] * product[y];
x--, y--;
}
对比之下,会发现,实际考察的算法思想很简明,想到这是最优解的问题,那么就会清晰很多了。