AtCoder Beginner Contest 137 Solving report (A ~ E)

Reprinted from: https://lornd.top/index.php/archives/37/

Game address: AtCoder Beginner Contest The 137

A - +-x

Subject to the effect

Given two numbers \ (A \) , \ (B \) it, find \ (A + B \) , - \ (B \ A) , \ (A \ B Times \) maximum values

Problem-solving ideas

#include <cstdio>
#include <algorithm>

int a, b;

int main() {
    scanf("%d%d", &a, &b);
    printf("%d", std::max(a + b, std::max(a - b, a * b)));
    return 0;
}

B - One Clue

Subject to the effect

There \ (2000001 \) stones in a row, numbered \ (- 1000000 \) to \ (1000000 \) , wherein a continuous \ (K \) stone is dyed black, which coordinates \ (X-\ ) the stone is black, which seek stones may be dyed black.

Problem-solving ideas

Points of discussion, coordinates \ (X \) stone may in this \ (K \) anywhere in stones, and in addition to the extreme left or the extreme right, answer the remaining positions generated are included in the leftmost or most among the answers right to produce, so only need to calculate in both cases, which a few stones can be dyed black.

#include <cstdio>

int k, x;

int main() {
    scanf("%d%d", &k, &x);
    for (int i = x - k + 1; i <= x + k - 1; ++i) {
        printf("%d ", i);
    }
    return 0;
}

C - Green Bin

Subject to the effect

Definition of a string \ (A \) another string \ (B \) of the puzzle, if and only if \ (A \) may be made of \ (B \) rearranged composition given \ (n-\) string, asked how many of \ ((i, j) ( i <j) \) satisfies the first \ (i \) string with the first \ (j \) string mutually puzzle.

Problem-solving ideas

According to the meaning of problems, we can see each other two strings puzzle, if and only if a new two strings get it sorted after each letter are equal, for example: greenbinand beginnermutually puzzle, because after they have been sorted the beeginnrstring.

So we can this (n \) \ strings are sorted, then overall this \ (n \) string sorting to quickly get the same string, then count each of the same string the number, which can either take two to find the right \ ((i, J) \) , which is a combination of several problems.

#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>

const int MAXN = 1e5 + 5;

int n;
int tmp[MAXN], cnt;
long long int ans;
std::string s[MAXN];

int main() {
    std::cin >> n;
    for (int i = 1; i <= n; ++i) {
        std::cin >> s[i];
        std::sort(s[i].begin(), s[i].end());
    }
    std::sort(s + 1, s + n + 1);
    for (int i = 1; i <= n; ++i) {
        if (s[i] != s[i - 1])
            ++cnt;
        ++tmp[cnt];
    }
    for (int i = 1; i <= cnt; ++i) {
        ans += 1ll * tmp[i] * (tmp[i] - 1) >> 1;
    }
    std::cout << ans;
    return 0;
}

D - Summer Vacation

Subject to the effect

There \ (n \) modes of operation, the \ (i \) modes of operation will take a day to finish, and will \ (A_i \) days (including the work of the day) to obtain \ (B_i \) income, total there \ (m \) days, seeking the maximum benefit.

Problem-solving ideas

We might as well be greedy backwards, leaving \ (1 \) days, the remaining \ (2 \) days ...... Each time, we can record the rest under the \ (i \) days of work to get income, and then take profit greatest work, which can be used directly to complete the priority queue.

#include <cstdio>
#include <queue>
#include <algorithm>

const int MAXN = 1e5 + 1;

int n, m, ans, cur;

struct job {
    int wait, val;

    job(int ww = 0, int vv = 0) {
        wait = ww;
        val = vv;
    }

    bool operator < (const job &another) const {
        return wait < another.wait;
    }
}arr[MAXN];

struct node {
    int key;

    node (int kk = 0) {
        key = kk;
    }

    bool operator < (const node &another) const {
        return key < another.key;
    }
};

std::priority_queue<node> q;

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        scanf("%d%d", &arr[i].wait, &arr[i].val);
    }
    std::sort(arr + 1, arr + n + 1);
    cur = 1;
    for (int i = 1; i <= m; ++i) {
        while (cur <= n && arr[cur].wait <= i) {
            q.push(node(arr[cur].val));
            ++cur;
        }
        if (!q.empty()) {
            ans += q.top().key;
            q.pop();
        }
    }
    printf("%d", ans);
    return 0;
}

E - Coins Respawn

Subject to the effect

There is a \ (n-\) points, \ (m \) have edges directed graph, the \ (I \) Article edge of \ (C_i \) coins, may be repeated to collect. From \ (1 \) went \ (n \) , and at the end (in \ (n \) to pay when you can choose to continue, you can also choose to end) when \ (T \ times P \) gold coins, which \ (T \) is the number of edges traversed. The remaining demand the maximum number of coins, most if not present, the output \ (--1 \) .

Problem-solving ideas

We each \ (c_i \) are subtracted \ (P \) to obtain \ (d_i \) , the title became, after each side of a gathering \ (d_i \) gold coins, the largest number of remaining at the end to ask gold, namely \ (1 \) to the \ (n-\) longest path.

Since this has to FIG, and may have a ring, it can not be used topological sort + DP way, but we can SPFA relaxation operation is less than the number of change is greater than the number to obtain the longest path, while the positive determination ring ( misunderstanding) method on the same ring and the negative judgment, the number of times a point above its penetration into the team (or \ (n-\) ) can.

However, no positive solution does not represent a ring, this is possible because the ring by not reaching point n \ (n-\) , we should also be able to reach a pre-point (n-\) \ set point, then SPFA time only to be able to reach the point \ (n \) point, and this pre-treatment can reverse build map , obtained from (n \) \ point can reach the point of departure, we ask that point.

#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>

const int MAXN = 1e5 + 5;

int n, m, p, u, v, k;
int dist[MAXN], num[MAXN];
bool flag[MAXN], inQueue[MAXN];

struct graph {
    int vertex, val;

    graph(int vv = 0, int vval = 0) {
        vertex = vv;
        val = vval;
    }
};

std::vector<graph> con[MAXN], reverse[MAXN];
std::queue<int> q;

void ins(int start, int end, int key, std::vector<graph> a[]) {
    a[start].push_back(graph(end, key));
}

void init(int key) {
    flag[key] = true;
    for (int i = 0; i < reverse[key].size(); ++i) {
        int nxt = reverse[key][i].vertex;
        if (flag[nxt] == false)
            init(nxt);
    }
}

int spfa() {
    for (int i = 2; i <= n; ++i) {
        dist[i] = -(1e9 + 7);
    }
    dist[1] = 0;
    q.push(1);
    inQueue[1] = true;
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        inQueue[cur] = false;
        for (int i = 0; i < con[cur].size(); ++i) {
            int nxt = con[cur][i].vertex, cost = con[cur][i].val;
            if (flag[nxt] && dist[nxt] < dist[cur] + cost) {
                dist[nxt] = dist[cur] + cost;
                if (!inQueue[nxt]) {
                    q.push(nxt);
                    inQueue[nxt] = true;
                    ++num[nxt];
                    if (num[nxt] > n)
                        return -1;
                }
            }
        }
    }
    return std::max(dist[n], 0);
}

int main() {
    scanf("%d%d%d", &n, &m, &p);
    for (int i = 1; i <= m; ++i) {
        scanf("%d%d%d", &u, &v, &k);
        ins(u, v, k - p, con);
        ins(v, u, 1, reverse);
    }
    init(n);
    printf("%d", spfa());
    return 0;
}

Guess you like

Origin www.cnblogs.com/lornd/p/11364564.html