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