Reason, really some inexplicable, the Dfs belt should be less than the parameters should be faster, because without copying so many parameters, directly modify the global variable or pass the pointer. But in fact, with more parameters than fast 300ms, really fast a lot of time limit 1500ms, with parameter 1000ms, without the parameter 1300ms. It's so weird ... Is the address too far, the addressing is too slow ...
And also.. If your code is about 1500ms, it's still a question of probability, good luck and bad luck. It's really drunk.
But reducing the call to a function can be significantly faster.
So write the code in the future or try to write a little bit of detail, maybe it is not the algorithm you have a problem, but some of the details are not well handled.
At the beginning of the timeout, modified some details on the past.
1, to determine whether OK no longer write a separate function, directly with DFS completed.
2, map changed to set.
3, in the beginning will also be a,b,c ascending sort, and then judge, in fact, is not not in ascending order directly discarded.
4, weighing only need a make_pair (a,b) is enough, do not need to take C.
5, at the beginning also wrote a structure, it is a waste of time.
6, the long long to int, this estimate is useless.
N<=15, decisively on the violence enumerated O (3^n).
Code
#include <stdio.h>
#include <algorithm>
#include <set>
using namespace std;
int fen[20];
int ans;
int n;
typedef pair<int,int>pii;
set<pii>s;
void Dfs (int cur,int a,int b,int c)
{
if (cur>n)
{
if (a==0| | b==0| | c==0) return;
if (a>c| | b>c| | A>B) return;
PII P=make_pair (a,b);
if (S.count (p)) return;
S.insert (p);
if (a+b<=c) return;
ans++;
return;
}
DFS (CUR+1,A+FEN[CUR],B,C);
DFS (CUR+1,A,B+FEN[CUR],C);
DFS (Cur+1,a,b,c+fen[cur]);
int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
ans=0;
S.clear ();
scanf ("%d", &n);
for (int i=1;i<=n;i++) scanf ("%d", &fen[i]);
DFS (1,0,0,0);
printf ("%d\n", ans);
}
return 0;
}