#include <bits/stdc++.h>usingnamespacestd;int main() {int n, k;cin >> n >> k;vector<int> v(n);for (int i = 0; i < n; i++) {cin >> v[i];}int ans = 0;while (!v.empty() && v.back() <= k) {ans++;v.pop_back();}reverse(v.begin(), v.end());while (!v.empty() && v.back() <= k) {ans++;v.pop_back();}cout << ans << endl;return0;
}
B. Reversing Encryption
題意
給定一個長度為N的字符串,每次在N的除數區間 ( [1 - d] ) 進行翻轉,求原來的字符串。
AC
#include <bits/stdc++.h>usingnamespacestd;int main() {int n;string s;cin >> n >> s;for (int i = 1; i <= n; i++) {if (n % i == 0) {reverse(s.begin(), s.begin() + i);}}cout << s << endl;return0;
}
C. Alphabetic Removals
題意
給定一個長度為N字符串和操作次數K,一共刪除K個字母,按字典序刪除第一個出現的字母,輸出剩下的字母
AC
用數組統計每個字符一共刪除幾個,輸出的時候進行判讀
// 數組記錄刪除個數#include <bits/stdc++.h>usingnamespacestd;int main() {int n, k;string s;cin >> n >> k >> s;vector<int> sum(26), d(26);for (auto c : s) {sum[c - 'a']++;}for (int i = 0; i < 26; i++) {if (k >= sum[i]) {k -= sum[i];d[i] = sum[i];}else {d[i] = k;break;}}for (auto c : s) {if (d[c - 'a'] > 0) {d[c - 'a']--;}else {cout << c;}}cout << endl;return0;
}
用pair,先對字典序排序,刪除K個字母之后再按小標排序
// pair#include <bits/stdc++.h>usingnamespacestd;int main() {int n, k;string s;cin >> n >> k >> s;vector<pair<char,int>> v(n);for (int i = 0; i < n; i++) {v[i] = make_pair(s[i], i);}sort(v.begin(), v.end());sort(v.begin() + k, v.end(), [&] (const pair<char, int> &a, const pair<char, int> &b){return a.second < b.second;});for (int i = k; i < n; i++) {cout << v[i].first;}cout << endl;return0;
}
D. Equalize the Remainders
題意
給一個長度為N的序列和M,求最小的改動數使得改變后的序列的每個數對M取模,每個模都有 K = N / M 個,輸出最小改動次數和改變后的序列
AC
把原序列的每個mod的數量統計下,將數量大于K的存一下,有小于K的就補上。至少需要兩個循環
#include <bits/stdc++.h>#define N 200005usingnamespacestd;int main() {// freopen("in.txt", "r", stdin);ios::sync_with_stdio(false);int n, m, k;cin >> n >> m;k = n / m;vector<int> a(n);vector<vector<int>> d(m);vector<pair<int,int>> f;for (int i = 0; i < n; i++) {cin >> a[i];d[a[i] % m].push_back(i);}longlong ans = 0;for (int i = 0; i < 2 * m; i++) {int cur = i % m;while(int(d[cur].size()) > k) {int elem = d[cur].back();f.push_back(make_pair(elem, i));d[cur].pop_back();}while (int(d[cur].size()) < k && !f.empty()) {int elem = f.back().first;int dif = f.back().second;ans += i - dif;a[elem] += i - dif;d[cur].push_back(elem);f.pop_back();}}cout << ans << endl;for (auto it : a) {cout << it << " ";}cout << endl;return0;
}