#pragma GCC optimize("Ofast,unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #define ll long long #define ld long double #define u unsigned #define PB push_back #define MP make_pair #define LPI(i, a, b, d) for (int i = a; i < b; i += d) #define LPD(i, a, b, d) for (int i = a; i > b; i -= d) #define REP(n) for (int i = 0; i < n; ++i) #ifndef INT_MAX #define INT_MAX 2147483647 #endif #ifndef INT_MIN #define INT_MIN -2147483648 #endif #define LL_MIN -9223372036854775808 #define LL_MAX 9223372036854775807 using VI = vector<int>; using VVI = vector<VI>; using VS = vector<string>; using VVS = vector<VS>; using PI = pair<int, int>; using TI = tuple<int, int, int>; #define ZEROS_B(x) __builtin_clz(x) #define ZEROS_E(x) __builtin_ctz(x) constexpr double pi = acos(-1); constexpr int mod = 998244353; constexpr double eps = 1e-10; constexpr int N = 1e2 + 10; template <typename T> T gcd(T a, T b) { if (!a || !b) return a | b; unsigned shift = __builtin_ctz(a | b); a >>= __builtin_ctz(a); do { b >>= __builtin_ctz(b); if (a > b) swap(a, b); b -= a; } while (b); return a << shift; } template <typename T> T LCM(T a, T b) { return a / GCD(a, b) * b; } auto main() -> int { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; int marki[n]; LPI(i, 0, n, 1) { cin >> marki[i]; } int git = 0; bool da_sie = true; ll czas = 0LL; LPI(i, 0, n, 1) { bool ruch = false; if (i < n + 1 && marki[i] != marki[i + 1]) { auto tmp = marki[i]; marki[i] = marki[i + 1]; marki[i + 1] = tmp; if (i < k) ++git; if(git == k) break; ++czas; ruch = true; } if (i == n - 1 && !ruch) da_sie = false; if (i == n - 1 && git < k && ruch) i = 0; } da_sie ? cout << czas << '\n' : cout << "-1\n"; 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 94 95 | #pragma GCC optimize("Ofast,unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; #define ll long long #define ld long double #define u unsigned #define PB push_back #define MP make_pair #define LPI(i, a, b, d) for (int i = a; i < b; i += d) #define LPD(i, a, b, d) for (int i = a; i > b; i -= d) #define REP(n) for (int i = 0; i < n; ++i) #ifndef INT_MAX #define INT_MAX 2147483647 #endif #ifndef INT_MIN #define INT_MIN -2147483648 #endif #define LL_MIN -9223372036854775808 #define LL_MAX 9223372036854775807 using VI = vector<int>; using VVI = vector<VI>; using VS = vector<string>; using VVS = vector<VS>; using PI = pair<int, int>; using TI = tuple<int, int, int>; #define ZEROS_B(x) __builtin_clz(x) #define ZEROS_E(x) __builtin_ctz(x) constexpr double pi = acos(-1); constexpr int mod = 998244353; constexpr double eps = 1e-10; constexpr int N = 1e2 + 10; template <typename T> T gcd(T a, T b) { if (!a || !b) return a | b; unsigned shift = __builtin_ctz(a | b); a >>= __builtin_ctz(a); do { b >>= __builtin_ctz(b); if (a > b) swap(a, b); b -= a; } while (b); return a << shift; } template <typename T> T LCM(T a, T b) { return a / GCD(a, b) * b; } auto main() -> int { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; int marki[n]; LPI(i, 0, n, 1) { cin >> marki[i]; } int git = 0; bool da_sie = true; ll czas = 0LL; LPI(i, 0, n, 1) { bool ruch = false; if (i < n + 1 && marki[i] != marki[i + 1]) { auto tmp = marki[i]; marki[i] = marki[i + 1]; marki[i + 1] = tmp; if (i < k) ++git; if(git == k) break; ++czas; ruch = true; } if (i == n - 1 && !ruch) da_sie = false; if (i == n - 1 && git < k && ruch) i = 0; } da_sie ? cout << czas << '\n' : cout << "-1\n"; return 0; } |