Pku21_square (also TLE .....................)

Source: Internet
Author: User

/*

Square
Time Limit: 3000 Ms memory limit: 65536 K
Total submissions: 7770 accepted: 2774

Description
Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?

Input
The first line of input contains N, the number of test cases. each test case begins with an integer 4 <= m <= 20, the number of sticks. M integers follow; each gives the length of a stick-an integer between 1 and 10,000.

Output
For each case, output a line containing "yes" if is possible to form a square; otherwise output "no ".

Sample Input

3
4 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 3 5

Sample output

Yes
No
Yes

Source
Waterloo local 2002.09.21 */

# Include <iostream>
# Include <vector>
# Include <algorithm>

Using namespace STD;

Class stick
{
Public:
Int value;
Int sign;
};

Bool CMP (stick a, stick B)
{
Return A. value> B. value;
}
Bool flag = false;
Int number = 0;

Void solution (vector <stick> sticks, int St, int long1, int long2)
{

If (ST <sticks. Size () & sticks [st]. Sign = 0)
{
If (long1> = sticks [st]. value)
{
Long1 = long1-sticks [st]. value;
Sticks [st]. Sign = 1;
If (long1 = 0)
{
Number ++;
If (number = 4)
{
Flag = true;
Return;
}
For (INT Ss = 0; SS <sticks. Size (); SS ++)
{
If (sticks [ss]. Sign = 0)
{
Solution (sticks, SS, long2, long2 );
Break;
}
}
}
Else
{
Solution (sticks, ++ St, long1, long2 );
Sticks [st-1]. Sign = 0;
Solution (sticks, ST + 2, long2, long2 );
}
}
Else
{
Solution (sticks, ++ St, long1, long2 );
}
}
Else if (ST <sticks. Size () & sticks [st]. Sign = 1)
{
Solution (sticks, ++ St, long1, long2 );

}
}

Int main (void)
{
Int testcase;
Cin> testcase;
Int number2;
Stick TMP;
Int sum = 0;
While (testcase> 0)
{
Testcase --;
Cin> number2;
Vector <stick> sticks;
For (INT I = 0; I <number2; I ++)
{
Cin> TMP. value;
TMP. Sign = 0;
Sum = sum + TMP. value;
Sticks. push_back (TMP );
}
Sort (sticks. Begin (), sticks. End (), CMP );
If (sum % 4 = 0 & (* (sticks. Begin (). value <= sum/4)
{
Solution (sticks, 0, sum/4, sum/4 );

}
If (flag = true)
{
Cout <"yes" <Endl;
}
Else
{
Cout <"no" <Endl;
}
Sum = 0;
Flag = false;
Number = 0;
}

Return 0;
}

Almost the same as 1011 .. It seems simple ......... But it's actually tle ............

We recommend that you use less vector and less STL for the questions submitted to OJ later...

Contact Us

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.