Este artigo detalha as soluções para os problemas do Codeforces Round 913 (Divisão 3), com implementações em C++ otimizadas e adaptadas.
A. Rook
Dada a posição inicial de uma torre em um tabuleiro de xadrez, determine todos os movimentos possíveis.
#include <bits/stdc++.h>
using namespace std;
void resolver() {
string pos;
cin >> pos;
char col = pos[0];
int lin = pos[1] - '0';
for (int i = 1; i <= 8; i++) {
if (i != lin) cout << col << i << "\n";
}
for (char c = 'a'; c <= 'h'; c++) {
if (c != col) cout << c << lin << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int testes;
cin >> testes;
while (testes--) resolver();
return 0;
}
B. YetnotherrokenKeoard
Processe uma string com backspaces específicos para letras minúsculas e maiúsculas, produzindo a string final.
#include <bits/stdc++.h>
using namespace std;
void resolver() {
string texto;
cin >> texto;
deque<pair<int, char>> lower, upper;
for (int i = 0; i < texto.size(); i++) {
if (texto[i] == 'b') {
if (!lower.empty()) lower.pop_back();
} else if (texto[i] == 'B') {
if (!upper.empty()) upper.pop_back();
} else if (islower(texto[i])) {
lower.push_back({i, texto[i]});
} else {
upper.push_back({i, texto[i]});
}
}
while (!lower.empty() && !upper.empty()) {
if (lower.front().first < upper.front().first) {
cout << lower.front().second;
lower.pop_front();
} else {
cout << upper.front().second;
upper.pop_front();
}
}
while (!lower.empty()) {
cout << lower.front().second;
lower.pop_front();
}
while (!upper.empty()) {
cout << upper.front().second;
upper.pop_front();
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int testes;
cin >> testes;
while (testes--) resolver();
return 0;
}
C. Removal of Unattractive Pairs
Determine o comprimento mínimo da string após remover pares adjacentes de caracteres diferenets.
#include <bits/stdc++.h>
using namespace std;
const int TAM = 2e5 + 10;
int freq[26];
void resolver() {
memset(freq, 0, sizeof freq);
int n;
string s;
cin >> n >> s;
for (char c : s) freq[c - 'a']++;
int maxf = *max_element(freq, freq + 26);
if (maxf <= n / 2) {
cout << n % 2 << "\n";
} else {
cout << maxf * 2 - n << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int testes;
cin >> testes;
while (testes--) resolver();
return 0;
}
D. Jumping Through Segments
Encontre o valor mínimo de k para que uma sequência de segmentos possa ser percorrida com passsos limitados.
#include <bits/stdc++.h>
using namespace std;
const int TAM = 2e5 + 10;
int esq[TAM], dir[TAM];
int n;
bool verificar(int mid) {
int l = 0, r = 0;
for (int i = 1; i <= n; i++) {
l = max(l - mid, 0LL);
r += mid;
if (esq[i] > r || dir[i] < l) return false;
l = max(l, esq[i]);
r = min(r, dir[i]);
}
return true;
}
void resolver() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> esq[i] >> dir[i];
int baixo = 0, alto = 1e18;
int resp = 1e18;
while (baixo <= alto) {
int mid = (baixo + alto) / 2;
if (verificar(mid)) {
alto = mid - 1;
resp = mid;
} else {
baixo = mid + 1;
}
}
cout << resp << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int testes;
cin >> testes;
while (testes--) resolver();
return 0;
}
E. Good Triples
Conte o número de triplas de dígitos que somam a cada dígito de um número sem acarrretar.
#include <bits/stdc++.h>
using namespace std;
int contagem[20];
void precomputar() {
memset(contagem, 0, sizeof contagem);
for (int i = 0; i < 10; i++) {
for (int k = 0; k + i < 10; k++) {
for (int j = 0; j + k + i < 10; j++) {
contagem[i + k + j]++;
}
}
}
}
void resolver() {
int n;
cin >> n;
string digitos = to_string(n);
int res = 1;
for (char d : digitos) {
res *= contagem[d - '0'];
}
cout << res << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
precomputar();
int testes;
cin >> testes;
while (testes--) resolver();
return 0;
}
F. Shift and Reverse
Encontre a sequência ordenada mínima usando operações de deslocamento e inversão em um array circular.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
const int INF = 1e9;
int testes;
cin >> testes;
while (testes--) {
int n;
cin >> n;
vector<int> vet(2 * n);
for (int i = 0; i < n; i++) {
cin >> vet[i];
vet[i + n] = vet[i];
}
int ans = INF;
for (int i = 0; i < n; i++) {
int j = i;
while (j + 1 < 2 * n && vet[j + 1] >= vet[j]) j++;
for (int k = i; k <= j && k < n; k++) {
int len = j - k + 1;
if (len >= n) {
ans = min(ans, (n - k) % n);
int rev = n - 1 - k;
ans = min(ans, n - 1 - rev + 2);
}
}
i = j;
}
for (int i = 0; i < n; i++) {
int j = i;
while (j + 1 < 2 * n && vet[j + 1] <= vet[j]) j++;
for (int k = i; k <= j && k < n; k++) {
int len = j - k + 1;
if (len >= n) {
ans = min(ans, (n - k) % n + 1);
int rev = n - 1 - k;
ans = min(ans, n - 1 - rev + 1);
}
}
i = j;
}
if (ans > INF / 2) ans = -1;
cout << ans << "\n";
}
return 0;
}
G. Lights
Determine o conjunto mínimo de interruptores para desligar todas as luzes em um grafo direcionado.
#include <bits/stdc++.h>
using namespace std;
const int TAM = 1e5 + 10;
int dest[TAM], grau[TAM];
bool vis[TAM], luz[TAM], used[TAM];
int n;
void resolver() {
cin >> n;
string est;
cin >> est;
for (int i = 0; i < n; i++) {
luz[i + 1] = (est[i] == '1');
vis[i + 1] = false;
used[i + 1] = false;
grau[i + 1] = 0;
}
for (int i = 1; i <= n; i++) {
cin >> dest[i];
grau[dest[i]]++;
}
queue<int> fila;
for (int i = 1; i <= n; i++) {
if (grau[i] == 0) fila.push(i);
}
vector<int> resp;
while (!fila.empty()) {
int u = fila.front();
fila.pop();
if (luz[u]) {
luz[u] ^= 1;
luz[dest[u]] ^= 1;
vis[u] = true;
resp.push_back(u);
}
if (--grau[dest[u]] == 0) {
fila.push(dest[u]);
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i] && grau[i] > 0) {
int u = 0;
vector<vector<int>> grp(2);
for (int j = i; !used[j]; j = dest[j]) {
u ^= (luz[j] ? 1 : 0);
grp[u].push_back(j);
used[j] = true;
}
if (u != 0) {
cout << -1 << "\n";
return;
}
if (grp[0].size() < grp[1].size()) {
for (int x : grp[0]) {
vis[x] = true;
resp.push_back(x);
}
} else {
for (int x : grp[1]) {
vis[x] = true;
resp.push_back(x);
}
}
}
}
cout << resp.size() << "\n";
for (int u : resp) cout << u << " ";
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int testes;
cin >> testes;
while (testes--) resolver();
return 0;
}