【发布时间】:2014-09-07 08:04:33
【问题描述】:
我试图解决 CodeForces 第 255 轮中的“DZY Loves Modification”问题。
我在系统测试用例中得到了错误的答案。
我的做法如下:
- 构造两个 max-heaps - 一个用于存储行总数和 一个用于列总计
- 存储两个变量 - rowReductionValue 和 colReductionValue - 这些变量存储之前应该减去的值 使用行或列的总数总和。
- 对于每次迭代,从 rowSum 和 colSum 堆
- 如果选择了一行,则将该行的总数添加到结果中,然后 行总数减少了 P * (number_of_columns)
- 此后,每列的值减少 P。所以,这个 P 是 添加到 colReductionValue。
- 如果选择了列,则会使用类似的方法。
这种方法会导致错误的答案。
很抱歉,如果我在解释我的方法时不清楚,我希望它尽可能简洁。
非常感谢任何关于正确方法或该方法缺陷的说明。
代码如下:
long long getMaxResult(int rows, int cols, int reductionValue, int K)
{
vector<long long> rowSums, colSums;
long long rowReductionValue = 0, colReductionValue = 0, result = 0;
for(int row = 0; row < rows; ++row)
{
long long sum = 0;
for(int col = 0; col < cols; ++col)
{
sum += a[row][col];
}
rowSums.push_back(sum);
}
for(int col = 0; col < cols; ++col)
{
long long sum = 0;
for(int row = 0; row < rows; ++row)
{
sum += a[row][col];
}
colSums.push_back(sum);
}
make_heap(rowSums.begin(), rowSums.end());
make_heap(colSums.begin(), colSums.end());
for(int k = 0; k < K; ++k)
{
pop_heap(rowSums.begin(), rowSums.end());
long long rowMax = rowSums.back();
rowSums.pop_back();
pop_heap(colSums.begin(), colSums.end());
long long colMax = colSums.back();
colSums.pop_back();
if(rowMax - rowReductionValue >= colMax - colReductionValue)
{
result += rowMax - rowReductionValue;
rowMax -= reductionValue * cols;
colReductionValue += reductionValue;
}
else
{
result += colMax - colReductionValue;
colMax -= reductionValue * rows;
rowReductionValue += reductionValue;
}
rowSums.push_back(rowMax);
push_heap(rowSums.begin(), rowSums.end());
colSums.push_back(colMax);
push_heap(colSums.begin(), colSums.end());
}
return result;
}
谢谢-
【问题讨论】: