Codeforces Round #599 (Div. 2)

A - Maximum Square

The meaning of problems: for \ (n-\) block width \ (1 \) length \ (a_i \) of wood, the boards these pieces together, seeking the maximum side length of a square form.

Solution: greedy, sorted in descending order, then find the first meet \ (a_i <i \) location break away.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int n, a[1005];

int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
#endif // KisekiPurin
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i)
            scanf("%d", &a[i]);
        sort(a + 1, a + 1 + n, greater<int>());
        int ans = 0;
        for(int i = 1; i <= n; ++i) {
            if(a[i] >= i)
                ans = i;
            else
                break;
        }
        printf("%d\n", ans);
    }
}

B1 - Character Swap (Easy Version)

Question is intended: to two mutually different strings \ (S \) and \ (T \) , must select a \ (s [i] \) and \ (t [j] \) exchange exactly 1, Q suggest that the two strings are equal.

Solution: determine exactly once after the exchange should be reduced by up to two unequal position to identify unequal position is not exactly just two, and if so, then determine the direct exchange of equal, direct or No.

Note: Because it is not all special sentenced to 0, otherwise 0 is a certain legal (exchange (1,1)) different from each other.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int n;
char s[10005], t[10005];

int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
#endif // KisekiPurin
    int k;
    scanf("%d", &k);
    while(k--) {
        scanf("%d%s%s", &n, s + 1, t + 1);
        vector<int> dif;
        for(int i = 1; i <= n; ++i) {
            if(s[i] != t[i])
                dif.push_back(i);
        }
        if(dif.size() != 2) {
            puts("No");
        } else {
            swap(s[dif[0]], t[dif[1]]);
            if(strcmp(s + 1, t + 1) == 0)
                puts("Yes");
            else
                puts("No");
        }
    }
}

B2 - Character Swap (Hard Version)

Question is intended: to two mutually different strings \ (S \) and \ (T \) , select a \ (s [i] \) and \ (t [j] \) exchange does not exceed \ (2N \ ) times, and asked if he could make two strings are equal.

Solution: It is clear that all the necessary conditions for the solvability of the letters appear is an even number, first determine this. Secondly, after the above must meet the necessary conditions for a solution, because the use of up to 2 times on a different exchange can eliminate at least one position. Specific approach is: fixed already equal to the first half, and if \ (S [I] \ NEQ T [I] \) , if there is a later \ (S [ID] = S [I] \) , then put \ (s [id] \) and \ (t [i] \) exchange. Otherwise, there must be a behind \ (T [ID] = S [I] \) , but \ (t [id] \) can not be directly \ (t [i] \) exchange, and therefore it \ ( s [n] \) exchange (this is better to write), then \ (s [n] \) and \ (t [i] \) exchange.

Note: The output first output \ (i \) and then output \ (J \) , the WA is not worth it.

C - Tile Painting

Meaning of the questions: to a row of \ (n \) squares, require all \ (i, j \) satisfies \ (| ij | \) is \ (n \) of factors, should be the same color, to do the most coating color number.

Solution: is the gcd very clear that all prime factors, of course, as are the prime factors of all direct judgment there is no more then output \ (1 \) can, why put C deceptive title, do not we put B title .

D - 0-1 MST

Question is intended: to a \ (n-\) complete graph nodes have \ (m \) section of the right side 1, the other edge weight is 0, find the weight and the minimum spanning tree.

Solution: rewrite Prim algorithm, put three containers, a right is infinite (unknown), a weight of 0, a weight of 1, each priority 0 out inside the container extension, do not consume anything else, remove the expansion vessel 1 , cost ++. 1 container can extend the time to seize the infinite container node, node 0 container can wrest from the container, so the container and the container should be an infinite set. Complexity does not want to understand.

Note: for a practice several times, last time should be enough to waste too much in front in exchange for going, it should be similar 0-1BFS ideas, rewritten Prim algorithm. ? ? ? Not that 0-1BFS after this magic change it? The direct use 0-1BFS on it.

Guess you like

Origin www.cnblogs.com/KisekiPurin2019/p/11809724.html