Question:
Given n sides, each side has a certain length. Now we need to enclose all sides in a triangle and ask how many different triangles can be formed (the three sides have at least one side ).
Analysis:
Number A, B, and C sides of a triangle. All the edges must be on one of them. You only need to compress the position of each edge in triplicate, or directly
DFS enumeration.
In addition, I noticed that the first wooden stick must be on one side of the triangle. Therefore, based on the symmetric relationship, you can directly place it on the side of side.
View code
# Include <cstdio> # Include <Cstring> # Include < Set > # Include <Algorithm> Using Namespace STD; Const Int X =16 ; Int A [X], n, ANS, sum; Int B [ 5 ]; Set <Pair < Int , Int > Ma; Void DFS ( Int POs, Int Suma, Int Sumb,Int Sumc ){ If (Pos = N ){ // This is to determine whether the conditions are met. Sort the three sides and determine whether a triangle can be formed. If (SUMA < Sumb) {B [ 0 ] = SUMA; B [ 1 ] = Sumb ;} Else {B [ 0 ] =Sumb; B [ 1 ] = SUMA ;} If (B [ 0 ]> Sumc) B [ 0 ] = Sumc; If (B [ 1 ] < Sumc) B [ 1 ] = Sumc; B [ 2 ] = Sum-B [0 ]-B [ 1 ]; Swap (B [ 2 ], B [ 1 ]); If (B [ 0 ] + B [ 1 ] <= B [ 2 ]) Return ; If (B [ 2 ]-B [0 ]> = B [ 1 ]) Return ; If (Ma. Find (make_pair (B [ 0 ], B [ 1 ])! = Ma. End ()) Return ; Ans ++ ; MA. insert (make_pair (B [ 0 ], B [ 1 ]); Return ;} DFS (Pos + 1 , Suma + A [POS], sumb, sumc); DFS (Pos + 1 , Suma, sumb + A [POS], sumc); DFS (Pos + 1 , Suma, sumb, sumc + A [POS]);} Int Main () {freopen ( " Sum. In " ," R " , Stdin ); Int Ncase; scanf ( " % D " ,& Ncase ); While (Ncase -- ) {Ma. Clear (); sum = 0 ; Scanf ( " % D " ,& N ); For ( Int I = 0 ; I <n; I ++ ) {Scanf ( " % D " ,& A [I]); sum + = A [I];} ans = 0 ; DFS ( 1 , [0 ], 0 , 0 ); Printf ( " % D \ n " , ANS );} Return 0 ;}
Question about semi-finals 1006
I may have changed the data.ProgramWa dropped =. Sorry for the inconvenience.-I have deleted this blog...
UPDATE: The question 1006 should be a question. In the competition, I was right with this program, but it was wrong last night, I changed a place I <6 to I <8 just now (or the judgment for directly deleting the condition seems to be correct). It seems that the question is not clear, sorry ~~