Educational Codeforces Round 116 (Rated for Div. 2)

A. AB Balance

You are given a string s of length n consisting of characters a and/or b.

Let AB(s) be the number of occurrences of string ab in s as a substring. Analogically, BA(s) is the number of occurrences of ba in s as a substring.

In one step, you can choose any index i and replace si with character a or b.

What is the minimum number of steps you need to make to achieve AB(s)=BA(s)?

Reminder:

The number of occurrences of string d in s as substring is the number of indices i (1≤i≤|s|−|d|+1) such that substring sisi+1…si+|d|−1 is equal to d. For example, AB(aabbbabaa)=2 since there are two indices i: i=2 where aabbbabaa and i=6 where aabbbabaa.

题解

你会发现,ab与ba个数相差最多为1
找到第一个多余的打掉就好了

AC

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int, int>
#define psi pair<string, int>
#define ull unsigned ll
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define ld long double
const int N = 1E5 + 7;
#define INF ~0ULL
int t;
string s;
int len;
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> s;
        len = s.length();
        int ab = 0;
        int ba = 0;
        for (int i = 1; i < len; i++)
        {
            if (s[i - 1] == 'a' && s[i] == 'b')
                ab++;
            if (s[i - 1] == 'b' && s[i] == 'a')
                ba++;
        }
        if (ab == ba)
        {
            cout << s << endl;
        }
        else if (ab > ba)
        {
            auto it = s.find('a');
            s[it] = 'b';
            cout << s << endl;
        }
        else
        {
            auto it = s.find('b');
            s[it] = 'a';
            cout << s << endl;
        }
       // cout << ab << " " << ba << endl;
    }
}

B. Update Files

Berland State University has received a new update for the operating system. Initially it is installed only on the 1-st computer.

Update files should be copied to all n computers. The computers are not connected to the internet, so the only way to transfer update files from one computer to another is to copy them using a patch cable (a cable connecting two computers directly). Only one patch cable can be connected to a computer at a time. Thus, from any computer where the update files are installed, they can be copied to some other computer in exactly one hour.

Your task is to find the minimum number of hours required to copy the update files to all n computers if there are only k patch cables in Berland State University.

模拟

AC

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int, int>
#define psi pair<string, int>
#define ull unsigned ll
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define ld long double
const int N = 1E5 + 7;
#define INF ~0ULL
ll cc[65];
ll n, k;
void init()
{
    cc[0] = 1;
    for (int i = 1; i < 63; i++)
    {
        cc[i] = cc[i - 1] * 2;
    }
}
int main()
{
    init();
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        if (n == 1)
        {
            cout << 0 << endl;
        }
        else
        {
            ll inde = 0;
            ll ans = 0;
            n -= 1;
            while (n)
            {
                if (cc[inde] >= k)
                {
                    ans += (n + k - 1) / k;
                    cout << ans << endl;
                    break;
                }
                else if (cc[inde] <= k)
                {
                    n -= cc[inde];
                    inde++;
                    ans++;
                }
                if (n <= 0)
                {
                    cout << ans << endl;
                    break;
                }
            }
        }
    }
}

C. Banknotes

In Berland, n different types of banknotes are used. Banknotes of the i-th type have denomination 10ai burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly 1.

Let’s denote f(s) as the minimum number of banknotes required to represent exactly s burles. For example, if the denominations of banknotes used in Berland are 1, 10 and 100, then f(59)=14: 9 banknotes with denomination of 1 burle and 5 banknotes with denomination of 10 burles can be used to represent exactly 9⋅1+5⋅10=59 burles, and there’s no way to do it with fewer banknotes.

For a given integer k, find the minimum positive number of burles s that cannot be represented with k or fewer banknotes (that is, f(s)>k).

思路

模拟,看代码吧

AC

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int, int>
#define psi pair<string, int>
#define ull unsigned ll
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define ld long double
const int N = 1E5 + 7;
#define INF ~0ULL
ll n, k;
ll arr[12];
ll cc[30];
void init()
{
    cc[0] = 1;
    for (int i = 1; i <= 32; i++)
    {
        cc[i] = cc[i - 1] * 10;
    }
}
int main()
{
    init();
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        k++;
        for (int i = 1; i <= n; i++)
        {
            cin >> arr[i];
        }
        ll ans = 0;
        for (int i = 1; i <= n; i++)
        {
            ans += min(k, cc[arr[i + 1] - arr[i]] - 1) * cc[arr[i]];
            k -= min(k, cc[arr[i + 1] - arr[i]] - 1);
        }
        ans+= k * cc[arr[n]];
        cout<<ans<<endl;
    }
}
关于Java和Redis面试题,你可以参考以下资源: 1. "Java基础教程(入门篇)"这本书中可能包含与Java和Redis相关的基础知识点,例如如何连接和操作Redis以及在Java中使用Redis的常见场景。 2. "java面试大集合"这本书中可能包含Java和Redis面试题,涵盖了Java技术栈以及与Redis相关的问题。你可以浏览这本书中的相关章节以寻找你感兴趣的Java和Redis面试题。 3. "Java基础教程(进阶篇)"这本书可能包含更深入的Java和Redis面试题,例如Java高并发和如何在Java中使用Redis进行缓存。 这些资源可能会给你提供一些有关Java和Redis面试题的参考。你可以根据自己的需求和兴趣选择适合的资源进行学习。希望这些资源能帮助到你。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [redis面试题总结(附答案)](https://blog.csdn.net/guorui_java/article/details/117194603)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [java面试大集合一共485页](https://download.csdn.net/download/wm9028/88268176)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.0-0.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值