CSDN Programming Contest Thirty Questions

Competition overview

CSDN Programming Competition Thirty: Competition Details (csdn.net)

competition solution

Topic 1. Natural Gas Orders

Natural gas transportation is expensive and dangerous. In order to save transportation costs and promote green environmental protection, it is necessary to optimize order delivery as much as possible. For example, natural gas orders in the same area can be delivered at one time. There is a need to transport natural gas to multiple regions. But there may be multiple order demands in the same region. At present, it is only known that some pairs of orders are from the same region. Natural gas in the same region needs to be delivered at one time as much as possible to reduce transportation costs, so it is necessary to put orders from the same region together as much as possible. Orders are numbered from 1 to n.

#include <cstdio>
#include <vector>

int data [100005];

struct node {
    int nId;
    int cId;
};

int search (int x) {
    return data [x] == x ? x : data [x] = search (data [x]);
}

void input (node* p) {
    scanf ("%d", &p -> nId);
    p -> cId = search (p -> nId);
}

int main () {
    int n, m;
    scanf ("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) data [i] = i;
    for (int i = 1; i <= m; i++) {
        node x, y;
        input (&x);
        input (&y);
        data [(x.cId < y.cId ? y : x).cId] = (x.cId < y.cId ? x : y).cId;
    }
    std::vector<int> result [n + 1];
    for (int i = 1; i <= n; i++) {
        result [data [i] == i ? i : search (data [i])].push_back (i);
    }
    for (int i = 1; i <= n; i++) {
        if (i > 1 && result [i - 1].size () > 0) printf ("\n");
        for (unsigned int j = 0; j < result [i].size (); j++) {
            if (j > 0) printf (" ");
            printf ("%d", result [i][j]);
        }
    }
    return 0;
}

Use the union search algorithm to group related orders into one category.

Compared with the topic of the traditional union search algorithm, this question has one more statistical process. It is necessary to put the orders belonging to the same category into the corresponding list, and finally output them collectively.

Therefore, during the classification process, if there is a smaller order number in the same class, the type can be represented by a smaller value. For example, 1 4 5 is one category, 2 3 is one category, then 1 and 2 can be used to represent the orders of these two categories. Doing so can facilitate the final statistical operation.

Topic 2, Xiaoyi Reading

The book is the ladder of human progress. Xiaoyi chooses to read a few more pages or a few fewer pages every week due to work reasons. Xiaoyi wants to know the day of the week she will finish reading a book with n pages.

#include <cstdio>

int main () {
    int n; scanf ("%d", &n);
    int data [7]; for (int i = 0; i < 7; i++) scanf ("%d", &data [i]);
    int d; for (d = 0; n > 0; d = (d + 1) % 7) n = n - data [d];
    return 0;
}

Direct violent simulation, starting from Monday, each time reduces the number of pages corresponding to that day, if there are remaining pages, continue to skip to the next day, otherwise stop. There is an optimization point here. You can subtract the number of pages that can be read in a week at one time, until the remaining pages are less than a week, and then judge day by day.

Topic 3. Buy an apple

Xiao Yi went to a nearby store to buy apples, and the treacherous vendor used a bundled deal, only offering packages of 6 and 8 per bag (the packages cannot be split). But now Xiaoyi only wants to buy exactly n apples, and Xiaoyi wants to buy as few bags as possible for easy carrying. If he cannot buy exactly n apples, Xiao Yi will not buy them.

int main () {
    int result = -1;
    int n;
    scanf ("%d", &n);
    int n_6 = n / 6 + 1;
    for (int i = 0; i < n_6; i++) {
        for (int j = 0; j < n_6; j++) {
            if (i * 6 + j * 8 == n) {
                if (i + j < result || result == -1) result = i + j;
            }
        }
    }
    return 0;
}

The questions are relatively simple, just try one by one. When you find a condition that meets the conditions, judge whether the number of bags is smaller than the previous consumption. If it is smaller, update the answer until you find the optimal solution.

You can also use a greedy algorithm to solve the problem. First, judge whether the apple to be purchased is an even number. If it is not an even number, return -1 directly.

First of all, suppose you buy all 8 products per bag, and make up the shortage with 6 products per bag to ensure that the number of apples you buy is not less than the demand.

After that, reduce the number of products per bag by 8, and increase the number of products by 6 per bag if the number of apples bought in the process is less than expected.

Break out of the loop until a solution that just satisfies the condition is found.

This method is theoretically feasible, but the hand speed is more tested during the competition. The blogger did not try it, but just provided an idea. Interested friends can try it by themselves.

Topic 4. Round table

There are N guests and enough round tables. The host arranges each guest to sit at a round table, but each guest wishes to have some vacant seats on the left and right, otherwise they will feel shy. Note that if a guest is the only one at the round table, then the number of empty seats on his left is the number of empty seats on his right. Just ask how many seats the host needs to prepare so that each guest can sit comfortably.

#include <cstdio>
#include <algorithm>

long long int data1 [1000005];
long long int data2 [1000005];

int main () {
    long long int result = 0;
    long long int n;
    scanf ("%lld", &n);
    for (long long int i = 0; i < n; i++) scanf ("%lld %lld", &data1 [i], &data2 [i]);
    std::sort (data1, data1 + n);
    std::sort (data2, data2 + n);
    for (long long int i = 0; i < n; i++) result = result + max (data1 [i], data2 [i]);
    return 0;
}

Greedy algorithm. Try to make the vacancies fit, and add n after the calculation to get the final result.

This question appeared once in the 11th competition. You need to pay attention to the data range, you must use long long int to pass all the test data, otherwise you can only pass 90% of the test data.

Guess you like

Origin blog.csdn.net/x1051496412/article/details/129110610