codeforces: add solutions for round 923

This commit is contained in:
Ivan R. 2024-02-07 01:39:43 +05:00
parent c8a967dcaa
commit dea3af5d33
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C
11 changed files with 416 additions and 0 deletions

1
codeforces/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
main

View file

@ -0,0 +1,3 @@
# Round 923
[Contest](https://codeforces.com/contest/1927/)

View file

@ -0,0 +1,5 @@
all:
g++ main.cpp -o main
run:
./main

View file

@ -0,0 +1,29 @@
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
size_t n;
cin >> n;
std::string buffer;
cin >> buffer;
int first_b = -1;
int last_b = -1;
for (int j = 0; j < n; j++) {
if (buffer[j] == 'B') {
if (first_b == -1) {
first_b = j;
}
last_b = j;
}
}
cout << last_b - first_b + 1 << endl;
}
}

View file

@ -0,0 +1,62 @@
# A. Make it White
[Codeforces.com](https://codeforces.com/contest/1927/problem/A)
You have a horizontal strip of n cells. Each cell is either white or black.
You can choose a continuous segment of cells once and paint them all white. After this action, all the black cells in this segment will become white, and the white ones will remain white.
What is the minimum length of the segment that needs to be painted white in order for all n cells to become white?
## Input
The first line of the input contains a single integer t
(1 ≤ t ≤ 104) — the number of test cases. The descriptions of the test cases follow.
The first line of each test case contains a single integer n
(1 ≤ n ≤ 10) — the length of the strip.
The second line of each test case contains a string s, consisting of n characters,
each of which is either 'W' or 'B'. The symbol 'W' denotes a white cell, and 'B' — a black one.
It is guaranteed that at least one cell of the given strip is black.
## Output
For each test case, output a single number — the minimum length of a continuous segment of cells that needs to be painted white in order for the entire strip to become white.
## Example
Input
```
8
6
WBBWBW
1
B
2
WB
3
BBW
4
BWWB
6
BWBWWB
6
WWBBWB
9
WBWBWWWBW
```
Output
```
4
1
1
2
4
6
4
7
```

View file

@ -0,0 +1,5 @@
all:
g++ main.cpp -o main
run:
./main

View file

@ -0,0 +1,41 @@
#include <iostream>
using namespace std;
string alph = "abcdefghijklmnopqrstuvwxyz";
int main() {
int t;
cin >> t;
int used_chars[26];
for (int k = 0; k < t; k++) {
int n;
cin >> n;
int* s = new int[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < 26; i++) {
used_chars[i] = 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 26; j++) {
if (used_chars[j] == s[i]) {
cout << alph[j];
used_chars[j]++;
break;
}
}
}
cout << endl;
delete[] s;
}
}

View file

