Loading

「解题报告」Codeforces Round 905 (Div. 3)

A. Morning

You are given a four-digit pin code consisting of digits from \(0\) to \(9\) that needs to be entered. Initially, the cursor points to the digit \(1\). In one second, you can perform exactly one of the following two actions:

  • Press the cursor to display the current digit,
  • Move the cursor to any adjacent digit.

The image above shows the device you are using to enter the pin code. For example, for the digit \(5\), the adjacent digits are \(4\) and \(6\), and for the digit \(0\), there is only one adjacent digit, \(9\).

Determine the minimum number of seconds required to enter the given four-digit pin code.

Input

Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) - the number of the test cases. This is followed by their description.

The single line of each test case describes the pin code as a string of length \(4\), consisting of digits from \(0\) to \(9\).

Output

For each test case, output the minimum number of seconds required to enter the given pin code.

简单模拟题。

// The code was written by yifan, and yifan is neutral!!!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug puts("NOIP rp ++!");
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define per(i, a, b, c) for (int i = (a); i >= (b); i -= (c))

template<typename T>
inline T read() {
    T x = 0;
    bool fg = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        fg |= (ch == '-');
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    return fg ? ~x + 1 : x;
}

template<typename T>
void write(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

template<typename T>
void print(T x, char c) {
   write(x);
   putchar(c);
}

int T;
int s[10];

void solve() {
    rep (i, 1, 4, 1) {
        scanf("%1d", &s[i]);
        if (s[i] == 0) {
            s[i] = 10;
        }
    }
    int cur = 1, ans = 4;
    rep (i, 1, 4, 1) {
        if (s[i] != cur) {
            ans += abs(s[i] - cur);
            cur = s[i];
        }
    }
    print(ans, '\n');
}

int main() {
    T = read<int>();
    while (T --) {
        solve();
    }
    return 0;
}

B. Chemistry

You are given a string \(s\) of length \(n\), consisting of lowercase Latin letters, and an integer \(k\).

You need to check if it is possible to remove exactly \(k\) characters from the string \(s\) in such a way that the remaining characters can be rearranged to form a palindrome. Note that you can reorder the remaining characters in any way.

A palindrome is a string that reads the same forwards and backwards. For example, the strings "z", "aaa", "aba", "abccba" are palindromes, while the strings "codeforces", "reality", "ab" are not.

Input

Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of the test cases. This is followed by their description.

The first line of each test case contains two integers \(n\) and \(k\) (\(0 \leq k < n \leq 10^5\)) — the length of the string \(s\) and the number of characters to be deleted.

The second line of each test case contains a string \(s\) of length \(n\), consisting of lowercase Latin letters.

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).

Output

For each test case, output "YES" if it is possible to remove exactly \(k\) characters from the string \(s\) in such a way that the remaining characters can be rearranged to form a palindrome, and "NO" otherwise.

You can output the answer in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers.

\(26\) 个桶,然后先取偶数对元素,最后判断偶数对元素的总个数是否大于等于 \(n - k + 1\) 即可。

// The code was written by yifan, and yifan is neutral!!!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug puts("NOIP rp ++!");
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define per(i, a, b, c) for (int i = (a); i >= (b); i -= (c))

template<typename T>
inline T read() {
    T x = 0;
    bool fg = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        fg |= (ch == '-');
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    return fg ? ~x + 1 : x;
}

template<typename T>
void write(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

template<typename T>
void print(T x, char c) {
   write(x);
   putchar(c);
}

const int N = 1e5 + 5;

int T, n, k;
int cnt[26];
char s[N];

void solve() {
    rep (i, 0, 25, 1) {
        cnt[i] = 0;
    }
    n = read<int>(), k = read<int>();
    scanf("%s", s);
    rep (i, 0, n - 1, 1) {
        ++ cnt[s[i] - 'a'];
    }
    ll res = 0;
    rep (i, 0, 25, 1) {
        res += 2 * (cnt[i] / 2);
    }
    if (res >= n - k - 1) {
        puts("YES");
    } else {
        puts("NO");
    }
}

int main() {
    T = read<int>();
    while (T --) {
        solve();
    }
    return 0;
}

C. Raspberries

You are given an array of integers \(a_1, a_2, \ldots, a_n\) and a number \(k\) (\(2 \leq k \leq 5\)). In one operation, you can do the following:

  • Choose an index \(1 \leq i \leq n\),
  • Set \(a_i = a_i + 1\).

Find the minimum number of operations needed to make the product of all the numbers in the array \(a_1 \cdot a_2 \cdot \ldots \cdot a_n\) divisible by \(k\).

Input

Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains two integers \(n\) and \(k\) (\(2 \leq n \leq 10^5\), \(2 \leq k \leq 5\)) — the size of the array \(a\) and the number \(k\).

The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10\)).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).

Output

For each test case, output the minimum number of operations needed to make the product of all the numbers in the array divisible by \(k\).

分两类处理,一类是 \(k = 2, 3, 5\) 的时候,另一类是 \(k = 4\) 的时候。

// The code was written by yifan, and yifan is neutral!!!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug puts("NOIP rp ++!");
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define per(i, a, b, c) for (int i = (a); i >= (b); i -= (c))

template<typename T>
inline T read() {
    T x = 0;
    bool fg = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        fg |= (ch == '-');
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    return fg ? ~x + 1 : x;
}

