Codeforces Round 923 (Div. 3)
Codeforces Round 923 (Div. 3)
A. Make it White
题意:略
思路:找最小和最大的‘B’下标即可
AC code:
void solve() {
cin >>n;
string s; cin>> s;
int mn = INF, mx = 0;
for (int i = 0; i < n; i ++) {
if (s[i] == 'B') {
mn = min(mn, i);
mx = max(mx, i);
}
}
cout << mx - mn + 1 << endl;
}
B. Following the String
题意:略
思路:map记录26个字母的出现次数,0即是用新的字母
AC code:
void solve() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
map<char, int> mp;
string s = "";
for (int i = 1; i <= n; i ++) {
for (char c = 'a'; c <= 'z'; c ++) {
if (mp[c] == a[i]) {
s += c;
mp[c] ++;
break;
}
}
} cout << s << endl;
}
C. Choose the Different Ones!
题意:给出n个a数组元素和m个b数组元素,是否可以分别从a和b数组中取k/2个元素来组成1到k的每个元素
思路:用map分别记录ab数组元素,然后枚举1到k的元素,若有未出现的直接返回NO,然后记录ab数组的交集元素,最后检查仅存在于其中一个集合的元素是否大于k/2,若不存在则YES
AC code:
void solve() {
cin >> n >> m >> k;
map<int, int> ma, mb;
for (int i = 1; i <= n; i ++) {
int x; cin >> x;
if (x <= k && x >= 1) ma[x] ++;
}
for (int i = 1; i <= m; i ++) {
int x; cin >> x;
if (x <= k && x >= 1) mb[x] ++;
}
int ca = ma.size(), cb = mb.size();
int cnt = 0;
for (int i = 1; i <= k; i ++) {
if (!ma[i] && !mb[i]) {
cout << "NO" << endl;
return;
}
if (ma[i] && mb[i]) cnt ++;
}
if (ca - cnt > k / 2 || cb - cnt > k / 2) {
cout << "NO" << endl;
return;
}
cout << "YES" << endl;
}
D. Find the Different Ones!
题意:查询任意区间元素是否存在两个不同的元素
思路:从后往前记录最近的一个不同元素的下标,依次向前迭代
AC code:
void solve() {
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> a[i];
nex[n] = -1;
for (int i = n - 1; i >= 1; i --) {
if (a[i] != a[i + 1]) nex[i] = i + 1;
else nex[i] = nex[i + 1];
}
cin >> q;
while (q --) {
int l, r; cin >> l >> r;
if (nex[l] != -1 && nex[l] <= r) {
cout << l << " " << nex[l] << endl;
} else {
cout << "-1 -1" << endl;
}
}
}
E. Klever Permutation
题意:将1到n的数排列组合,使得每组相邻的k个元素的和相差不超过1
思路:
可以发现,每组向前迭代的过程都是先去掉一个头元素,再添一个尾元素;
那么只要去掉的和新的元素差不超过1即可,而两元素下标差为k;
所以我们可以枚举答案序列的前k个起始元素,每个元素以下标+k的方式迭代到最后;
这样可以满足答案序列每组删除和新增的元素差为1,但是不能保证每一组的和都是相差小于1;
所以可以从小到大一组,从大到小一组,通过一个标记点可以实现;
AC code:
void solve() {
cin >> n >> k;
vector<int> ans(n + 10, 0);
int st = 1;
bool flag = true;
for (int i = 1; i <= k; i ++) {
int x = i;
if (flag) {
while (x <= n) {
ans[x] = st;
st ++;
x += k;
}
flag = false;
} else {
while (x <= n) {
st ++;
x += k;
}
int now;
if (x > n) x -= k, now = st - 1;
x = i;
while (x <= n) {
ans[x] = now;
now --;
x += k;
}
flag = true;
}
}
for (int i = 1; i <= n; i ++) cout << ans[i] << " ";
cout << endl;
}
F待补。。。。。。