Programming Contest series (a) C / C ++ exhaustion algorithm (search violence) and binary search

I believe many people have a willingness to want to get medals in the ACM-ICPC, GCJ, TopCoder at the university level, but the market a lot of uneven course, no guarantee of quality and the price, there are many ridiculously expensive. I hope my article can help you learn to save costs, and provide a higher quality of learning pathways. The title of this series of articles are selected from a number of well-known algorithm books, there is no original topic. But I will be better understood by way of, you try to explain it difficult to understand the algorithm becomes easier to understand. This is my goal, and everyone is willing to learn together, and common progress.

Here Insert Picture Description

Programming Contest is probably introduced

Type Programming Contest There are many, we have had to fight the problem-solving performance. It can be said variety, we are talking about is the content of programming so the speaker is problem solving. Such competition does, before starting players will know the number of topics, and their task is, for a limited time as much as possible (of course, is correct and satisfy the conditions necessary) to complete these questions.

This article is the first article Programming Contest introductory article series, we will introduce two algorithms today: Exhaustion algorithms and binary search

Exhaustion Algorithms

Exhaustion Algorithms Advantages and Disadvantages

Exhaustion algorithm , is an algorithm used by a lot of people, it has the advantage of simplicity, but it also has huge drawbacks. That is its complexity. Its complexity is directly related to the number of cycles to its exponential relationship . Let n be the number of times per a cycle, m is the number of its cycles. Then its complexity is O (the n-^ m) , which means that if you do a quadruple loop then your complexity is already a scary figure. If you have a five-fold or more, you have to face the basic overtime.
Of course, in some places this algorithm is still very useful, here we will introduce a topic to describe it further

Topic: Getting triangle, the classical algorithm topicHere Insert Picture Description

For this we can analyze this problem, three sticks optionally determines whether they meet conditions of a triangle composed of any two sides is greater than the sum of the third side , satisfied if then we use some means to record it, and when there is a larger circumference when the triangle, our algorithm can be independent of the larger triangle perimeter perimeter before replacing recorded off the perimeter. So we will be able to have a rough idea.

  1. Determining whether the triangle can be composed.
  2. If so, the triangle is put around its perimeter than the size, it retains a large and may be determined by the composition and the next three triangles stick perimeter than the perimeter of the triangle before. And that a longer retention again.
  3. The final output is retained triangle.
    Here we use algorithms to represent the process of it.
int n, a[Max];
void solve(){
	int ans = 0;  //答案

	//让i<j<k, 这样棍子就不会被重复选中了
	for(int i = 0; i < n; i++){
		for(int j = i +1; j <n; j++){
			for(int k= j + 1; k< n ; k++){
			int  len = a[i] + a[j] + a[k];               //周长
			int ma = max(a[i] , max(a[j], a[k]));//最长棍子的长度
			int rest = len - ma;
		if(ma < rest){
		//  可以组成三角形,如果可以更新答案则更新
		ans = max(ans, len);
			 }
 		}
 	}
printf("%d\n", ans);
}

	

Let's explain this algorithm: First, we assign the ans , if the group is not a triangle, then the value is 0 ans will , if you can make up a triangle, then ans value will be the largest triangular perimeter . Then enter our focus on the triple loop for some friends to remember some vague concept of grammar, see here will feel a bit strange, is it not the first time the algorithm runs i = 0, j = 1, c = 2, the second runs i = 1, j = 2, c = 3 and so do . In fact, not of . Because our priorities for loop is to start the outer loop into the loop , and then execute all loops finish in the loop and back outside the loop. So in fact, all of the stick all the possible combinations are related to. Again analyzes the contents of the innermost loop. First we let the length of three stick sum assigned to len, len is actually perimeter (if you can make up a triangle, then) And then let us stick with the three, the longest length of assignment to stick ma, let the rest and the length of the two sticks assigned to rest, as long as we compare ma is less than the rest it can be determined whether the composition triangle. If you can, we put the greatest perimeter assigned ans , and then the cycle continues until the implementation of complete cycle of all , because the back so if there is a longer perimeter of the triangle will replace the original ans value and re-assigned to ans. Then we completed the largest circumference of the output task

You talked about here compared to the Exhaustion algorithm have a good understanding.

Here Insert Picture Description

Binary search

In view of the sky too late, bloggers will complete the update binary search before 3.10 pm . Good night, sweet dreams!

Released eight original articles · won praise 41 · views 4682

Guess you like

Origin blog.csdn.net/weixin_45950372/article/details/104743305