#include <cassert> #include <iostream> #include <istream> #include <fstream> #include <algorithm> #include <cstdio> #include <complex> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> #include <string> #include <cstdlib> #include <memory> #include <ctime> #include <bitset> #include <queue> #include <stack> #include <unordered_map> #include <unordered_set> #include <bitset> using namespace std; class Mro2024 { int n; std::string inputString; vector<int> vec; public: std::vector<int> countChanges(std::vector<int>& arr) { int n = arr.size(); std::vector<int> changes(n, 0); for (int i = 0; i < n; ++i) { for (int j = 0; j < n - i - 1; ++j) { if (arr[j] > arr[j + 1]) { std::swap(arr[j], arr[j + 1]); // Increment change count for both elements changes[j]++; changes[j + 1]++; } } } return changes; } void virtual Solve() { //how to create a vector of zeros: cin >> n; //how to read a string from input: cin >> inputString; //initialize the vec with n zeros: vec = vector<int>(n, 0); //set in vector 1 if the inputString[i] == 'P': for (int i = 0; i < n; i++) { if (inputString[i] == 'P') { vec[i] = 1; } } std::vector<int> changes = countChanges(vec); //how to print all numbers from the changes by adding space between them: for (int i = 0; i < changes.size(); i++) { cout << changes[i] << " "; } std::cout << std::endl; } }; int main() { Mro2024 *p = new Mro2024(); p->Solve(); delete p; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <cassert> #include <iostream> #include <istream> #include <fstream> #include <algorithm> #include <cstdio> #include <complex> #include <vector> #include <set> #include <map> #include <cmath> #include <queue> #include <string> #include <cstdlib> #include <memory> #include <ctime> #include <bitset> #include <queue> #include <stack> #include <unordered_map> #include <unordered_set> #include <bitset> using namespace std; class Mro2024 { int n; std::string inputString; vector<int> vec; public: std::vector<int> countChanges(std::vector<int>& arr) { int n = arr.size(); std::vector<int> changes(n, 0); for (int i = 0; i < n; ++i) { for (int j = 0; j < n - i - 1; ++j) { if (arr[j] > arr[j + 1]) { std::swap(arr[j], arr[j + 1]); // Increment change count for both elements changes[j]++; changes[j + 1]++; } } } return changes; } void virtual Solve() { //how to create a vector of zeros: cin >> n; //how to read a string from input: cin >> inputString; //initialize the vec with n zeros: vec = vector<int>(n, 0); //set in vector 1 if the inputString[i] == 'P': for (int i = 0; i < n; i++) { if (inputString[i] == 'P') { vec[i] = 1; } } std::vector<int> changes = countChanges(vec); //how to print all numbers from the changes by adding space between them: for (int i = 0; i < changes.size(); i++) { cout << changes[i] << " "; } std::cout << std::endl; } }; int main() { Mro2024 *p = new Mro2024(); p->Solve(); delete p; return 0; } |