Educational Codeforces Round 134 (Rated for Div. 2) A~D
A - Image
#include<bits/stdc++.h>
void solve()
{
char a[4];
std::map<char, int> mp;
int cnt = 0;
for (int i = 0; i < 4; i++) {
std::cin >> a[i];
if (!mp[a[i]]) {cnt++; mp[a[i]] = 1;}
}
std::cout << cnt - 1 << "\n";
}
int main()
{
int T;
std::cin >> T;
while (T--) solve();
return 0;
}
B - Deadly Laser
#include<bits/stdc++.h>
void solve()
{
int n, m, sx, sy, d;
std::cin >> n >> m >> sx >> sy >> d;
if ((sx + d >= n && sy + d >= m)) {std::cout << "-1\n"; return;}
if ((sx - d <= 1 && sx + d >= n)) {std::cout << "-1\n"; return;}
if ((sx - d <= 1 && sy - d <= 1)) {std::cout << "-1\n"; return;}
if ((sy - d <= 1 && sy + d >= m)) {std::cout << "-1\n"; return;}
std::cout << n + m - 2 << "\n";
}
int main()
{
int T;
std::cin >> T;
while (T--) solve();
return 0;
}
C - Min-Max Array Transformation
题意:给出数组a和b,满足条件
b
i
=
a
i
+
d
i
b_i = a_i + d_i
bi=ai+di要求构造数组dmin和dmax,代表d数组每个元素的上下界,要求求出来b排序之后是给定的数组,a和b都是不递减的非负数组
思路:对于最小值直接找到比
a
i
a_i
ai大的
b
j
b_j
bj满足
d
i
=
b
j
−
a
i
d_i=b_j-a_i
di=bj−ai。对于最大值,首先可以发现,一个数字不能构造比自己小的数组,所以我们对于一段区间要找到它能容纳的最大
b
l
b_l
bl就行了
#include<bits/stdc++.h>
void solve()
{
int n;
std::cin >> n;
std::vector<int> a(n), b(n), dmin(n), dmax(n);
for (int i = 0; i < n; i++) std::cin >> a[i];
for (int i = 0; i < n; i++) std::cin >> b[i];
for (int i = 0; i < n; i++) {
dmin[i] = std::lower_bound(b.begin(), b.end(), a[i]) - b.begin();
dmin[i] = b[dmin[i]] - a[i];
}
int l = n - 1;
for (int i = n - 1; i >= 0; i--) {
dmax[i] = b[l] - a[i];
if (lower_bound(b.begin(), b.end(), a[i]) - b.begin() == i) l = i - 1;
}
for (int i = 0; i < n; i++) std::cout << dmin[i] << " \n"[i == n - 1] ;
for (int i = 0; i < n; i++) std::cout << dmax[i] << " \n"[i == n - 1] ;
std::cout << "\n";
}
int main()
{
int T;
std::cin >> T;
while (T--) solve();
return 0;
}
D - Maximum AND
题意:给出数组a和b,
c
i
=
a
x
⊕
b
y
c_i = a_x \oplus b_y
ci=ax⊕by,现在要求
m
a
x
{
c
1
&
c
2
&
c
3
.
.
.
&
c
n
}
max\{c_1\&c_2\&c_3...\&c_n\}
max{c1&c2&c3...&cn}
思路:我们对于二进制位拆位,因为
c
i
=
a
x
⊕
b
y
c_i=a_x \oplus b_y
ci=ax⊕by,所以假如答案是
1001010
1001010
1001010,也就代表a和b的01状态要相反,所以把b取反从高位枚举ans这一位置的情况,看a和b&上ans的情况是不是一样,一样说明这一位可以置1
#include<bits/stdc++.h>
void solve()
{
int n;
std::cin >> n;
std::vector<int> a(n), b(n);
for (int i = 0; i < n; i++) std::cin >> a[i];
for (int i = 0; i < n; i++) {std::cin >> b[i]; b[i] = ~b[i];}
int ans = 0;
for (int t = 29; t >= 0; t--) {
ans |= (1 << t);
std::map<int, int> mpa, mpb;
for (int i = 0; i < n; i++) {
mpa[a[i] & ans]++;
mpb[b[i] & ans]++;
}
int f = 1;
for (auto m : mpa) {
if (m.second != mpb[m.first]) {f = 0; break;}
}
if (f) continue;
ans ^= (1 << t);
}
std::cout << ans << "\n";
}
int main()
{
int T;
std::cin >> T;
while (T--) solve();
return 0;
}