Greedy Special Training 2

1: Venue arrangement issues

Author Chen Xiaomei
Unit Guangdong University of Foreign Studies
Title source: Wang Xiaodong "Algorithm Design and Analysis"

Suppose you want to schedule a batch of events 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 1 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 example:

5
1 23
12 28
25 35
27 80
36 50 

Output sample:
The corresponding output is given here. For example:

3

Code length limit
16 KB
Time limit
400 ms
Memory limit
64 MB

Analysis: We only need to use the greedy algorithm to find the optimal solution for each traversal. If we perform it at most n times, we will be able to traverse all the times. However, an error will occur if we sort by the end time. We must sort by the start time. The reason is unknown.

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010;

int vis[N];

struct node
{
    
    
    int x, y;
    bool operator < (const node &a)
    {
    
    
        return x < a.x;
    }
}f[N];

int main()
{
    
    
    int n;
    cin >> n;
    for (int i = 0; i < n; i ++ ) cin >> f[i].x >> f[i].y;
    
    sort(f, f + n);
    
    int cnt = 0;
    for (int i = 0; i < n; i ++ )
    {
    
    
        if(!vis[f[i].y])
        {
    
    
            cnt ++;
            vis[f[i].y] = 1;
            int end = f[i].y;
            for(int j = i + 1; j < n; j ++ )
            {
    
    
                if(end <= f[j].x && !vis[f[j].y])
                {
    
    
                    end = f[j].y;
                    vis[f[j].y] = 1;
                }
            }
        }
    }
    cout << cnt;
    
    return 0;
}

2: h0145. Meeting arrangements

Author Huang Zhengpeng
Unit: Guizhou Institute of Engineering Technology

There are many activities in the school auditorium every day. Sometimes the planned schedules of these activities may conflict, and some activities need to be selected to be held. Xiao Liu's job is to arrange activities in the school auditorium, and he can arrange at most one activity at a time. Now Xiao Liu has some activity plans. He wants to arrange as many activities as possible. How should he arrange them?

Input format:
The first line is an integer m (m<100) indicating a total of m sets of test data.
The first line of each set of test data is an integer n (1<n<10000), indicating that there are n activities in the test data.
The following n lines, each line has two positive integers Bi, Ei (0<=Bi,Ei<10000), which respectively represent the start and end time of the i-th activity (Bi<=Ei)

Output format:
For each set of inputs, output the maximum number of activities that can be arranged.
The output of each group occupies one line

Input sample:
Give a set of input here. For example:

2
2
1 10
10 11
3
1 10
9 11
11 20

Output sample:
The corresponding output is given here. For example:

2
2

Code length limit
16 KB
Time limit
400 ms
Memory limit
64 MB

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010;

struct node
{
    
    
    int x, y;
    bool operator < (const node &a)
    {
    
    
        return y < a.y;
    }
}f[N];