@ -0,0 +1,61 @@
# B. Following the String
[Codeforces.com](https://codeforces.com/contest/1927/problem/B)
Polycarp lost the string s of length n consisting of lowercase Latin letters, but he still has its trace.
The trace of the string s
is an array a of n integers, where ai is the number of such indices j (j<i) that si=sj. For example, the trace of the string abracadabra is the array `[0,0,0,1,0,2,0,3,1,1,4]`.
Given a trace of a string, find any string s from which it could have been obtained. The string s should consist only of lowercase Latin letters a-z.
## Input
The first line of the input contains a single integer t
(1 ≤ t ≤ 10^4) — the number of test cases. Then the descriptions of the test cases follow.
The first line of each test case contains a single integer n
(1 ≤ n ≤ 2*10^5) — the length of the lost string.
The second line of each test case contains n
integers a1,a2,…,an (0 ≤ ai < n) the trace of the string. It is guaranteed that for the given trace, there exists a suitable string s.
It is guaranteed that the sum of n over all test cases does not exceed 2*10^5.
## Output
For each test case, output a string s
that corresponds to the given trace. If there are multiple such strings s, then output any of them.
The string s should consist of lowercase Latin letters a-z.
It is guaranteed that for each test case, a valid answer exists.
## Example
Input
```
5
11
0 0 0 1 0 2 0 3 1 1 4
10
0 0 0 0 0 1 0 1 1 0
1
0
8
0 1 2 3 4 5 6 7
8
0 0 0 0 0 0 0 0
```
Output
```
abracadabra
codeforces
a
aaaaaaaa
dijkstra
```

View file

@ -0,0 +1,5 @@
all:
g++ main.cpp -o main
run:
./main

View file

@ -0,0 +1,85 @@
#include <iostream>
using namespace std;
const int UNKN = -1;
const int NF = -2;
int main() {
int t;
cin >> t;
for (int y = 0; y < t; y++) {
int len;
cin >> len;
int* arr = new int[len];
int* cache = new int[len];
for (int i = 0; i < len; i++) {
cin >> arr[i];
cache[i] = UNKN;
}
int q_num;
cin >> q_num;
for (int v = 0; v < q_num; v++) {
int l, r;
cin >> l >> r;
l--;
r--;
if (cache[l] == NF) {
// Every number will be the same.
cout << "-1 -1" << endl;
} else if (cache[l] != UNKN) {
// Cache hit.
if (cache[l] > r) {
// Another number is beyond our range;
cout << "-1 -1" << endl;
} else {
// Answer was cached
cout << l + 1 << " " << cache[l] + 1 << endl;
}
} else {
bool found = false;
for (int i = l + 1; i <= r; i++) {
if (arr[i] != arr[l]) {
// Different number was found
cout << l+1 << " " << i+1 << endl;
found = true;
cache[l] = i;
break;
}
if (cache[i] != UNKN) {
// CACHE HIT
if (cache[i] > r || cache[i] == NF) {
// Not found
break;
} else {
// Answer was cached
cout << l + 1 << " " << cache[i] + 1 << endl;
found = true;
cache[l] = cache[i];
break;
}
}
}
if (!found) {
// Nothing was found
cout << "-1 -1" << endl;
if (r == len - 1) {
// If we reached the end of our array and nothing was found,
// remember this.
cache[l] = NF;
}
}
}
}
delete[] cache;
delete[] arr;
}
}

View file

@ -0,0 +1,119 @@
# D. Find the Different Ones!
[Codeforces.com](https://codeforces.com/contest/1927/problem/D)
You are given an array a of n integers, and q queries.
Each query is represented by two integers l
and r (1≤l≤r≤n). Your task is to find, for each query, two indices i and j (or determine that they do not exist) such that:
- l ≤ i ≤ r
- l ≤ j ≤ r
- ai ≠ aj
In other words, for each query, you need to find a pair of different elements among al,al+1,…,ar, or report that such a pair does not exist.
## Input
The first line of the input contains a single integer t
(1≤t≤104) — the number of test cases. The descriptions of the test cases follow.
The first line of each test case contains a single integer n
(2≤n≤2⋅105) — the length of the array a.
The second line of each test case contains n
integers a1,a2,…,an (1≤ai≤106) — the elements of the array a.
The third line of each test case contains a single integer q
(1≤q≤2⋅105) — the number of queries.
The next q lines contain two integers each, l and r (1≤l<rn) the boundaries of the query.
It is guaranteed that the sum of the values of n across all test cases does not exceed 2⋅105. Similarly, it is guaranteed that the sum of the values of q across all test cases does not exceed 2⋅105.
## Output
For each query, output two integers separated by space: i and j (l≤i,j≤r), for which ai≠aj. If such a pair does not exist, output i=1 and j=1.
You may separate the outputs for the test cases with empty lines. This is not a mandatory requirement.
## Example
Input
```
5
5
1 1 2 1 1
3
1 5
1 2
1 3
6
30 20 20 10 10 20
5
1 2
2 3
2 4
2 6
3 5
4
5 2 3 4
4
1 2
1 4
2 3
2 4
5
1 4 3 2 4
5
1 5
2 4
3 4
3 5
4 5
5
2 3 1 4 2
7
1 2
1 4
1 5
2 4
2 5
3 5
4 5
```
Output
```
2 3
-1 -1
1 3
2 1
-1 -1
4 2
4 6
5 3
1 2
1 2
2 3
3 2
1 3
2 4
3 4
5 3
5 4
1 2
4 2
1 3
2 3
3 2
5 4
5 4
```