我正在尝试解决: https://www.spoj.com/problems/ANARC05B/
我正在使用二进制搜索来解决此问题。 我想我遵循了正确的方法。 有人可以帮我解决一个失败的测试用例吗?
下面是我的代码,它通过了示例测试用例,我没有看到我的代码有问题,但是仍然不确定为什么我会得到WA!请帮忙!
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int BinarySearch(vector<int> arr, int key)
{
int minNum = 0;
int maxNum = arr.size() - 1;
while (minNum <= maxNum)
{
int mid = (minNum + maxNum) / 2;
if (key == arr[mid])
{
return mid;
}
else if (key < arr[mid])
{
maxNum = mid - 1;
}
else
{
minNum = mid + 1;
}
}
return -1;
}
signed long Solve(vector<int> First, vector<int> Second)
{
// Find the intersections in both the arrays and store the indices for them.
vector<int> FirstInterIndices;
vector<int> SecondInterIndices;
for (int i = 0; i < Second.size(); i++)
{
int idx = BinarySearch(First, Second[i]);
if (idx != -1)
{
SecondInterIndices.push_back(i);
FirstInterIndices.push_back(idx);
}
}
if (FirstInterIndices.empty())
{
FirstInterIndices.push_back(FirstInterIndices.size() - 1);
SecondInterIndices.push_back(SecondInterIndices.size() - 1);
}
//Find Intersections ends
//Calc the sum upto intersections in both the arrays and store them
vector<signed long> FirstInterSum;
vector<signed long> SecondInterSum;
for (int i = 0; i < SecondInterIndices.size(); i++)
{
int k = 0;
signed long Sum = 0;
if (i > 0)
{
k = SecondInterIndices[i - 1] + 1;
}
for (; k <= SecondInterIndices[i]; k++)
{
Sum += (signed long)Second[k];
}
SecondInterSum.push_back(Sum);
}
signed long SumLeft = 0;
if (SecondInterIndices.size() > 0)
{
for (int k = SecondInterIndices[SecondInterIndices.size() - 1] + 1; k < Second.size(); k++)
{
SumLeft += (signed long)Second[k];
}
SecondInterSum.push_back(SumLeft);
}
for (int i = 0; i < FirstInterIndices.size(); i++)
{
int k = 0;
signed long Sum = 0;
if (i > 0)
{
k = FirstInterIndices[i - 1] + 1;
}
for (; k <= FirstInterIndices[i]; k++)
{
Sum += (signed long)First[k];
}
FirstInterSum.push_back(Sum);
}
if (FirstInterIndices.size() > 0)
{
SumLeft = 0;
for (int k = FirstInterIndices[FirstInterIndices.size() - 1] + 1; k < First.size(); k++)
{
SumLeft += (signed long)First[k];
}
FirstInterSum.push_back(SumLeft);
}
// Calc sum upto intersections ENDs
// Compare corresponding elements (sum upto intersections) and add the max from First and Second
signed long MaxSum = 0;
int j = 0;
for (; j < FirstInterSum.size(); j++)
{
if (j < SecondInterSum.size())
{
if (FirstInterSum[j] > SecondInterSum[j])
{
MaxSum += FirstInterSum[j];
}
else
{
MaxSum += SecondInterSum[j];
}
}
}
// If Any sum exists after last intersection add that as well.
if (j < FirstInterSum.size())
{
MaxSum += FirstInterSum[FirstInterSum.size() - 1];
}
if (j < SecondInterSum.size())
{
MaxSum += SecondInterSum[SecondInterSum.size() - 1];
}
return MaxSum;
}
vector<int> getArray()
{
vector<int> res;
int n;
cin >> n;
int x;
for (int i = 0; i < n; i++)
{
cin >> x;
res.push_back(x);
}
return res;
}
int main()
{
for (;;)
{
vector<int> First = getArray();
if (First.size() == 0)
return 0;
vector<int> Second = getArray();
cout << Solve(First, Second);
}
return 0;
}
答案 0 :(得分:2)
您得到WA,因为输出格式不正确。我运行了您的代码,输出看起来像这样:
13 3 5 7 9 20 25 30 40 55 56 57 60 62
11 1 4 7 11 14 25 44 47 55 57 100
4 -5 100 1000 1005
3 -12 1000 1001
0450
2100
Program ended with exit code: 0
但预期的格式应为:
13 3 5 7 9 20 25 30 40 55 56 57 60 62
11 1 4 7 11 14 25 44 47 55 57 100
4 -5 100 1000 1005
3 -12 1000 1001
0
450
2100
Program ended with exit code: 0
差异为0,而450不在同一行。
答案 1 :(得分:0)
但是此问题不需要二进制搜索或动态编程(因为它是在站点上标记的),并且可以在线性时间内解决(最好的解决方法)
制作两个索引-第一个数组和第二个数组。
在每一步上,移动索引都引用较小的元素(例如在合并排序的合并过程中)。移动时,计算当前总和(相交点之间的总和)。
当两个索引指向相等的值时,您已经找到了交点。从两个数组中选择更大的当前总和,将其与相交项相加得出结果,将总和重置为零。
伪代码草图
# This works. It shows column A
https://docs.google.com/spreadsheets/d/e/2PACX-1vS-vH8TLPDj64Xqm2DQaS4MKT13y9yu6Nz69cwEw-sd-VapPyG_iW4_nrs2XxpMEdN41hbrfb_SwA2c/pubhtml?gid=0&single=true&range=A:A
# None of these work. I'd like to see columns A and C
https://docs.google.com/spreadsheets/d/e/2PACX-1vS-vH8TLPDj64Xqm2DQaS4MKT13y9yu6Nz69cwEw-sd-VapPyG_iW4_nrs2XxpMEdN41hbrfb_SwA2c/pubhtml?gid=0&single=true&range=A:A,C:C
https://docs.google.com/spreadsheets/d/e/2PACX-1vS-vH8TLPDj64Xqm2DQaS4MKT13y9yu6Nz69cwEw-sd-VapPyG_iW4_nrs2XxpMEdN41hbrfb_SwA2c/pubhtml?gid=0&single=true&range={A:A,C:C}
https://docs.google.com/spreadsheets/d/e/2PACX-1vS-vH8TLPDj64Xqm2DQaS4MKT13y9yu6Nz69cwEw-sd-VapPyG_iW4_nrs2XxpMEdN41hbrfb_SwA2c/pubhtml?gid=0&single=true&range=query({A:A,C:C})
https://docs.google.com/spreadsheets/d/e/2PACX-1vS-vH8TLPDj64Xqm2DQaS4MKT13y9yu6Nz69cwEw-sd-VapPyG_iW4_nrs2XxpMEdN41hbrfb_SwA2c/pubhtml?gid=0&single=true&range=query(A:A,C:C)