int main()
{
    
    
    int T;
    cin >> T;
    while (T -- )
    {
    
    
        int n;
        cin >> n;
        for (int i = 0; i < n; i ++ ) cin >> f[i].x >> f[i].y;

        sort(f,f + n);

        int end = f[0].y;
        int cnt = 1;
        for (int i = 0; i < n - 1; i ++ )
        {
    
    
            if(end <= f[i + 1].x)
            {
    
    
                end = f[i + 1].y;
                cnt ++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

3: Minimum no-show

Author usx Programming Course Group
Unit Shaoxing University of Arts and Sciences

One day, Nono had many activities to attend. But due to too many activities, Nono was unable to participate in all the activities. Please help Nono make arrangements so that he can participate in as many activities as possible and reduce the number of missed appointments. Assumption: You can immediately participate in another activity as soon as one activity ends.

Input format:
First enter an integer T, indicating the number of groups of test data, and then T groups of test data. Each set of test data first inputs a positive integer n, representing the total number of activities that need to be participated in that day, and then enters n lines. Each line contains two integers i and j (0≤i<j<24), which respectively represent the start and end time of an activity. .

Output format:
For each set of tests, output the minimum total number of missed appointments on one line.

Input example:

1
5
1 4
3 5
3 8
5 9
12 14

Output sample:

2

Code length limit
16 KB
Time limit
400 ms
Memory limit
64 MB

Analysis: We only need to use the greedy strategy to find the optimal solution, and then subtract the optimal solution from the total number of activities to get the minimum default strategy

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 110;

struct node
{
    
    
    int x, y;
    bool operator < (const node &a)
    {
    
    
        return y < a.y;
    }
}f[N];

int main()
{
    
    
    int T;
    cin >> T;
    while (T -- )
    {
    
    
        int n;
        cin >> n;
        for (int i = 0; i < n; i ++ ) cin >> f[i].x >> f[i].y;

        sort(f,f + n);

        int end = f[0].y;
        int cnt = 1;
        for (int i = 0; i < n - 1; i ++ )
        {
    
    
            if(end <= f[i + 1].x)
            {
    
    
                end = f[i + 1].y;
                cnt ++;
            }
        }
        cout << n - cnt << endl;
    }
    return 0;
}

4: Activity selection problem

Author Li Tingyuan
Unit Civil Aviation Flight University of China

Assume a set S = {a 1 , a 2 ,..., a n } with n activities . These activities use the same resource (such as the same lecture theater), and this resource can only be used by Used by an activity. Each activity a i has a start time s i and an end time f i , where 0<=s i <f i <=32767. If selected, task a i occurs during the half-open time interval [s i , fi ) . Two activities a i and a j are said to be compatible if they satisfy [s i ,f i ) and [s j , f j ) without overlapping. That is to say, if s i >=f j or s j >=f i , then a i and a jare compatible. In the activity selection problem, we wish to select a maximal set of compatible activities.

Input format:
The first line is an integer n (n≤1000);

The next n lines have two integers in each line, the first is s i and the second is f i (0<=s i <f i <=32767).

Output format:
Output the maximum number of activities that can be arranged.

Input example:

11
3 5
1 4
12 14
8 12
0 6
8 11
6 10
5 7
3 8
5 9
2 13

Output sample:

4

Sample explanation:
The four scheduled activities are 1 4, 5 7, 8 11 and 12 14.

Code length limit
16 KB
Time limit
400 ms
Memory limit
64 MB

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1010;

struct node
{
    
    
    int x, y;
    bool operator < (const node &a)
    {
    
    
        return y < a.y;
    }
}f[N];

int main()
{
    
    
    int n;
    while (cin >> n)
    {
    
    
        for (int i = 0; i < n; i ++ ) cin >> f[i].x >> f[i].y;

        sort(f,f + n);

        int end = f[0].y;
        int cnt = 1;
        for (int i = 0; i < n - 1; i ++ )
        {
    
    
            if(end <= f[i + 1].x)
            {
    
    
                end = f[i + 1].y;
                cnt ++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

5: Deletion problem

Author Ren Wei
Affiliated with Hebei Agricultural University

There is a positive integer of length n (n <= 240). Take k (k < n) numbers from it and keep the remaining numbers in their original order. Find the minimum number of this positive integer after deletion.

Input format:
n and k

Output format:
a number representing the minimum value of this positive integer after deletion.

Input example:

178543 4

Output sample:

13

Code length limit
16 KB
Time limit
400 ms
Memory limit
64 MB

Analysis: According to the greedy strategy, as long as the optimal solution is chosen every time, the final result must be the optimal solution. The strategy that I just thought of when I started this question is to delete the largest number each time, and the remaining sequence must be the optimal solution. By The proof by contradiction method shows that if there is a string of zeros in the middle of a number, an error will occur. For example: 30008 1. The optimal solution should be 8. So after thinking about it later, I found that as long as the number deleted each time is larger than the next number, the optimal solution can be obtained.

#include <iostream>

using namespace std;

int vis[250];

int main()
{
    
    
    string s;
    int n;
    cin >> s >> n;
    
    for (int i = 0; i < n; i ++ )
    {
    
    
        for (int j = 0; j < s.size(); j ++ )
            if(s[j] > s[j + 1] || j == s.size() - 1)
            {
    
    
                s.erase(s.begin() + j);
                break;
            }
    }
    while (s[0] == '0') s.erase(s.begin()); //  删除前导零
    
    cout << s;
    
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_52331221/article/details/127811420