发布于  更新于 

Codeforces Round 580 (Div. 2)

题解 OI

这次cf的div. 2感觉是手速场啊, ABC题能读懂题就会做; D题虽然看上去有好多pretest但是依然很水, 一大半的人都fst了; EF题div. 2就没几个人做得出来...(居然还有交互题, 长见识了)

A. Choose Two Numbers

签到题, 直接枚举.

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
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;

typedef long long int64;

const int INF = 0x3f3f3f3f;
const int MAXN = 210;

int a[MAXN], b[MAXN];
bool ins[MAXN];

#include <cctype>
#include <cstdio>

template <typename T = int>
inline T read() {
T X = 0, w = 0;
char ch = 0;
while (!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch)) {
X = (X << 3) + (X << 1) + (ch ^ 48);
ch = getchar();
}
return w ? -X : X;
}

int main() {
int n = read();
for (int i = 1; i <= n; i++) {
cin >> a[i];
ins[a[i]] = true;
}
int m = read();
for (int i = 1; i <= m; i++) {
cin >> b[i];
ins[b[i]] = true;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i] + b[j] > 200 || !ins[a[i] + b[j]]) {
cout << a[i] << ' ' << b[j] << endl;
return 0;
}
}
}
return 0;
}

B. Make Product Equal One

简单分类讨论, 记得int64(结果我就数组开小了WA了一次).

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
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <cassert>
#include <cmath>
using namespace std;

typedef long long int64;

const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;

#include <cctype>
#include <cstdio>

template <typename T = int>
inline T read() {
T X = 0, w = 0;
char ch = 0;
while (!isdigit(ch))
{
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch)) {
X = (X << 3) + (X << 1) + (ch ^ 48);
ch = getchar();
}
return w ? -X : X;
}

int a[MAXN];

int main() {
int n = read();
int64 ans = 0;
int cnt0 = 0, cntn1 = 0;
for (int i = 1; i <= n; i++) {
a[i] = read();
if (a[i] > 0) {
ans += a[i] - 1;
} else if (a[i] == 0) {
ans += 1;
cnt0++;
} else {
ans += -a[i] - 1;
cntn1++;
}
}
if (cntn1 & 1) {
if (cnt0 == 0) {
ans += 2;
}
}
cout << ans << endl;
return 0;
}

C. Almost Equal

找规律, 盲猜奇数可以偶数不行, 手推几个发现确实是这样. 规律的结论直接看代码吧.

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
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;

typedef long long int64;

const int INF = 0x3f3f3f3f;
const int MAXN = 1e5;

int ans[MAXN];

int main() {
int n;
cin >> n;
if (n & 1) {
cout << "YES" << endl;
for (int i = 1; i <= n; i++) {
if (i & 1) {
ans[i] = i;
} else {
ans[i] = 2 * n + 2 - i;
}
cout << ans[i] << ' ';
}
for (int i = 1; i <= n; i++) {
cout << (((ans[i] + 1) ^ 1) - 1) << ' ';
}
cout << endl;
} else {
cout << "NO" << endl;
}
return 0;
}

D. Shortest Cycle

  1. 如果有大于等于3个数在某一位上是1, 答案就是3
  2. 建图DFS找环, 每个开一个点, 1到64各开一个点, 如果某一个数有某一个二进制位就向相应的二进制位节点连一条无向边, DFS时忽略长度小于等于4的环, 答案是DFS找出的最小环长度除以2.

结果我做题的时候没有判第一点还过pretest了, 最后五分钟发现, 病急乱投医, resubmission还是写挂了, 最终fst...

下面是正确代码.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;

typedef long long int64;

const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 1000;
const int MAXS = 64;

int cnt[MAXS + 10];

class lfs {
public:
lfs(int N) {
memset(head, -1, sizeof head);
ecnt = 0;
n = N;
}
void adde(int from, int to) {
e[ecnt].to = to;
e[ecnt].next = head[from];
head[from] = ecnt++;
}
void addde(int a, int b) {
// clog << "adding edge" << a << b << endl;
adde(a, b);
adde(b, a);
}

int solve() {
memset(dep, 0, sizeof dep);
memset(vis,false,sizeof vis);
ans = INF;
for (int i = 1; i <= 64; i++) {
if (cnt[i] >= 3) {
return 3;
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfs(i, 0, 1);
}
return (ans < INF) ? (ans / 2) : -1;
}

protected:
struct Edge {
int to, next;
} e[MAXN * 64 * 2];
int head[MAXN];
int ecnt;
int n;

int ans;
int dep[MAXN];
bool vis[MAXN];

void dfs(int u, int fa, int d) {
dep[u] = d;
vis[u] = true;
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].to;
if (v != fa) {
if (vis[v]) {
int len = abs(dep[u] - dep[v]) + 1;
if (len > 4) {
ans = min(ans, len + 1);
}
} else {
dfs(v, u, d + 1);
}
}
}
}
};

int64 a[MAXN];

#include <cctype>
#include <cstdio>

template <typename T = int>
inline T read() {
T X = 0, w = 0;
char ch = 0;
while (!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch)) {
X = (X << 3) + (X << 1) + (ch ^ 48);
ch = getchar();
}
return w ? -X : X;
}

int main() {
int n = read();
auto graph = new lfs(n + 64);
for (int i = 1; i <= n; i++) {
a[i] = read<int64>();
for (int64 j = 1; j <= 64; j++) {
if ((a[i] >> (j - 1)) & 1) {
graph->addde(i, n + j);
cnt[j]++;
}
}
}
cout << graph->solve() << endl;
return 0;
}

EF过于毒瘤, 全场都没有几个人做掉...