Greedy training topic 1

1: Swan College Club Recruitment

Author liaoning
unit College of Foreign Affairs, Central South University of Forestry and Technology

The Swan College club recruits new students, and the new recruitment seminars are scattered at different time periods. Xiao Huahua, a freshman, wants to know how many new recruitment seminars she can attend completely (you cannot interrupt or leave when attending a new recruitment seminar).
[Problem Description] This problem is to schedule several competing recruitment briefing activities. They all require the exclusive use of a certain public resource (Xiaohuahua). The goal of scheduling is to find a maximum set of mutually compatible activities.
The activity selection problem is to select the largest subset of mutually compatible problems.

[Warm Tips] You should first sort all activities in ascending order by end time, then select possible time combinations, and find the maximum number of combinations. Using the qsort() sorting function is a good choice. The function prototype of qsort is:
void qsort(voidbase,size_t num,size_t width,int(__cdeclcompare)(const void*,const void*));
Function: Use quick sort routine for sorting Header file: stdlib.h
Parameter: 1 The first address of the array to be sorted; 2 the number of elements to be sorted in the array; 3 the space occupied by each element; 4 a pointer to a function, used to determine the order of sorting

Input format:
The first line is n, indicating that there are n recruitment briefings. The next n lines have two integers in each line indicating the start time and end time, represented by the number of hours starting from 0 o'clock on the first day of the recruitment meeting (24 hour format). n <= 1000.

Output format:
Maximum number of job fairs attended.

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

 3  
 9 10  
 10 20  
 8 15  

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

2

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

Analysis: According to the meaning of the question, the following image can be abstracted.
Insert image description here
It can be seen from the image that the solution with the smallest end point every time we choose must be the optimal solution, and the solution must be less than or equal to this solution.

#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 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;
}

2: Watch a movie

Author Wang Huiyong
Unit Hebei University of Science and Technology

It’s finally the weekend, and I obviously love watching movies. He wanted to see as many complete movies as possible in one day.
Now he gives you the playback schedule of his favorite movies, hoping that you can help him arrange them reasonably.

Input format:
The input contains multiple sets of test data. The first line of each set of input is an integer n (n<=100), which represents the total number of movies that Mingming likes.
In the next n lines, each line inputs two integers si and ei (1<=i<=n), indicating the start and end time of the i-th movie. In order to simplify the problem, each time is represented by a positive integer.
When n=0, the input ends.

Output format:
For each set of inputs, output the number of movies that can be seen completely.

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

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

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

5

Code length limit
16 KB
Time limit
1000 ms
Memory limit
32 MB

Analysis: Same as the first question

