Educational Codeforces Round 70 题解

Game link : https://codeforc.es/contest/1202
 
A. Two Binary Strings by You Are the Given ...
meaning of the questions : given two binary numbers \ (f (x) \) and \ (f (y) \ ) , define a binary number \ (S_k = F (X) + 2 ^ K \ CDOT F (Y) \) , asks \ (K \) when what value of \ (S_k \) reverse string (binary serial number 01 seen) lexicographically smallest.

Analysis : First of all, we need to know for a binary number by 2, equivalent to the number of whole left one, so we just need to find \ (f (y) \) position at the far right of the 1 \ (pos \) , and to make it \ (f (x) \) in \ (POS \) left latest (\ Y) \ matching string thus obtained reverse lexicographically smallest inevitable.

AC Code :

#include <bits/stdc++.h>
#define SIZE 200007
#define rep(i, a, b) for(int i = a; i <= b; ++i)
using namespace std;
typedef long long ll;
void io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}
ll n, m, t;
string s1, s2;
int main() {
    io(); cin >> t;
    while(t--) {
        cin >> s1 >> s2;
        int len1 = s1.length() - 1, len2 = s2.length() - 1;
        n = 0;
        while(s2[len2--] == '0') ++n;
        m = n;
        while(s1[len1 - m] == '0') ++m;
        cout << m - n << endl;
    }
}

 
B. You Are Given a Decimal String ...
meaning of the questions : speak not very clear, you can see the sample questions in understanding. Given a string string each digit of two numbers of any combination of determination, query, in order to obtain the string, multiple configuration requires a minimum of a few invalid number. For example:
given a string \ (0840 \) , determined for the two numbers \ ((2, 4) \) .

  1. 0 (Initial must be zero)
  2. 04 (\(+4\)
  3. 048 (\(+4\)
  4. 0482 ( \ (4 + \) , and then only if more than 10 digits)
  5. 04824 (\(+2\)
  6. 048248 (\(+4\)
  7. 0482480 (\(+2\)

Thus, (0) 4 (8) 2 (4) 8 (0), the number generated for that number of 3.

Analysis : From the first string \ (K \) bits to \ (k + 1 \) bits actually corresponds to a change of the \ (\ K +. 1 Vert S_ {} -s_k \ Vert \) , this value will not exceed 10 number of digits can be invalid, so we just need to find the 10 cases of violence generated.

AC Code :

#include <bits/stdc++.h>
#define SIZE 200007
#define rep(i, a, b) for(int i = a; i <= b; ++i)
const int maxn = 1e9;
using namespace std;
typedef long long ll;
void io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}
int n, m, t; int dp[10];
string s;
int main() {
    io(); cin >> s;
    int len = s.length();
    rep(i, 0, 9) {
        rep(j, 0, 9) {
            rep(i, 0, 9) dp[i] = maxn;
            rep(x, 0, 9) {
                rep(y, 0, 9) {
                    if (!x && !y) continue;
                    dp[(x * i + y * j) % 10] = min(x + y - 1, dp[(x * i + y * j) % 10]);
                }
            }
            int ans = 0;
            rep(k, 0, len - 2) {
                int tmp = s[k + 1] - s[k];
                if (dp[(tmp + 10) % 10] == maxn) { ans = -1; break; }
                ans += dp[(tmp + 10) % 10];
            }
            cout << ans << ' ';
        }
        cout << endl;
    }
}

 
C. You Are Given a WASD-string ...
that Italy : T set of inputs, each containing only a given input " \ (W is, A, S, D \) " string (string again .. .). \ (W, A, S, D \) representing a robot can be moved up, left, down, and right direction one space. The definition of the robot through an area of the minimum area that can contain all the robot went off the grid. Now, you can add a character at any position in the string (only for the \ (W, A, S, D \) ), asking the smallest area possible robot walked.

Analysis : As to only one character, so we found that lateral movement and longitudinal movement do not interfere (the smallest rectangle that can only be retracted a unit length or width), so we need only be divided into two sub-problems lateral longitudinal consider and find The minimum area can be. I thought a prefix and write, while standard process gives a more compact solution.

AC Code :

#include <bits/stdc++.h>
#define SIZE 200007
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
void io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}
int n, m, t;
string str;
struct node {
    int val;
    int pos;
    int la, lb, ra, rb;
}p1[SIZE], p2[SIZE];
void init1(node p[], int n) {
    int x = INF, y = -INF;
    rep(i, 0, n) {
        p[i].la = min(x, p[i].pos);
        x = min(x, p[i].la);
        p[i].ra = max(y, p[i].pos);
        y = max(y, p[i].ra);
    }
}
void init2(node p[], int n) {
    int x = INF, y = -INF;
    for (int i = n; i >= 0; --i) {
        p[i].lb = min(x, p[i].pos);
        x = min(x, p[i].lb);
        p[i].rb = max(y, p[i].pos);
        y = max(y, p[i].rb);
    }
}
int main() {
    io(); cin >> t;
    while (t--) {
        cin >> str;
        int k = 1, j = 1;
        rep(i, 0, str.length() - 1) {
            if (str[i] == 'W') p1[k++].val = 1;
            else if (str[i] == 'S') p1[k++].val = -1;
            else if (str[i] == 'A') p2[j++].val = 1;
            else p2[j++].val = -1;
        }
        rep(i, 0, k - 1) p1[i].pos = p1[i - 1].pos + p1[i].val;
        rep(i, 0, j - 1) p2[i].pos = p2[i - 1].pos + p2[i].val;
        init1(p1, k - 1); init1(p2, j - 1);
        init2(p1, k - 1); init2(p2, j - 1);
        ll w = p1[k - 1].ra, a = p2[j - 1].la, s = p1[k - 1].la, d = p2[j - 1].ra;
        ll h = p1[k - 1].ra - p1[k - 1].la + 1, wid = p2[j - 1].ra - p2[j - 1].la + 1;
        bool f1 = false, f2 = false;
        rep(i, 0, k - 1) {
            if (p1[i].lb < p1[i].la && p1[i].rb < p1[i].ra) { f1 = true; break; }
            if (p1[i].lb > p1[i].la && p1[i].rb > p1[i].ra) { f1 = true; break; }
        }
        rep(i, 0, j - 1) {
            if (p2[i].lb < p2[i].la && p2[i].rb < p2[i].ra) { f2 = true; break; }
            if (p2[i].lb > p2[i].la && p2[i].rb > p2[i].ra) { f2 = true; break; }
        }
        if (f1 || f2) {
            if (f1 && f2) {
                if (wid > h) cout << wid * (h - 1) << endl;
                else cout << (wid - 1) * h << endl;
                continue;
            }
            if (f1) cout << wid * (h - 1) << endl;
            else cout << (wid - 1) * h << endl;
        }
        else cout << wid * h << endl;
    }
}

 
D. Print a 1337-string ...
that Italy : a given positive integer \ (n-\) , a construct comprising \ (n-\) a \ (1337 \) substring of string (or character string ... ).

Analysis : We consider each configured \ (C_n ^ 2 \) two until completion, for example, \ (10 + 3 + 1 = 6 \) , then we can construct \ (13,373,737 \) . But pay attention to the last remaining 2 may be, but can not be repeated twice structure 1. So finally we construct 2 is inserted in the bottom third of two 1, for example: \ (6 + 2 = 8 \) construct: \ (13,311,337 \) .

AC Code :

#include <bits/stdc++.h>
#define SIZE 200007
#define rep(i, a, b) for(int i = a; i <= b; ++i)
using namespace std;
typedef long long ll;
void io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}
ll n, m, t; bool flag;
ll dp[SIZE];
vector<int> v;
int main() {
    io(); cin >> t;
    rep(i, 1, 100050) dp[i] = 1ll * i * (i - 1) / 2;
    while (t--) {
        cin >> n; m = n; v.clear();
        if (n == 2) { cout << "11337\n"; continue; }
        flag = false;
        while (n) {
            int pos = lower_bound(dp, dp + 100010, n) - dp;
            if (dp[pos] > n) --pos;
            n -= dp[pos];
            v.emplace_back(pos);
            if (n == 2) { flag = true; break; }
        }
        int cnt = 1, it = v.size() - 1;
        cout << "13";
        while (cnt < v[0]) {
            if (flag && cnt == v[0] - 2) cout << "11";
            if (cnt < v[it]) cout << 3;
            else { --it, --cnt; cout << 7; }
            cnt++;
        }
        cout << "7\n";
    }
}

Guess you like

Origin www.cnblogs.com/st1vdy/p/11329097.html