template<typename T>
void write(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

template<typename T>
void print(T x, char c) {
   write(x);
   putchar(c);
}

const int N = 1e5 + 5;

int T, n, k;
int a[N];

int gcd(int a, int b) {
    if (!b) {
        return a;
    }
    return gcd(b, a % b);
}

void solve() {
    n = read<int>(), k = read<int>();
    int g, ans = 1e9;
    ll res = 1;
    rep (i, 1, n, 1) {
        a[i] = read<int>();
        res = res * a[i] % k;
    }
    if (!res) {
        ans = 0;
        print(ans, '\n');
        return ;
    }
    if (k != 4) {
        rep (i, 1, n, 1) {
            rep (j, 1, 10, 1) {
                if (k * j < a[i])   continue ;
                ans = min(ans, k * j - a[i]);
            }
        }
        print(ans, '\n');
    } else {
        int fg1 = 0, fg2 = 0;
        rep (i, 1, n, 1) {
            if (a[i] % 2 == 0) {
                ++ fg2;
            } else {
                ++ fg1;
            }
        }
        if (fg1 && fg2) {
            puts("1");
            return ;
        }
        if (!fg1 && fg2) {
            puts("2");
            return ;
        }
        if (!fg2) {
            ans = 2;
            rep (i, 1, n, 1) {
                rep (j, 1, 10, 1) {
                    if (k * j < a[i])   continue ;
                    ans = min(ans, k * j - a[i]);
                }
            }
            print(ans, '\n');
        }
    }
}

int main() {
    T = read<int>();
    while (T --) {
        solve();
    }
    return 0;
}

D. In Love

Initially, you have an empty multiset of segments. You need to process $$\(q\)$$ operations of two types:

  • \(+\) \(l\) \(r\) — Add the segment \((l, r)\) to the multiset,
  • \(-\) \(l\) \(r\) — Remove exactly one segment \((l, r)\) from the multiset. It is guaranteed that this segment exists in the multiset.

After each operation, you need to determine if there exists a pair of segments in the multiset that do not intersect. A pair of segments \((l, r)\) and \((a, b)\) do not intersect if there does not exist a point \(x\) such that \(l \leq x \leq r\) and \(a \leq x \leq b\).

Input

The first line of each test case contains an integer \(q\) (\(1 \leq q \leq 10^5\)) — the number of operations.

The next \(q\) lines describe two types of operations. If it is an addition operation, it is given in the format \(+\) \(l\) \(r\). If it is a deletion operation, it is given in the format \(-\) \(l\) \(r\) (\(1 \leq l \leq r \leq 10^9\)).

Output

After each operation, print "YES" if there exists a pair of segments in the multiset that do not intersect, and "NO" otherwise.

You can print the answer in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers.

考察 multiset 的使用。

// The code was written by yifan, and yifan is neutral!!!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug puts("NOIP rp ++!");
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define per(i, a, b, c) for (int i = (a); i >= (b); i -= (c))

template<typename T>
inline T read() {
    T x = 0;
    bool fg = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        fg |= (ch == '-');
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    return fg ? ~x + 1 : x;
}

template<typename T>
void write(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

template<typename T>
void print(T x, char c) {
   write(x);
   putchar(c);
}

using tii = tuple<ll, ll>;

int n;
multiset<ll> l, r;

int main() {
    n = read<int>();
    string op;
    ll x, y;
    rep (i, 1, n, 1) {
        cin >> op >> x >> y;
        if (op == "+") {
            l.emplace(x);
            r.emplace(y);
        } else {
            l.erase(l.lower_bound(x));
            r.erase(r.lower_bound(y));
        }
        
        if (l.size() < 2) {
            puts("NO");
            continue ;
        }
        ll it1 = *r.begin(), it2 = *l.rbegin();
        if (it1 < it2) {
            puts("YES");
        } else {
            puts("NO");
        }
    }
    return 0;
}

E. Look Back

You are given an array of integers \(a_1, a_2, \ldots, a_n\). You need to make it non-decreasing with the minimum number of operations. In one operation, you do the following:

  • Choose an index \(1 \leq i \leq n\),
  • Set \(a_i = a_i \cdot 2\).

An array \(b_1, b_2, \ldots, b_n\) is non-decreasing if \(b_i \leq b_{i+1}\) for all \(1 \leq i < n\).

Input

Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. This is followed by their description.

The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)) — the size of the array \(a\).

The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).

Output

For each test case, output the minimum number of operations needed to make the array non-decreasing.

log2 函数的使用。

// The code was written by yifan, and yifan is neutral!!!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define bug puts("NOIP rp ++!");
#define rep(i, a, b, c) for (int i = (a); i <= (b); i += (c))
#define per(i, a, b, c) for (int i = (a); i >= (b); i -= (c))

template<typename T>
inline T read() {
    T x = 0;
    bool fg = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        fg |= (ch == '-');
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    return fg ? ~x + 1 : x;
}

template<typename T>
void write(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

template<typename T>
void print(T x, char c) {
   write(x);
   putchar(c);
}

const int N = 1e5 + 5;

using ull = unsigned long long;
using db = double;

const db eps = 1e-11;

int T, n;
db _2[N];
ull a[N];

void solve() {
    n = read<int>();
    ll cnt = 0;
    _2[1] = 0;
    rep (i, 1, n, 1) {
        a[i] = read<ll>();
        _2[i] = log2(1.0 * a[i]);
    }
    rep (i, 2, n, 1) {
        if (_2[i - 1] - _2[i] > eps) {
            db cha = _2[i - 1] - _2[i];
            ll chatmp = cha;
            if (cha - chatmp <= eps) {
                cnt += chatmp;
                _2[i] += chatmp;
            } else {
                cnt += chatmp + 1;
                _2[i] += chatmp + 1;
            }
        }
    }
    print(cnt, '\n');
}

int main() {
    T = read<int>();
    while (T --) {
        solve();
    }
    return 0;
}
posted @ 2023-10-23 20:52  yi_fan0305  阅读(250)  评论(2编辑  收藏  举报