Codeforces Round #761 (Div. 2)
这周的codeforces终于突破了A题战士,来记录一下。
A Forbidden Subsequence
题面
1.给出了三个概念: permutation 、 subsequence和 lexicographically smaller。 permutation指两个字符串中各个字母数量相同;subsequence指可以通过删减b的字符得到a; lexicographically smaller指a是b的前缀,或者a和b两个字符串中两个字符不匹配时a出现的更早。
String a is a permutation of string b if the number of occurrences of each distinct character is the same in both strings.
A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
A string a is lexicographically smaller than a string b if and only if one of the following holds:
a is a prefix of b, but a≠b;
in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
输出
output a single string S′, the lexicographically smallest permutation of S such that T is not a subsequence of S′.
思路
只需要判断T是不是“abc”和S中有无‘a’。
如果T不是“abc”,只要将S按顺序排列,那么不可能出现T的情况,因为‘a’永远在前,不可能与b**,c**重复,也不可能与“acb”重复。
如果T是“abc”并且S中有‘a’,那么需要排序并且bc调换顺序。
代码
#include<bits/stdc++.h>
using namespace std;
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e6 + 10;
int test;
int alpha[26];
string s, t;
int main() {
io_opt;
cin >> test;
while (test--) {
cin >> s;
cin >> t;
memset(alpha, 0, sizeof(alpha));
for (int i = 0; i < s.size(); i++) alpha[s[i] - 'a']++;
if (t != "abc" || !alpha[0]) sort(s.begin(), s.end());
else {
s = "";
for (int i = 0; i < alpha[0]; i++) s += 'a';
for (int i = 0; i < alpha[2]; i++) s += 'c';
for (int i = 0; i < alpha[1]; i++) s += 'b';
for (int i = 3; i < 26; i++) {
for (int j = 0; j < alpha[i]; j++) s += char('a' + i);
}
}
cout << s << endl;
}
return 0;
}
C Paprika and Permutation
题面
将一组长度为n的序列通过变换为从1到n的数组,变换可以是将ai变为ai的余数。
In order to achieve this goal, she can perform operations on the array. In each operation she can choose two integers i (1≤i≤n) and x (x>0), then perform ai:=aimodx (that is, replace ai by the remainder of ai divided by x). In different operations, the chosen i and x can be different.
输出
For each test case, output the minimum number of operations needed to make the array a permutation of integers 1 to n, or −1 if it is impossible.
思路
一个贪心的算法。因为要求得到最小操作数,所以数列中满足ai∈[1,n]的数字不变,将其余数字变换为所需[1,n]中没有的数字。
我们要判断一个数字能不能变换为所需数字,只需判断他的最大余数是否>=所需数字,并且一个数字的最大余数为(ai-1)/2。由于两者为线性关系,只需将两组数据排序后分别判断就好了。
#include<bits/stdc++.h>
using namespace std;
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e6 + 10;
int t, n;
int arr[N];
bool ex[N];
vector<int> p1,p2;
int main() {
io_opt;
cin >> t;
while (t--) {
cin >> n;
bool wrong = false;
int res = 0;
p1.clear(), p2.clear();
memset(ex, false, sizeof(ex));
for (int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
for (int i = 0; i < n; i++) {
if (1<=arr[i] && arr[i] <= n && !ex[arr[i]]) ex[arr[i]] = true;
else p2.push_back(arr[i]);
}
for (int i = 1; i <= n; i++) if (!ex[i]) p1.push_back(i);
int idx = 0;
while (idx < p1.size()) {
if ((p2[idx] - 1) / 2 < p1[idx]) wrong = true;
else res++;
idx++;
}
if (wrong) cout << -1 << endl;
else cout << res << endl;
}
return 0;
}