From dea3af5d33d83b68d4e45237483ad412081e41e6 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Wed, 7 Feb 2024 01:39:43 +0500 Subject: [PATCH] codeforces: add solutions for round 923 --- codeforces/.gitignore | 1 + codeforces/round-923/readme.md | 3 + codeforces/round-923/task-a/Makefile | 5 ++ codeforces/round-923/task-a/main.cpp | 29 +++++++ codeforces/round-923/task-a/readme.md | 62 ++++++++++++++ codeforces/round-923/task-b/Makefile | 5 ++ codeforces/round-923/task-b/main.cpp | 41 +++++++++ codeforces/round-923/task-b/readme.md | 61 +++++++++++++ codeforces/round-923/task-d/Makefile | 5 ++ codeforces/round-923/task-d/main.cpp | 85 ++++++++++++++++++ codeforces/round-923/task-d/readme.md | 119 ++++++++++++++++++++++++++ 11 files changed, 416 insertions(+) create mode 100644 codeforces/.gitignore create mode 100644 codeforces/round-923/readme.md create mode 100644 codeforces/round-923/task-a/Makefile create mode 100644 codeforces/round-923/task-a/main.cpp create mode 100644 codeforces/round-923/task-a/readme.md create mode 100644 codeforces/round-923/task-b/Makefile create mode 100644 codeforces/round-923/task-b/main.cpp create mode 100644 codeforces/round-923/task-b/readme.md create mode 100644 codeforces/round-923/task-d/Makefile create mode 100644 codeforces/round-923/task-d/main.cpp create mode 100644 codeforces/round-923/task-d/readme.md diff --git a/codeforces/.gitignore b/codeforces/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/codeforces/.gitignore @@ -0,0 +1 @@ +main diff --git a/codeforces/round-923/readme.md b/codeforces/round-923/readme.md new file mode 100644 index 0000000..5ac6055 --- /dev/null +++ b/codeforces/round-923/readme.md @@ -0,0 +1,3 @@ +# Round 923 + +[Contest](https://codeforces.com/contest/1927/) diff --git a/codeforces/round-923/task-a/Makefile b/codeforces/round-923/task-a/Makefile new file mode 100644 index 0000000..a40930b --- /dev/null +++ b/codeforces/round-923/task-a/Makefile @@ -0,0 +1,5 @@ +all: + g++ main.cpp -o main + +run: + ./main diff --git a/codeforces/round-923/task-a/main.cpp b/codeforces/round-923/task-a/main.cpp new file mode 100644 index 0000000..2bfe8d2 --- /dev/null +++ b/codeforces/round-923/task-a/main.cpp @@ -0,0 +1,29 @@ +#include + +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; + } +} diff --git a/codeforces/round-923/task-a/readme.md b/codeforces/round-923/task-a/readme.md new file mode 100644 index 0000000..8184bc5 --- /dev/null +++ b/codeforces/round-923/task-a/readme.md @@ -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 +``` diff --git a/codeforces/round-923/task-b/Makefile b/codeforces/round-923/task-b/Makefile new file mode 100644 index 0000000..a40930b --- /dev/null +++ b/codeforces/round-923/task-b/Makefile @@ -0,0 +1,5 @@ +all: + g++ main.cpp -o main + +run: + ./main diff --git a/codeforces/round-923/task-b/main.cpp b/codeforces/round-923/task-b/main.cpp new file mode 100644 index 0000000..f15d564 --- /dev/null +++ b/codeforces/round-923/task-b/main.cpp @@ -0,0 +1,41 @@ +#include + +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; + } +} diff --git a/codeforces/round-923/task-b/readme.md b/codeforces/round-923/task-b/readme.md new file mode 100644 index 0000000..210ca2d --- /dev/null +++ b/codeforces/round-923/task-b/readme.md @@ -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 + +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; + } +} diff --git a/codeforces/round-923/task-d/readme.md b/codeforces/round-923/task-d/readme.md new file mode 100644 index 0000000..4c547d9 --- /dev/null +++ b/codeforces/round-923/task-d/readme.md @@ -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