Home Backend Development C#.Net Tutorial C++ greedy algorithm (venue arrangement, interval selection) example

C++ greedy algorithm (venue arrangement, interval selection) example

Nov 29, 2019 pm 04:10 PM
greedy algorithm

The first recording after learning the algorithm course. Gradually, the factors to be considered in program design increased. Program = data structure algorithm. This equation made me deeply understand. From starting with simple C programming to selecting appropriate data structures, now we need to go one step further and consider the efficiency of program execution from the algorithm level. My understanding of the algorithm is to achieve better execution results with less overhead.

C++ greedy algorithm (venue arrangement, interval selection) example

The divide and conquer method and dynamic programming have not been recorded before. When I learned the greedy algorithm, I felt that I needed to summarize what I had learned so that I could understand it better. . The design of dynamic programming must satisfy the optimal substructure properties and overlapping subproblems, and adopt a bottom-up strategy to calculate the optimal value and find the overall optimal solution. This process is sometimes quite difficult. The main thing is to write the recursive expression and fill in the table from the bottom up. The greedy strategy is a bit like dynamic programming, but it is different in some aspects. Sometimes the idea of ​​​​a greedy algorithm is easier to think of. Does it satisfy the sub-problem optimality and obtain the overall optimality? Two conditions: optimal substructure property and greedy selection property. Satisfying the greedy selection property must satisfy the optimal substructure property, while satisfying the optimal substructure property does not necessarily satisfy the greedy selection property. For example, the knapsack problem can be solved with a greedy algorithm, while the 0-1 knapsack problem can only be solved with dynamic programming.

Typical greedy problem activity arrangement, there are n activities, the start time and end time are given, and as many activities as possible should be arranged (the time does not conflict with each other). The correct greedy idea to solve this problem is to use the end time of each activity as a comparison variable, sort the activities in ascending order of the end time, and then perform comparison and selection. There are some differences between venue arrangement issues and activities. The following is my problem-solving process.

7-2 Venue Arrangement Issues (20 points)

Suppose you want to arrange a batch of activities in enough venues and want to use as few venues as possible. Design an efficient greedy algorithm for scheduling. (This problem is actually the famous graph coloring problem. If each activity is regarded as a vertex of the graph, incompatible activities are connected by edges. The minimum number of colorings that make adjacent vertices have different colors corresponds to the Minimum number of venues.)

Input format:

The first line contains a positive integer k, indicating that there are k activities to be arranged. In the next k lines, each line has 2 positive integers, representing the start time and end time of k to-be-scheduled activities respectively. Time is measured in minutes starting at 0 o'clock.

Output format:

Output the minimum number of venues.

Input sample:

5
1 23
12 28
25 35
27 80
36 50

Output sample:

3
#include<iostream>
#include<algorithm>
using namespace std;
struct node {
    int begin;
    int end;
    int flag;//标记该活动是否被安排,0表示未安排,1表示已安排 
}t[10001];
int cmp(const node &a,const node &b)//比较规则:以结束时间升序排列 
{ 
    return a.end<b.end;
 } 
int main()
{
    int i,j,n;
    node temp;
    cin>>n;
    for(i=0;i<n;i++) 
    {
        cin>>t[i].begin>>t[i].end;
        t[i].flag=0;
    }
    sort(t,t+n,cmp);
        
    int sum=0;//总共需要的会场数量 
    for(i=0;i<n;i++)//方法2 
    {
        if(!t[i].flag)//找到未安排的活动,进行场地安排 
        {
            sum++;
            int p=i;
            for(j=p+1;j<n;j++)//当前活动结束时间与下一个活动开始不相交 ,则安排到同一个会场 
            {
                if(t[p].end<=t[j].begin&&!t[j].flag)
                {
                    p=j;t[j].flag=1;
                }
            }
            t[i].flag=1;
        }
    }
    cout<<sum;
    return 0;
}

The greedy strategy is: arrange as many non-conflicting activities as possible into one venue. If the activity If the time overlaps, it will be arranged to another venue.

