# [Original] poj 1703 & rqnoj energy necklace solution report

Source: Internet
Author: User

Alas

I don't want to talk about anything anymore.

Poj 1703: it takes only 15 minutes to finish the code of the first version after reading the question.

And then from to the next morning

Finally, I found the bug. The question requested "Not sure yet." And I typed "no sure yet ."

Then there is the noip of rqnoj, a classic energy necklace.

It took 30 minutes to finish writing the pseudo code, and 10 minutes to complete the code.

Wa is performed 7 times, and only the first three vertices can be crossed for the next time. All the following vertices have timed out.

No? How can I time out when I plan all resources dynamically?

Start to optimize the main function.

It is found that the complexity of N * (N-1) can be reduced to C (n, 2) through a comparison ).

Submitted after modification. Good. One more point later, continue to time out

XXXX, I finally found that if (DP [start] [end] is missing in the recursive function! = 0) return DP [start] [end];!

At that time, my heart was truly overwhelmed by 10 thousand grass and mud horses. I am still young!

Then 10 points are AC in 10 ms.

Finally, the Code of the two friends

I sent 10 + wa in total, right?

I learned the best way.

Poj 1703:

# Include <stdio. h>
Typedef struct man
{
Int Rev;
Int parent;
} Man;
Man set [100008];
Int set_find (int p)
{
If (p <0) Return-1;
If (set [p]. Parent <0) return P;
Return set [p]. Parent = set_find (set [p]. Parent );
}
Void join (int p, int q)
{
P = set_find (P );
Q = set_find (Q );
If (P! = Q) set [p]. Parent = Q;
}
Void deal (int p, int q)
{
Int tmp_rev1;
Int tmp_rev2;
If (set [Q]. REV =-1) set [Q]. REV = P;
If (set [p]. REV =-1) set [p]. REV = Q;
Tmp_rev1 = set_find (set [p]. Rev );
Tmp_rev2 = set_find (set [Q]. Rev );
P = set_find (P );
Q = set_find (Q );
If (Q! = Tmp_rev1) join (Q, tmp_rev1 );
If (P! = Tmp_rev2) join (p, tmp_rev2 );
}
Void searchset (int p, int q)
{
P = set_find (P );
Q = set_find (Q );
If (P = q)
{
Printf ("in the same gang. \ n ");
}
Else if (P = set_find (set [Q]. Rev) | q = set_find (set [p]. Rev ))
{
Printf ("in different gangs. \ n ");
}
Else printf ("Not sure yet. \ n ");
}
Int main (void)
{
Int T;
Int n, m, I;
Int case;
Int num1, num2;
Char command;
Scanf ("% d", & T );
While (t --)
{
Scanf ("% d", & N, & M );
If (n = 2) Case = 1;
Else case = 0;
For (I = 0; I <= N; I ++)
{
Set [I]. Parent =-1;
Set [I]. REV =-1;
}
For (I = 0; I <m; I ++)
{
Getchar ();
Command = getchar ();
Scanf ("% d", & num1, & num2 );
If (! Case)
{
If (command = 'A ')
{
Searchset (num1, num2 );
}
Else if (command = 'D ')
{
Deal (num1, num2 );
}
}
Else printf ("in different gangs. \ n ");
}

}
Return 0;
}

Rqnoj energy necklace:

# Include <stdio. h>
Int DP [200] [200];
Int value [200];
Int N;
Int pre (int)
{
If (A = 0) return n-1;
Else return a-1;
}
Int next (int)
{
If (A = n-1) return 0;
Else return a + 1;
}
Int fun (INT start, int end)
{
Int tmp1, tmp2, I, j, max = 0, result;
If (DP [start] [end]! = 0) return DP [start] [end];
If (START = END) return 0;
For (I = start; I! = End; I = next (I ))
{
Tmp1 = fun (start, I );
Tmp2 = fun (next (I), end );
Result = tmp1 + tmp2 + value [start] * value [next (I)] * value [next (end)];
If (max <result) max = result;
}
Return DP [start] [end] = max;
}
Int main (void)
{
Int I, j, result;
Int Max, tmp1, tmp2;
Scanf ("% d", & N );
For (I = 0; I <n; I ++)
Scanf ("% d", & value [I]);
Max = 0;
Int COUNT = 0;
Int size;
For (I = 0; I <n; I ++)
{
Size = n-I-1;
Count = 0;
For (j = I; count <size; j = next (J), Count ++)
{
Tmp1 = fun (I, j );
Tmp2 = fun (next (J), pre (I ));
If (value [next (j)] <value [I])
Result = tmp1 + tmp2 + value [I] * value [next (j)] * value [I];
Else result = tmp1 + tmp2 + value [next (j)] * value [next (j)] * value [I];
If (max <result) max = result;
}
}
Printf ("% d \ n", max );
Return 0;
}

[Original] poj 1703 & rqnoj energy necklace solution report

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.