求資料對最大值
// arithmetic.cpp : 定義控制檯應用程式的入口點。
//
#include "stdafx.h"
#include<iostream>
#include<algorithm>
#include <vector>
#include <Windows.h>
using namespace std;
struct Result
{
std::vector<int> m_idxs;
int m_sumA;
int m_sumB;
};
void UpdateResult(Result& rlt, const std::vector<int>& A, const std::vector<int>& B, int idx, std::vector<Result>& newRlt)
{
// if ((rlt.m_sumA + A[idx] < 0) || (rlt.m_sumB + B[idx]) < 0)
// {
// return;
// }
if (A[idx] >=0 && B[idx] >= 0)
{
rlt.m_idxs.push_back(idx);
rlt.m_sumA += A[idx];
rlt.m_sumB += B[idx];
}
else
{
Result tmp = rlt;
tmp.m_idxs.push_back(idx);
tmp.m_sumA += A[idx];
tmp.m_sumB += B[idx];
newRlt.push_back(tmp);
}
}
void MakeResultSig(Result& rlt, const std::vector<int>& A, const std::vector<int>& B, int idx)
{
rlt.m_idxs.push_back(idx);
rlt.m_sumA = A[idx];
rlt.m_sumB = B[idx];
}
void UpdateResults(std::vector<Result>& rlt, const std::vector<int>& A, const std::vector<int>& B, int idx)
{
std::vector<Result> newRlt;
for (std::vector<Result>::iterator it = rlt.begin();it != rlt.end(); ++it)
{
Result& r = *it;
UpdateResult(r, A, B, idx, newRlt);
}
Result sig;
MakeResultSig(sig, A, B, idx);
rlt.insert(rlt.end(),newRlt.begin(), newRlt.end());
rlt.push_back(sig);
}
int GetMaxPairVec(const std::vector<int>& A, const std::vector<int>& B, int N, std::vector<Result>& rlt)
{
for (int i = 0; i < N; ++i)
{
if (A[i] <= 0 && B[i] <=0)
{
continue;
}
UpdateResults(rlt, A, B, i);
}
int sum = 0;
Result* pResult = NULL;
for(std::vector<Result>::iterator it = rlt.begin(); it != rlt.end(); ++it)
{
Result& r = *it;
if (r.m_sumA < 0 || r.m_sumB < 0)
{
continue;
}
if ((r.m_sumA + r.m_sumB) > sum)
{
sum = r.m_sumA + r.m_sumB;
pResult = &r;
}
}
if (pResult != NULL)
{
std::cout << pResult->m_sumA + pResult->m_sumB << std::endl;
for (std::vector<int>::iterator it = pResult->m_idxs.begin(); it != pResult->m_idxs.end(); ++it)
{
std::cout << A[*it] << "," << B[*it] << std::endl;
}
}
return sum;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<Result> vResult;
std::vector<int> A;
A.push_back(-403);
A.push_back(-847);
A.push_back(-624);
A.push_back(-293);
A.push_back(886);
A.push_back(9);
A.push_back(110);
A.push_back(39);
A.push_back(942);
A.push_back(-1342);
std::vector<int> B;
B.push_back(-625);
B.push_back(901);
B.push_back(-708);
B.push_back(413);
B.push_back(709);
B.push_back(-3);
B.push_back(-102);
B.push_back(729);
B.push_back(-3);
B.push_back(503);
DWORD t1,t2;
t1 = GetTickCount();
int c = GetMaxPairVec(A, B, 10, vResult);
t2 = GetTickCount();
printf("\t");
printf("Use Time :%f\n",(t2-t1)*1.0/1000);
//typedef boost::shared_ptr<Node> HTTPReplyPtr;
//unsigned long n = GetThreadId();
//{
// std::vector<Node> vNodes(2);
// vNodes.reserve(5);
// Node s(100);
// vNodes.push_back(s);
// vNodes.push_back(s);
// vNodes.push_back(s);
// //vNodes.push_back(s);
// //vNodes.push_back(s);
// //Node n(200);
// //vNodes.push_back(n);
//}
//std::cout << default_constuctor_size << std::endl;
//std::cout << param_constuctor_size << std::endl;
//std::cout << copy_constuctor_size << std::endl;
//std::cout << set_constuctor_size << std::endl;
//std::cout << destuctor_size << std::endl;
getchar();
return 0;
}