Arrange all activities in ascending order by end time, use the sort function, and customize the cmp method. In the loop body, you can find an unscheduled activity each time, and use this activity to search for other activities that can be accommodated in a venue at the same time (this step is nested in the inner loop). After two layers of loops, all activities are arranged. Okay, now the required number of venues, sum, has been calculated.

A similar problem is interval point selection

7-10 point selection problem (15 points)

There are n closed intervals on the number axis [ai , bi]. Take as few points as possible so that there is at least one point in each interval (the points in different intervals can be the same).

Input format:

A number n in the first line means there are n closed intervals. The following n lines, each line contains 2 numbers, indicating the closed interval [ai, bi]

Output format:

An integer, indicating at least how many points are needed

Input sample:

Give a set of input here. For example:

3
1 3
2 4
5 6

Output sample:

The corresponding output is given here. For example: 2

initially wants to find common segments among several intervals, and record which intervals are included in each common segment, so as to calculate the minimum selected points. Later I found that this idea can actually be simplified. The strategy is: use the right end as the baffle to see if there are other intervals in front of it. If so, do not count. Otherwise, it means that there are no common segments. You need to count and move the baffle position to continue. Look for. The greedy strategy is to select the right endpoint of the interval to ensure that it can contain a larger intersection segment and select the fewest points.

#include<bits/stdc++.h>
using namespace std;
struct dot{
    int l,r;
    bool v[10001];
}dots[10001];
int cmp(const dot &a,const dot &b)//比较规则,按区间右端点升序排列 
{
    return a.r<b.r;
} 
int main()
{
    int n,i,j,count=1,select;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>dots[i].l>>dots[i].r;
    sort(dots,dots+n,cmp);//预处理,将区间按规则排好序,方便后续比较 
    select=dots[0].r;
    //贪心策略是选择区间右端点,保证能够包含更大交叉段,选的点最少 
    for(i=1;i<n;i++)//每次将当前选择的一个区间的右端点与下一个(或者同一区间,可忽略)左端比较 
    {
        if(dots[i].l>select)//如果没有交叉,选点+1,并以此区间右端为新一轮比较的点 
        {
            count++;
            select=dots[i].r;
        }
    }
    cout<<count;
    return 0;
}

After learning the algorithm, I found that solving problems requires a change in thinking. The choice of algorithm before programming is very important. I also need to learn from the big guys. The study and research of typical algorithms is really broad and profound!

This article comes from the C#.Net Tutorial column, welcome to learn!

The above is the detailed content of C++ greedy algorithm (venue arrangement, interval selection) example. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1580
276
How to implement greedy algorithm in C# How to implement greedy algorithm in C# Sep 19, 2023 am 11:48 AM

How to implement the greedy algorithm in C# The greedy algorithm (Greedy algorithm) is a commonly used problem-solving method. It selects the current optimal solution every time in the hope of obtaining the global optimal solution. In C#, we can use greedy algorithms to solve many practical problems. This article will introduce how to implement the greedy algorithm in C# and provide specific code examples. 1. Basic principles of greedy algorithm The basic idea of ​​greedy algorithm is to choose the current optimal solution every time, regardless of the possible impact of subsequent steps. This kind of thinking

How to implement an efficient solution to the least coin change problem in PHP using the greedy algorithm? How to implement an efficient solution to the least coin change problem in PHP using the greedy algorithm? Sep 19, 2023 am 10:22 AM

How to implement an efficient solution to the least coin change problem in PHP using the greedy algorithm? Introduction: In daily life, we often need to make change, especially when shopping or trading. To use as few coins as possible, the change amount should be combined using as few coins as possible. In computer programming, we can use a greedy algorithm to solve this problem to get an efficient solution. This article will introduce how to use the greedy algorithm in PHP to achieve an efficient solution to the minimum coin change problem, and provide corresponding code examples.

Parse the Ford-Fulkerson algorithm and implement it through Python Parse the Ford-Fulkerson algorithm and implement it through Python Jan 22, 2024 pm 08:09 PM