#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 n;
    while (cin >> n, 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: Participate in more activities and your life will be more exciting.

Author Long Xing
Unit: Guizhou Institute of Engineering Technology

Xiao Pan came to Guizhou University to study at university this year, and his university life is colorful. There are many clubs in the university, and each club holds some activities. Xiao Pan is an active child who wants to participate in more activities. We all know that different activities have different credits. Every activity has a start time and an end time.

Tomorrow is the weekend. Every club holding activities will post the start time, end time and credits of the activity on the school's WeChat public account in advance. Xiao Pan chatted with you about going to participate in activities tomorrow. He saw that there were many activities on it. He wanted to participate in more activities, but some activities would conflict. You and Xiao Pan calculated the maximum number of activities that he could participate in tomorrow. , and how many credits can be obtained?

Input format:
In the first line, n represents the number of activities. (n<= 100)
From the second line to line n + 1, each line has s, e, f (the time when s activity starts, the time when e activity ends, and the score of f activity), s, e, f are Positive integer, s, e <= 22, f <= 100.

Output format:
Please output the maximum number of activities that Xiao Pan can participate in and the scores obtained.

Input example:

5
1 3 5
2 3 5
3 4 5
4 5 5
4 6 5

Output sample:

3 15

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

Analysis: Same as the first question

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 110;

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

int main()
{
    
    
    int n;
    cin >> n;
    for (int i = 0; i < n; i ++ ) cin >> f[i].x >> f[i].y >> f[i].z;
    
    sort(f,f + n);
    
    int end = f[0].y;
    int ans = f[0].z, cnt = 1;
    for (int i = 0; i < n - 1; i ++ )
    {
    
    
        if(end <= f[i + 1].x)
        {
    
    
            end = f[i + 1].y;
            ans += f[i + 1].z;
            cnt ++;
        }
    }
    cout << cnt << ' ' << ans;
    
    return 0;
}

4: Packing problem

Author DS Course Group
Unit Zhejiang University

Suppose there are N items, the sizes of which are s 1 , s 2 ,..., s i ,..., s N , where s i is an integer satisfying 1≤s i ≤100. These items should be packed into a batch of boxes with a capacity of 100 (serial number 1-N). The packing method is: for each item, scan the boxes sequentially and put the item into the first box that is large enough to accommodate it. Please write a program to simulate this packing process and output the box number of each item and the number of boxes required to place all items.

Input format:
The first line of input gives the number of items N (≤1000); the second line gives N positive integers s i ( 1≤s i ≤100, indicating the size of the i-th item).
Output format:
Output the size of each item and the box number in which it is located in the order of input. Each item occupies one line, and the last line outputs the required number of boxes.

Input example:

8
60 70 80 90 30 40 10 20

Output sample:

60 1
70 2
80 3
90 4
30 1
40 5
10 1
20 2
5

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

Analysis: Directly simulate the process of loading items into a box according to the question. If it cannot be loaded, add a new box.

Insert image description here

#include <iostream>

using namespace std;

const int N = 1010;

int n, idx;
int f[N], a[N];

int main()
{
    
    
    cin >> n;
    for (int i = 0; i < n; i ++ ) cin >> a[i];
    
    for (int i = 0; i < n; i ++ )
    {
    
    
        int j = 0;
        for ( ;j < idx; j ++ )
            if(f[j] + a[i] <= 100)
            {
    
    
                f[j] += a[i];
                cout << a[i] << ' ' << j + 1 << endl;
                break;
            }
        if(j == idx)  // 增加一个箱子
        {
    
    
            f[idx ++ ] += a[i];
            cout << a[i] << ' ' << idx << endl;
        }
    }
    cout << idx;
    
    return 0;
}

5: CPA Recruitment I

Author Long Xing
Unit: Guizhou Institute of Engineering Technology

The new semester has begun. Our CPA was established in June 2019 with 20 veterans at the time. Now we need to recruit new members. Every year in the new semester, the Society Service Center will organize a Hundred Regiment Battle. Our CPA is recruiting new members for the first time, and we are looking forward to welcoming new members.
Every day, veterans go to recruit new people. Every time they recruit a new person, the new person will write a capital letter on the paper. CPA has four departments: Competition Department, Publicity Department, Office Department, and Organization Department. We stipulate that A represents the Competition department, B represents the Propaganda Department, C represents the Office, and D represents the Organization Department. After the club recruits new people, it is necessary to count how many people there are in each department?

Input format:
Enter a line of string, the length of the string should not be greater than 10000.

Output format:
Line break for each department output.

Input example:

AABBCCDD

Output sample:

Competition department 2 people!
Propaganda Department 2 people!
Office 2 people!
Organization Department 2 people!

Note:
Maybe some people are naughty with more than four characters ABCD. The actual number of people is based on ABCD.
Code length limit
16 KB
Time limit
400 ms
Memory limit
64 MB

Analysis: Simulate the process according to the meaning of the question

#include <iostream>
#include <algorithm>

using namespace std;

struct node
{
    
    
    string name;
    int num;
}f[4];

int main()
{
    
    
    string s;
    getline(cin, s);
    
    for (auto p : s)
        if(p >= 'A' && p <= 'D')
            f[p - 'A'].num ++;
    f[0].name = "Competition department";
    f[1].name = "Propaganda Department";
    f[2].name = "Office";
    f[3].name = "Organization Department";
    
    for (int i = 0; i < 4; i ++ )
        cout << f[i].name << ' ' << f[i].num << ' ' <<"people!\n";
    
    return 0;
}

6: CPA Recruitment II

Author Long Xing
Unit: Guizhou Institute of Engineering Technology

The new semester has begun. Our CPA was established in June 2019 with 20 veterans at the time. Now we need to recruit new members. Every year in the new semester, the Society Service Center will organize a Hundred Regiment Battle. Our CPA is recruiting new members for the first time, and we are looking forward to welcoming new members.

Every day, veterans go to recruit new people. Every time they recruit a new person, the new person will write a capital letter on the paper. CPA has four departments: Competition Department, Publicity Department, Office Department, and Organization Department. We stipulate that A represents the Competition department, B represents the Propaganda Department, C represents the Office, and D represents the Organization Department. After the club recruits new people, it is necessary to count how many people there are in each department? One day the president comes suddenly and needs you to give him a list of department personnel. The list needs to be sorted from large to small according to the number of people. If you are smart, you will directly write a program to the president and let him use the program to sort directly.

Input format:
Enter a line of string, the length of the string should not be greater than 10000.

Output format:
If the number of people is the same, sort them in dictionary order from small to large, and wrap each department in the output.

Input example:

AABBCCCDDAA

Output sample:

Competition department 4 people!
Office 3 people!
Organization Department 2 people!
Propaganda Department 2 people!

Note:
Some people may be naughty with more than four characters ABCD. The actual number of people is based on ABCD.
Code length limit
16 KB
Time limit
1000 ms
Memory limit
64 MB

Analysis: Simulate the process according to the meaning of the question

#include <bits/stdc++.h>

using namespace std;

const int N = 10010;

struct node{
    
    
    int num;
    string name;
}f[N];

bool cmp(node a, node b)
{
    
    
    if(a.num != b.num) return a.num > b.num;
    return a.name < b.name;
}
int cnt[4];

int main()
{
    
    
    string s;
    getline(cin, s);
    for (auto p : s)
        if(p >= 'A' && p <= 'D')
            cnt[p - 'A'] ++;
    for (int i = 0; i < 4; i ++ )
        f[i].num = cnt[i];
    f[0].name = "Competition department";
    f[1].name = "Propaganda Department";
    f[2].name = "Office";
    f[3].name = "Organization Department";
    
    sort(f, f + 4, cmp);
    
    for (int i = 0; i < 4; i ++ )
    {
    
    
        cout << f[i].name << " " << f[i].num << " ";
        cout << "people!\n";
    }
    return 0;
}

7: h0154. Pirates of the Caribbean - Optimal Loading Problem

Author Huang Zhengpeng
Unit: Guizhou Institute of Engineering Technology
6.png

In the southeastern part of North America, there is a mysterious sea area with blue sea, blue sky and bright sunshine. This is the Caribbean Sea where pirates are said to be most active. In the 17th century, this was the only place for merchant fleets from the European continent to reach the Americas. Therefore, piracy activities were very rampant at that time. Pirates not only attacked passing merchants, but even attacked British royal ships... One day, the pirates intercepted a ship
. A cargo ship filled with all kinds of antiques. Each antique is priceless. Once broken, it loses its value. Although the pirate ship is large enough, the load capacity is C and the weight of each antique is wi. How should the pirates load as many treasures as possible onto the pirate ship?

Input format:
In the first line, enter T groups of test data. For each group of test data, enter the load capacity c and the number of antiques n. In the next line, enter the weight wi of each antique, separated by spaces.

Output format:
Maximum number of antiques that can be loaded into each group

Input example:

1
30 8
4 10 7 11 3 5 14 2

Output sample:

5

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

Analysis: According to the analysis of the meaning of the question, we only need to select the antique with the smallest weight every time to get a set of optimal solutions, so we only need to sort it from small to large and traverse it once.

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010;

int m, n;
int w[N];

int main()
{
    
    
    int T;
    cin >> T;
    
    while (T -- )
    {
    
    
        cin >> m >> n;
        for (int i = 0; i < n; i ++ ) cin >> w[i];
        
        sort(w, w + n);
        
        int cnt = 0;
        for (int i = 0; i < n; i ++ )
        {
    
    
            if(w[i] <= m)
            {
    
    
                m -= w[i];
                cnt ++;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}

Guess you like

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