「解题报告」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 (1t104) - 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 (1t104) — 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 (0k<n105) — 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 2105.

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 个桶,然后先取偶数对元素,最后判断偶数对元素的总个数是否大于等于 nk+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 a1,a2,,an and a number k (2k5). In one operation, you can do the following:

  • Choose an index 1in,
  • Set ai=ai+1.

Find the minimum number of operations needed to make the product of all the numbers in the array a1a2an divisible by k.

Input

Each test consists of multiple test cases. The first line contains a single integer t (1t104) — 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 (2n105, 2k5) — the size of the array a and the number k.

The second line of each test case contains n integers a1,a2,,an (1ai10).

It is guaranteed that the sum of n over all test cases does not exceed 2105.

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 lxr and axb.

Input

The first line of each test case contains an integer q (1q105) — 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 (1lr109).

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 a1,a2,,an. You need to make it non-decreasing with the minimum number of operations. In one operation, you do the following:

  • Choose an index 1in,
  • Set ai=ai2.

An array b1,b2,,bn is non-decreasing if bibi+1 for all 1i<n.

Input

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

The first line of each test case contains an integer n (1n105) — the size of the array a.

The second line of each test case contains n integers a1,a2,,an (1ai109).

It is guaranteed that the sum of n over all test cases does not exceed 2105.

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;
}

作者:yifan0305

出处:https://www.cnblogs.com/yifan0305/p/17781386.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载时还请标明出处哟!

posted @   yi_fan0305  阅读(312)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
more_horiz
keyboard_arrow_up light_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示