The Ford-Fulkerson algorithm is a greedy algorithm used to calculate the maximum flow rate in a network. The principle is to find an augmenting path with a positive remaining capacity. As long as the augmenting path is found, you can continue to add paths and calculate traffic. Until the augmenting path no longer exists, the maximum flow rate can be obtained. The term remaining capacity of the Ford-Fulkerson algorithm is to subtract the flow from the capacity. In the Ford-Fulkerson algorithm, the remaining capacity is a positive number before it can continue to be used as a path. Residual network: It is a network with the same vertices and edges, using residual capacity as capacity. Augmented path: It is the path from the source point to the receiving point in the residual graph, with a final capacity of 0. A possible outline of the Ford-Fulkerson algorithm principle example

How to implement greedy algorithm using Python? How to implement greedy algorithm using Python? Sep 19, 2023 am 11:43 AM

How to implement greedy algorithm using Python? Greedy Algorithm is a simple and effective algorithm suitable for solving problems with optimal substructure properties. It takes the best choice in the current state in each step of selection, hoping to find the global optimal solution. In this article, we will introduce how to use Python to implement the greedy algorithm, with specific code examples. 1. The basic idea of ​​the greedy algorithm The basic idea of ​​the greedy algorithm is to select the optimal solution in the current state at each step, and then

How to write a greedy algorithm using PHP How to write a greedy algorithm using PHP Jul 07, 2023 pm 03:45 PM

How to use PHP to write a greedy algorithm Greedy algorithm (Greedy algorithm) is a simple and effective algorithm used to solve a type of optimization problem. Its basic idea is to make the choice at each step that seems best at the moment, without regard to future consequences. This article will introduce how to write a greedy algorithm using PHP and provide relevant code examples. 1. Problem Description Before explaining the greedy algorithm, let us first define a specific problem for a better understanding. Suppose there is a set of tasks, each task has a start

How to implement greedy algorithm using java How to implement greedy algorithm using java Sep 19, 2023 am 11:13 AM

How to use Java to implement greedy algorithm Greedy algorithm (GreedyAlgorithm) is an algorithmic idea for solving problems. Its characteristic is to select the current optimal solution at each step, hoping to eventually reach the global optimal solution through each local optimal solution. The simple and efficient characteristics of the greedy algorithm make it a commonly used algorithm when solving some optimization problems or certain specific problems. This article will introduce how to implement the greedy algorithm using Java and provide specific code examples. 1. The basic idea of ​​greedy algorithm The basis of greedy algorithm

Greedy algorithm and its implementation in C++ Greedy algorithm and its implementation in C++ Aug 22, 2023 am 10:04 AM

The greedy algorithm is a commonly used algorithm idea and is widely used in many problems. The core idea is to only consider the immediate optimal solution when making a decision at each step, without considering the long-term impact. In C++, the implementation of greedy algorithms often involves basic operations such as sorting and data processing. Below, we will introduce the idea of ​​greedy algorithm and its implementation in C++ for several typical problems. 1. Activity Scheduling Problem Given a set of activities, each activity has its start time and end time, and a person can only participate in one activity at a time.

C/C++ program for greedy algorithm to find the minimum number of coins C/C++ program for greedy algorithm to find the minimum number of coins Sep 19, 2023 pm 11:01 PM

The greedy algorithm is an algorithm used to find the optimal solution to a given problem. The greedy algorithm works by finding a local optimal solution for each part (the optimal solution to one part of the problem), thus showing that a global optimal solution can be found. In this problem, we will use the Greedy Algorithm algorithm to find the minimum number of coins/notes that can make up a given sum. For this we will consider all valid coins or banknotes, i.e. denominations {1,2,5,10,20,50,100,200,500,2000}. We need to return the number of coins/notes needed to make up the sum. Let us give a few examples to understand the context better - Example 1 - Input: 1231 Output: 7 Description - We need two 500 rupee notes

See all articles