Luo2383: Dog brother playing with the stick

Source: Internet
Author: User

Problem topic background

Dog brother also take advantage of Chinese class to do some boring things ...

Title Description

Now give some of the length of the stick, then the dog brother can be used to give the wooden stick (all the wood used) to form a square?

Input output Format input format:

The first line in the input file is an integer n representing the number of groups tested, and the next n lines represent the test data for each group. The first number of each row is M (4<=m<=20), and the next m-number AI (1<=ai<=1000) represents the length of the stick.

Output format:

For each set of test data, if you can make a square output "yes", output "no".

Input and Output Sample input example # #:
34 1 1 1 1 5 10 20 30 40 50 8 1 7 2 6 4 4 3 5
Sample # # of output:
yesnoyes
Solution

Hehe, this is actually a problem, in fact, is a violent search problem, I believe we will write.

However, it can be rated because the sheer search is not over the problem, need to add some pruning .

This is my submission record, you can see, from the beginning TLE to the AC time gap is huge, even AC the same code also has 5 times times the gap. This is all caused by the pruning operation.

Let's talk about how TLE it came from AC , this is the original version of my search:

bool dfs(int t, int l1, int l2, int l3, int l4){ //t 表示当前到第几个棍子了,l1,l2,l3,l4分别是正方形的四条边的长度,返回值表示是否可行    if (t == n + 1){return (l1 == l2 && l2 == l3 && l3 == l4);}    if (dfs(t+1, l1 + a[t], l2, l3, l4)) return true;    if (dfs(t+1, l1, l2 + a[t], l3, l4)) return true;    if (dfs(t+1, l1, l2, l3 + a[t], l4)) return true;    if (dfs(t+1, l1, l2, l3, l4 + a[t])) return true;    return false;}

This search has not been optimized and we need to consider the motivation for optimization. We found that there are some states that are not necessary to enumerate down , for example, if \ (sum = \sigma a_i\),\ (sum\) is not a multiple of 4, then obviously cannot be spelled as a square; l1,l2,l3,l4 A number greater than \ (sum \over 4\), clearly should be returned false , there is no need to enumerate down.

With these two optimizations, the code is on AC the ha.

Can we further optimize it?
Think about that pruning, because pruning must be as early as possible, and if we put the stick in descending order of length, the pruning time will certainly be ahead. This optimization can optimize the time to the original \ (1 \over 5\).
As for my one CE , it was written in descending order sort(a+1, a+1+n, greater<int>() ) sort(a+1, a+1+n, greater<int> ) , so I wrote the wrong qaq before?

Luo2383: Dog brother playing with the stick

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.