"2018.2.26 algorithm summary # Divide and conquer"

Source: Internet
Author: User
Tags ticket

Divide and Conquer law : 1. Divide a problem into many small problems and solve small problems. 2. Regroup small problems into one problem.

Example:

    1. Queue to buy Tickets

Problem Description: Ticket sales are on the tight front before a game starts. Each ticket is 50 yuan, there are m+n individuals lined up to buy tickets, including m personal hand-held 50 yuan banknotes, the other n individuals holding 100 yuan of banknotes. Find this m+n individual queue to buy tickets, so that the ticket office does not appear to find the situation of the different queuing species. (Agreed: The ticket office to start the ticket no change, take the same denomination of banknotes to the same position for the same line.) )

Problem Analysis:

Make F (m,n) said that there is m personal hand-held 50 yuan banknotes, n individuals holding 100 yuan banknotes when the total number of exclusions. discussed in the following 3 scenarios.

    • N=0: It means that all the people who are queuing up for the tickets are 50 yuan in money, and notice that the person who takes the same denomination is in the same line, so the total number of this m-person queue is 1, i.e. f (m,0) = 1.
    • M<n: When the m<n, that is, the number of people who buy a ticket is less than 100 yuan of the bill, even if the M-sheet of 50 yuan to find out, will still appear to find no money situation, when the queue total is 0, that is, f (m,n) = 0.
    • In other cases, M+n personal hand-held 100 yuan banknotes, in his previous m+n-1 individuals have m individuals holding 50 yuan of banknotes, there are n-1 individuals holding 100 yuan of banknotes, this situation is a total of f (m,n-1). M+n personal Hand-held 50 yuan banknotes, then in his previous m+n-1 individuals have m-1 personal hand-held 50 yuan banknotes, there are n individuals holding 100 yuan of banknotes, this situation is a total of f (m-1,n).

The recursive relationship of F (m,n) is obtained by the addition principle: f (m,n) =f (m,n-1) +f (m-1,n)

Initial conditions: When M<n, F (m,n) =0 when n=0, F (m,n) =1

Long f (int j,int i) {   long y;   if (i==0)      Y=1;   else if (j<i)      y=0;       Determine the initial condition     else      y=f (j-1,i) +f (j,i-1);  Implement recursive     return (y);}

2. The trouble of the lake without name

Problem description

every winter, Peking University is a good place for skating on the lake. Peking University sports team prepared a lot of skates, but too many people, every afternoon after the day, often a pair of skates are not left.
every morning, rental shoes window will be lined up long, fake with shoes of M, there is need to rent shoes n. The question now is how many of these people have a way to avoid embarrassing situations where the sports Group has no skates to rent. (Two people with the same needs (such as renting shoes or all shoes) Exchange position is the same row method)Input Formattwo integers representing M and noutput FormatAn integer that represents the number of schemes for the troop's platoon. Sample Input3 2Sample Output5data size and conventionsm,n∈[0,18]

Analysis: As in the first example, consider the initial two conditions before considering the situation of the last person.

1#include <iostream>2#include <stdio.h>3 using namespacestd;4 intFintMintN) {5     inty;6     if(n==0)//no one borrowed the shoes.7y=1;8     Else if(m<n)//the number of people who borrow shoes9y=0;Ten     Else  OneY=f (M-1, N) +f (m,n-1);//How many people can withdraw their shoes before the last one? A     returny; - }  - intMain () { the     intM,n; -Cin>>m; -Cin>>N; -Cout<<f (m,n) <<Endl; +      -     return 0; +}
View Code

"2018.2.26 algorithm summary # Divide and conquer"

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.