Title:
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3, then caught in a deep boredom. But he thought of a game to make him boring. He took out N wooden sticks, then selected some of these one of them, and then chose some of the other one. He wanted to know how long the length was in the same long situation.
Input format:The first line of nage nn, indicating n sticks. The second row n is n. Each number represents the length of a stick.
output format:A number, the maximum length.
Sample input: 4(Enter)
1(space)2(space)3(space)1(Enter))
Sample output: 3
Data scale and agreement: n <= 15
Foreword:
The above article is my analysis of the Boring Drough of the Blue Bridge Cup algorithm, but there is a problem, but it happened to pass the test sample of the official website, so I did not discover it in time.
After some netizens’ reminders, I found that I had a loophole in solving the law, but during that time, I was busy. I only responded to the netizens occasionally, did not modify the article in time, and then forgot. Essence Essence
(2022.03.25) Suddenly when I was in class today, after derivating on the paper, all the situation can be solved. Write another article to explain.
As for the article that had loopholes before, I also talked about DFS thoughts at the least.
Start: (Readers have already understood the topic)
For this question, all we have to do isA pile of wooden sticksDivided intoThe same two piles. The focus is on how to divide it?
According to the previous articleDFSThinking, you can do all the situation. But there is still a problem:can be divided into two piles anyway.
For example:(1 1 2 3), and then what I think is:Remove the shortest wooden stick, so that the wood stick is the same as the even number, “it can be equally divided”.
but:
When(1 2 3 4 7), you have to remove the smallest puppet stick1 2.
Also:(3 4 5 7)Directly remove the middle size5。
There seems to be no regular. Essence Essence
At this time, we reviewed the DFS operation of the previous article. For a wooden stick, in addition to choosing/not selected, there should be a “abandon” option.
But the idea of the article was directly thinking that the wooden sticks were treated directly outside the DFS.
Now add an extra choice in DFS.
Still, for example, for (1 1 2 3). We followSelected/not selectedTwo options to operate. Will
The result will be: 0. That is: it cannot be divided into two piles anyway. But if we improve it, add a choice: “Give up”. The situation will be different.
Of course, there is no example, and the remaining results are obvious. I do n’t give examples one by one, mainly to show the situation of different branches.
At this time, the result obtained: 3, that is, the correct result.
As shown in the picture above, return to the yellow arrow, and find that there have been“Abandon”
The choice ofis to abandon one1, to achieve average divided into two piles.
continues:
There are also examples:3 4 5 7
Wooden stick to be removed 5is not before, the improved DFS can still be enumerated by itThe possibility of.
I do n’t show it one by one (I want to steal laziness), mainly because the students can write more deeply to understand the process by themselves.
Summary:
In summary, by adding a choice to DFS (double branches of three branches), all wooden sticks can be combined. (No need to do other operations: removing the wooden stick or something, of course, the general optimization is okay, for example: the longest equal to the total is half …)
DFS implementation:Parameters (array, array length, array element setting, left pile, right pile)
Left pile: initially the sum of all wooden sticks
Right pile: initial 0
Through DFS,Left piles are constantly decreasing, and the right piles are continuously increased。
equal toWhen the right dove, the end,comparison/update results。
Dangzhengless thanRight pile,pruning. (Later DFS is meaningless)
Attach code:(more concise)
#include <iostream>
using namespace std;
#include <algorithm>
// Record results (constantly refresh)
int Maxsum = 0;
// Deep search, array, length, lowering, (total) left, right pile
void DFS (int ii [], int n, int x, int left, int right = 0)
{{
// When satisfying the two piles is equal and the result can be updated
if (left == Right && Maxsum <Left)
{{
Maxsum = left;
Return;
}
// DFS pruning (less than the right heget) or the bidding border
if (left <right || x> = n)
{{
Return;
}
// Normal DFS
else
{{
DFS (II, N, X + 1, Left, Right); // Do not choose
DFS (II, N, X + 1, LEFT -II [X], Right + II [X]); // Select
DFS (II, N, X + 1, LEFT -II [X], Right); // Give up
Return;
}
}
int Main ()
{{
int add = 0;
int ii [15] = {0};
int n; cin >> n;
// Entry data and calculations summarize
for (int i = 0; i <n; i ++)
{{
cin >> ii [i];
add += ii [i];
}
// Sorting: Promotion
sort (II, II + N, Greater <ent> ());
// DFS
DFS (II, N, 0, ADD);
// Output results
COUT << Maxsum;
Return 0;
}
end:
Hope to inspire readers/classmates.
Turbus.
PS: This article cooperates with the previous article “consumption” is better.