Codeforces Round 946 (Div.3)

5月20日 Codeforces 实战记录。(1156 -> 1378)

AC数3/6
比赛跳转:Codeforces Round 946 (Div. 3)

题解

A. Phone Desktop

题意:有 x 个 $1\times1$ 的 icon 和 y 个 $2\times2$ 的 icon,一个 screen 大小 $5\times3$ ,试求需要多少个 screen 拼起来可以容纳所有 icon。

分析:满足两个约束条件即可:$res\times2-1\geq y,\ res\times 15\geq x+4y$

完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<bits/stdc++.h>
using namespace std;

inline int read(){
int x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
return x * f;
}

int main(){
int t = read();
while(t--){
int x = read(), y = read();
cout << max(y + 1 >> 1, (x + 4 * y + 14) / 15) << '\n';
}
return 0;
}

B. Symmetric Encoding

分析:按题目设定解码即可。

完整代码:

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
#include<bits/stdc++.h>
using namespace std;

inline int read() {
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
return x * f;
}

int main() {
int t = read();
while (t--) {
int n = read();
string s; cin >> s;
int cnt = 0;
vector<int> r;
for (int i = 0; i < n; i++)
r.emplace_back(s[i] - 'a');
sort(r.begin(), r.end());
r.erase(unique(r.begin(), r.end()), r.end());

vector<int> mp(30);
for (int i = 0; i < r.size(); i++)
mp[r[i]] = r[r.size() - 1 - i];

for (int i = 0; i < n; i++)
s[i] = 'a' + mp[s[i] - 'a'];

cout << s << '\n';
}
return 0;
}

C. Beautiful Triple Pairs

题意:给出数列中只有一位不同的有序三元组的个数。

分析:逐个三元组加入,用 map 存下每两位的出现情况,计数。

完整代码:

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
#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2e5 + 10;
int a[N];

inline int read() {
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
return x * f;
}

signed main() {
int t = read();
while (t--) {
map<int, map<int, map<int, int>>> mp;
map<int, map<int, int>>cul1, cul2, cul3;
int n = read(); int ans = 0;
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= n - 2; i++) {
ans -= 3 * mp[a[i]][a[i + 1]][a[i + 2]]++;

ans += cul1[a[i]][a[i + 1]]++;
ans += cul2[a[i]][a[i + 2]]++;
ans += cul3[a[i + 1]][a[i + 2]]++;
}

cout << ans << '\n';
}
return 0;
}

D. Ingenuity-2

一个庞大的分类讨论题。考虑相对运动,即将 R 的运动看作 H 朝反方向的运动,只需 H 最后运动到原点即可(各方向运动代数和为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
#include<bits/stdc++.h>
using namespace std;

inline int read(){
int x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
return x * f;
}

int main(){
int t = read();
while(t--){
int n = read(), a = 0, b = 0, c = 0, d = 0;
string s; cin >> s;
string res; res.resize(s.size());
for(int i = 0; i < n; i++){
if(s[i] == 'N') a++;
else if(s[i] == 'S') b++;
else if(s[i] == 'E') c++;
else if(s[i] == 'W') d++;
}

if((a - b) & 1 || (c - d) & 1 || !(a > 1 || b > 1 || c > 1 || d > 1) && !(a == 1 && b == 1 && c == 1 && d == 1)) {
cout << "NO\n";
continue;
}

// u,v表示需要翻转的次数。
int u = (a - b) / 2, v = (c - d) / 2;
for(int i = 0; i < n; i++){
if(s[i] == 'N'){
res[i] = u > 0 ? u--, 'R' : 'H';
}
else if(s[i] == 'S'){
res[i] = u < 0 ? u++, 'R' : 'H';
}
else if(s[i] == 'E'){
res[i] = v > 0 ? v--, 'R' : 'H';
}
else if(s[i] == 'W'){
res[i] = v < 0 ? v++, 'R' : 'H';
}
}

// 处理只有一方移动的情况(翻转两个对称的instruction)
if(!count(res.begin(), res.end(), 'R') || !count(res.begin(), res.end(), 'H')){
bool f1 = 1, f2 = 1, f3 = 1, f4 = 1;
for(int i = 0; i < s.size(); i++){
if(s[i] == 'N' && f1) res[i] = 'R' + 'H' - res[i], f1 = f3 = f4 = 0;
else if(s[i] == 'S' && f2) res[i] = 'R' + 'H' - res[i], f2 = f3 = f4 = 0;
else if(s[i] == 'E' && f3) res[i] = 'R' + 'H' - res[i], f3 = f1 = f2 = 0;
else if(s[i] == 'W' && f4) res[i] = 'R' + 'H' - res[i], f4 = f1 = f2 = 0;
}
}

cout << res << '\n';
}
return 0;
}