首页 > 其他分享 >「解题报告」Codeforces Round 905 (Div. 3)

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

时间:2023-10-23 21:01:07浏览次数:36  
标签:10 ch 905 int rep Codeforces leq test Div

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

标签:10,ch,905,int,rep,Codeforces,leq,test,Div
From: https://www.cnblogs.com/yifan0305/p/17781386.html

相关文章

  • Codeforces Round 905 (Div. 2) C. You Are So Beautiful(数据结构)
    CodeforcesRound905(Div.2)C.YouAreSoBeautiful定义:设数组abcd子数组定义:从原数组砍去前面若干元素,后边若干元素,剩余的数组。如:bc、ab子序列定义:从原数组删除若干元素,剩余元素拼凑一起,组成的数组。如:ac、bd思路:作为结果的连续子数组,如果他为[\(a_l\),……,\(a_......
  • Educational Codeforces Round 144(CF1796 D~E) 题解
    D.MaximumSubarray题目链接十分钟秒了一道*2000,非常爽,但是个人感觉确实非常简单。下面令\(b_i=a_{i}-x\),\(b_i'=a_i+x\)。因为\(k<=20\),因此考虑一个复杂度与\(k\)相关的做法。不妨dp:设\(f_{i,j,0/1}\)表示考虑了前\(i\)个数,对\(j\)个数加上了\(x\),第\(i\)......
  • Codeforces Round 905 (Div. 2)
    目录写在前面ABCD1/D2E写在最后写在前面比赛地址:https://codeforces.com/contest/1884oonp这场div2怎么才2k5人打啊我草里面还不知道多少大神的小号,呃呃打了1k3掉了75分也是牛逼A考虑如何拼出一个长度为\(n-k\)的回文串,先一对一对地拼,再看需不需要再顶上去一个......
  • Codeforces Round 905 - div.3(A B C D E)
    目录CodeforcesRound905(Div.3)A.MorningB.ChemistryC.RaspberriesCodeforcesRound905(Div.3)A.Morning模拟光标移动即可voidsolve(){ stringss; cin>>ss; charch='1'; intans=0; for(autoc:ss){ if(c!=ch){ intx=c,y=c......
  • Codeforces Round 904 (Div. 2)
    目录写在前面ABCD写在最后写在前面比赛地址:https://codeforces.com/contest/1884捏吗我不会容斥,我是飞舞。A\(k\le10\),于是直接枚举\(x\simx+20\)检查即可。///*By:Luckyblock*/#include<bits/stdc++.h>#defineLLlonglong//=================================......
  • Codeforces Round 855 (Div. 3) C. Powering the Hero
    有\(n\)张卡的卡堆,你可以自顶向下抽卡。装备卡显示数值为\(a_i(a_i>0)\),英雄卡显示数值为\(a_i=0\)。如果是装备卡,你可以将卡抽出放在装备堆。如果是英雄卡,你可以将装备堆顶端的一张数值为\(x\)的装备卡装备给英雄,英雄数值\(+x\)。无论是否装备,都加入英雄堆。询问......
  • Codeforces Round 857 (Div. 2) B. Settlement of Guinea Pigs
    你非常喜欢豚鼠。每个笼子可以住两只豚鼠,且你不想把每个笼子放入不同性别的豚鼠。豚鼠只有两种性别。假设你买到豚鼠时并不知道性别,只有医生能够分辨。接下来的\(n\)天方案中,若第\(i\)天为\(1\),你买入一只豚鼠;若为\(2\),你请医生分辨你的豚鼠性别。给出方案,你最少需要准......
  • Nebius Welcome Round (Div. 1 + Div. 2) B. Vaccination
    你管理一个疫苗接种站,将会有\(n\)个人前来接种疫苗。第\(i\)个到来的人时间为\(t_i\),已知每个人的等待时间不会超过\(w\)分钟。疫苗存放在特质冰箱中,一袋疫苗有\(k\)支,当一袋疫苗在\(x\)时刻打开时,它的有效时间为\(d\)。现在询问最少需要打开多少袋疫苗满足\(n\)......
  • Educational Codeforces Round 145 (Rated for Div. 2) B. Points on Plane
    给一个二维平面,你需要在上面放\(n\)个芯片。将一个芯片放在\((x,y)\)的代价为\(|x|+|y|\)。放\(n\)个代价的代价为,所有芯片中耗费的最大代价。并且你需要保证任意两个芯片之间的距离严格\(>1\)。现在给出\(n\)给芯片,询问放\(n\)个芯片的最小代价。一:不难想到......
  • 使一个div垂直+水平居中的几种方法
    使一个div垂直+水平居中的几种方法思路1:绝对定位居中(原始版)思路2:绝对定位居中(改进版之一)思路3:绝对定位居中(改进版之二)思路4:flex布局居中思路1:绝对定位居中(原始版)这个是我回答出来的,也是被各位所熟知的一种方法,设外层div相对定位,内层div绝对定位,top、left分别设为50%,然后通过设置m......