NY 325 ZB's birthday

Source: Internet
Author: User

Assume that all the watermelons are loaded with asum, And the request is packed with asum/2 backbacks.

At the beginning, we started to use greedy, WA. Later, I used a 01 backpack. As a result, TLE had too much data. I used deep search!

DFS (INT sum, int I) indicates that sum is installed, and the number I is determined.

It took more than 1200 ms, and I don't know how big cows made it in 60 ms. It's 20 times that of coal! Optimize it later.

The Code is as follows:

# Include <iostream> # include <cstdio> # include <math. h ># include <algorithm> using namespace STD; int A [25], asum, mi; void DFS (INT sum, int I) {if (I <0) // After the watermelon has been installed, return; int T = FABS (asum-2 * sum); // The weight difference between the two siblings if (T <mi) mi = T; If (2 * sum-asum> = mi) // pruning, the weight difference is greater than or equal to the minimum value, and the search is greater than or equal to return; DFS (sum + A [I], I-1); // installs the I-th DFS (sum, I-1); // does not hold the I-th} int main () {int N; while (scanf ("% d", & N )! = EOF) {asum = 0; MI = 99999999; For (INT I = 0; I <n; I ++) {scanf ("% d ", & A [I]); asum + = A [I];} DFS (0, n-1); // never load anything, start cout For The Last watermelon decision <mi <Endl;} return 0 ;}
View code

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.