A. Make it White
思路:从前向后找到第一次出现'B‘的位置 l ,从后往前找到第一次出现'B'的位置 r ,答案就是 r-l+1.
#include <bits/stdc++.h>
using namespace std;
// #pragma G++ optimize(2)
#define int long long
#define double long double
#define xiaowen ac
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 4e6 + 7;
const int P = 131;
string s;
int n;
void solve()
{
cin >> n;
cin >> s;
bool ok = false;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == 'B')
{
ok = true;
break;
}
}
if (ok)
{
int l = 0, r = s.length() - 1;
while (l < s.length() && s[l] == 'W')
{
l++;
}
while (r >= l && s[r] == 'W')
{
r--;
}
int res = r - l + 1;
cout << res << '\n';
}
else
{
cout << "0" << '\n';
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
// T = 1;
while (T--)
{
solve();
}
return 0;
}
B. Following the String
思路:用一个map储存每个字符出现的频率,然后枚举模拟即可
#include <bits/stdc++.h>
using namespace std;
// #pragma G++ optimize(2)
#define int long long
#define double long double
#define xiaowen ac
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 4e6 + 7;
const int P = 131;
int n;
int a[N];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
map<char, int> mp;
string res;
for (int i = 1; i <= n; i++)
{
int x = a[i];
for (char ch = 'a'; ch <= 'z'; ch++)
{
if (mp[ch] == x)
{
res.push_back(ch);
mp[ch]++;
break;
}
}
}
cout << res << '\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
// T = 1;
while (T--)
{
solve();
}
return 0;
}
C. Choose the Different Ones!
思路:数组A中<=k且只出现在A中的元素数量记作cnta,数组B中<=k且只出现在B中的元素记作cntb,
如果cnta>k/2 或者 cntb > k/2,就输出"NO",否则判断两个数组中的元素是否可以组成1~k中的每个数,如果可以输出"YES“,否则输出"NO".
#include <bits/stdc++.h>
using namespace std;
// #pragma G++ optimize(2)
#define int long long
#define double long double
#define xiaowen ac
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 4e6 + 7;
const int P = 131;
int n, m, k;
int a[N];
int b[N];
void solve()
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
for (int i = 1; i <= m; i++)
{
cin >> b[i];
}
int c = k / 2;
set<int> sa, sb;
for (int i = 1; i <= n; i++)
{
if (a[i] <= k)
{
sa.insert(a[i]);
}
}
for (int i = 1; i <= m; i++)
{
if (b[i] <= k)
{
sb.insert(b[i]);
}
}
map<int, int> mpa, mpb;
for (auto it = sa.begin(); it != sa.end(); it++)
{
int x = (*it);
// cout << "SA " << x << "\n";
mpa[x] = 1;
}
for (auto it = sb.begin(); it != sb.end(); it++)
{
int x = (*it);
// cout << "SB " << x << '\n';
mpb[x] = 1;
}
int ca = 0, cb = 0;
for (auto it = sb.begin(); it != sb.end(); it++)
{
int x = (*it);
if (mpa[x] == 0)
{
cb++;
}
}
for (auto it = sa.begin(); it != sa.end(); it++)
{
int x = (*it);
if (mpb[x] == 0)
{
ca++;
}
}
// cout << "KK " << ca << " " << cb << '\n';
if (ca > c || cb > c)
{
cout << "NO" << '\n';
}
else
{
map<int, int> mpk;
for (auto it = sa.begin(); it != sa.end(); it++)
{
int x = (*it);
mpk[x] = 1;
}
for (auto it = sb.begin(); it != sb.end(); it++)
{
int x = (*it);
mpk[x] = 1;
}
if (mpk.size() != k)
{
cout << "NO" << '\n';
}
else
{
cout << "YES" << '\n';
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
// T = 1;
while (T--)
{
solve();
}
return 0;
}
D. Find the Different Ones!
思路:一开始想的是如何预处理然后O(1)的判断,但是后来发现行不通。(因此一个小时就过去了),但是在思考的过程中,发现这个题可以用二分来解决。(发现的那一瞬间很有成就感)
二分思路:从前向后遍历,当发现a[i] != a[i+1]时将下标 i 加入vector中,因此vector中的元素就是有序的啦。对于要查询的 l 和 r,如果vector是空的,就输出-1,否则二分找到第一个大于等于 l 的下标 i,如果这个下标 i >= r,就输出-1(因为我们要保证 i 和 i+1 都要属于 l 到 r),否则 i 和 i+1 就一定属于 l 到 r.二分前还应该确保vector中至少存在一个 i >= l , 因此就有了代码中的 (v[res] < l )那一段。
#include <bits/stdc++.h>
using namespace std;
// #pragma G++ optimize(2)
#define int long long
#define double long double
#define xiaowen ac
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 4e6 + 7;
const int P = 131;
int n, a[N], q;
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
cin >> q;
vector<int> v;
for (int i = 1; i + 1 <= n; i++)
{
if (a[i] != a[i + 1])
{
v.push_back(i);
}
}
while (q--)
{
int l, r;
cin >> l >> r;
if (v.size() == 0)
{
cout << "-1"
<< " "
<< "-1" << '\n';
}
else
{
int res = v.size() - 1;
int left = 0, right = v.size() - 1;
if (v[res] < l)
{
cout << "-1"
<< " "
<< "-1" << '\n';
}
else
{
while (left <= right)
{
int mid = (left + right) / 2;
if (v[mid] < l)
{
left = mid + 1;
}
else
{
right = mid - 1;
res = min(res, mid);
}
}
if (v[res] >= r)
{
cout << "-1"
<< " "
<< "-1" << '\n';
}
else
{
cout << v[res] << " " << v[res] + 1 << '\n';
}
}
}
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
// T = 1;
while (T--)
{
solve();
}
return 0;
}