@recursive 和@fatalme的蠕虫算法好,我试着写了下
struct Tuple
{
int val;
char from;//A,B,or C
};
int MinAbsDiff(vectr& A,vector& B,vector& C)
{
sort(A.begin(),A.end());
sort(B.begin(),B.end());
sort(C.begin(),C.end());
vector D = Merge(A,B,C);
int findCount[3]={0};
int count = 0;
int ans = 2147483647;
for(int start =0,end =0;end < D.length();end++)
{
if(findCount[D[end].from-'A'] == 0)count++:
findCount[D[end].from-'A']++;
if(count == 3)
{
while(findCount[D[start].from-'A'] > 1)
{
start++;
findCount[D[start].from-'A']--;
}
if(ans == 0)return 0;
ans = min(ans,2*(D[end].val - D[start].val));
}
}
return ans;
}
简单的测了几组数组:
{1,2,3,5};{-3,4,6};{4,6,6,9}; 2
{1,2,3,};{2,3,4};{6,7,8,}; 6
{-9,-5,-10,};{0};{8,2,5,}; 14
{1,2,};{6,5, 4};{7,9,8}; 10
{2,2};{5,5, 5};{8,8,8,8,8}; 12