2020年CCCC-总决赛

本文最后更新于:星期四, 二月 3日 2022, 9:15 晚上

写在前面

因为考研,所以没参加,但是早上忍不住还是打了补题场,9点开打,大概三小时不到一点,然后261/290分,还可以吧

L1-1 嫑废话上代码 (5分)

1
print("Talk is cheap. Show me the code.")

L1-2 猫是液体 (5分)

1
2
3
4
5
6
7
8
9
#include<stdio.h>
#include<string.h>

int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%d", a * b * c);
return 0;
}

L1-3 洛希极限(10分)

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;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

void RUN() {
db a, b;
int op;
while (cin >> a >> op >> b) {
op = !op;
db res = a * (op ? 2.455 : 1.26);
if (res > b) {
cout << res << " T_T" << endl;
} else {
cout << res << " ^_^" << endl;
}
}
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(2);

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L1-4 调和平均 (10分)

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 <bits/stdc++.h>

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

int n;
db a[N];

void RUN() {
scanf("%d", &n);
db sum = 0, x;
for (int i = 1; i <= n; ++i) {
scanf("%lf", &x);
sum += 1.0 / x;
}
sum = n / sum;
printf("%.2f\n", sum);
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L1-5 胎压监测 (15分)

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

int a[10];

void RUN() {
for (int i = 1; i <= 6; ++i) {
cin >> a[i];
}
int Max = a[1];
for (int i = 1; i <= 4; ++i) Max = max(Max, a[i]);
vector<int> vec;
for (int i = 1; i <= 4; ++i) {
if (abs(a[i] - Max) > a[6] || a[i] < a[5]) vec.push_back(i);
}
if (vec.empty()) cout << "Normal" << endl;
else if (vec.size() == 1) {
cout << "Warning: please check #" << vec[0] << "!" << endl;
} else {
cout << "Warning: please check all the tires!" << endl;
}
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L1-6 吃火锅 (15分)

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

int n;
string s, str = "chi1 huo3 guo1";
vector<int> vec;

void RUN() {
int len = str.size();
while (getline(cin, s)) {
if (s == ".") break;
++n;
if (s.find(str) != s.npos) {
vec.push_back(n);
}
}
cout << n << endl;
if (vec.empty()) {
cout << "-_-#" << endl;
} else {
cout << vec[0] << " " << vec.size() << endl;
}
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(2);

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L1-7 前世档案 (20分)

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

int m, n;
string s;

void RUN() {
cin >> m >> n;
ll begin = 1ll << m;
--begin;
for (int i = 1; i <= n; ++i) {
cin >> s;
ll id = 1;
for (auto it : s) {
if (it == 'y') id = id << 1;
else id = id << 1 | 1;
}
cout << id - begin << endl;
}
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L1-8 刮刮彩票 (20分)

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

int a[4][4], vis[10];
int sum[] = {10000, 36, 720, 360, 80, 252, 108, 72, 54, 180, 72, 180, 119, 36, 306, 1080, 144, 1800, 3600};

void RUN() {
int x = 0, y = 0;
for (int i = 1; i <= 3; ++i) {
for (int j = 1; j <= 3; ++j) {
cin >> a[i][j];
if (!a[i][j]) {
x = i, y = j;
} else {
vis[a[i][j]] = 1;
}
}
}
for (int i = 1; i <= 9; ++i) if (!vis[i]) a[x][y] = i;
for (int i = 1; i <= 3; ++i) {
cin >> x >> y;
cout << a[x][y] << endl;
}
int op;
cin >> op;
int cnt = 0;
if (op <= 3) {
for (int i = 1; i <= 3; ++i) {
cnt += a[op][i];
}
} else if (op <= 6) {
for (int i = 1; i <= 3; ++i) {
cnt += a[i][op - 3];
}
} else if (op == 7) {
for (int i = 1; i <= 3; ++i) {
cnt += a[i][i];
}
} else {
for (int i = 1, j = 3; i <= 3; ++i, --j) {
cnt += a[i][j];
}
}
cout << sum[cnt - 6] << endl;
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L2-1 简单计算器 (25分)

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

int n;
stack<int> num;
stack<string> op;

void RUN() {
cin >> n;
for (int i = 1, x; i <= n; ++i) {
cin >> x;
num.push(x);
}
string s;
for (int i = 2; i <= n; ++i) {
cin >> s;
op.push(s);
}

for (int i = 2; i <= n; ++i) {
int n1 = num.top();
num.pop();
int n2 = num.top();
num.pop();
s = op.top();
op.pop();
if (s == "+") n1 = n2 + n1;
else if (s == "-") n1 = n2 - n1;
else if (s == "*") n1 = n2 * n1;
else if (s == "/") {
if (n1 == 0) {
cout << "ERROR: " << n2 << "/" << n1 << endl;
return;
}
n1 = n2 / n1;
}
num.push(n1);
}
cout << num.top() << endl;
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L2-2 口罩发放 (25分)

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

struct E {
string name, id, time;
int idx;
int op;

bool input(int _idx) {
cin >> name >> id >> op >> time;
idx = _idx;
if (id.size() != 18) return false;
for (auto it : id) {
if (it < '0' || it > '9') return false;
}
return true;
}

void out() {
cout << name << " " << id << endl;
}

bool operator<(const E &other) {
if (time == other.time) return idx < other.idx;
return time < other.time;
}
}a[N];

int d, p, n, m;
map<string, int> mp;
vector<E> V;


void gao(int day) {
cin >> n >> m;
vector<E> vec;
for (int i = 1; i <= n; ++i) {
bool F = a[i].input(i);
if (F) {
if (a[i].op == 1) V.push_back(a[i]);
vec.push_back(a[i]);
}
}
sort(vec.begin(), vec.end());
for (auto it : vec) {
if (!m) {
continue;
}
if (!mp.count(it.id) || day > mp[it.id] + p) {
mp[it.id] = day;
--m;
it.out();
}
}
}

void RUN() {
cin >> d >> p;
for (int i = 1; i <= d; ++i) {
gao(i);
}
mp.clear();
for (auto it : V) {
if (mp.count(it.id)) continue;
it.out();
mp[it.id]++;
}
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);

RUN();

#ifdef LOCAL_JUDGE
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L2-3 完全二叉树的层序遍历 (25分)

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

int n;
int a[N];
int t[N];
int idx = 1;

void gao(int id) {
if (id > n) return;
gao(id << 1);
gao(id << 1 | 1);
t[id] = a[idx++];
}

void RUN() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
gao(1);
for (int i = 1; i <= n; ++i) cout << t[i] << " \n"[i == n];
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L2-4 网红点打卡攻略 (25分)

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e2 + 10;

int n, m;
int G[N][N];
int Max = 0x3f3f3f3f, ans = -1, cnt;
int a[N];

void gao(int id) {
cin >> m;
set<int> s;
for (int i = 1; i <= m; ++i) {
cin >> a[i];
s.insert(a[i]);
}
if (m != n) return;
if (s.size() != n) return;
int res = 0, pre = 0;
for (int i = 1; i <= m; ++i) {
if (G[pre][a[i]] == 0x3f3f3f3f) return;
res += G[pre][a[i]];
pre = a[i];
}
if (G[a[m]][0] == 0x3f3f3f3f) return;
res += G[a[m]][0];
++cnt;
if (res < Max) ans = id, Max = res;
}

void RUN() {
memset(G, 0x3f, sizeof G);
cin >> n >> m;
for (int i = 1, u, v, w; i <= m; ++i) {
cin >> u >> v >> w;
G[u][v] = w;
G[v][u] = w;
}
int q;
cin >> q;
for (int i = 1; i <= q; ++i) {
gao(i);
}
cout << cnt << endl;
cout << ans << " " << Max << endl;
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L3-1 那就别担心了 (30分)

垃圾题目,关于Yes/No只需要从A出发,然后只要一个终点就好了

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
cout.flush();
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 5e2 + 10;

int n, m, st, ed;
int in_degree[N], out_degree[N], back_in_degree[N], vis[N];
int cnt[N], g[N][N];
vector<vector<int>> G, R;

void BFS(int rt) {
queue<int> q;
q.push(rt);
while (!q.empty()) {
int u = q.front();
q.pop();
if (vis[u]) continue;
vis[u] = true;
for (auto it : R[u]) {
q.push(it);
g[u][it] = 1;
out_degree[u]++;
}
}
}

void gao() {
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
g[i][j] += g[i][k] * g[k][j];
}
}
}
cout << g[st][ed] << " ";
if (g[st][ed] == 0) {
cout << "No" << endl;
return;
}
vector<int> vec;
for (int i = 1; i <= n; ++i) {
if (vis[i] && out_degree[i] == 0) {
vec.push_back(i);
}
}
if (vec.size() == 1)cout << "Yes" << endl;
else cout << "No" << endl;
}

void RUN() {
cin >> n >> m;
G.resize(n + 1);
R.resize(n + 1);
for (int i = 1, u, v; i <= m; ++i) {
cin >> u >> v;
R[u].push_back(v);
}
cin >> st >> ed;
BFS(st);
gao();
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L3-2 传送门 (18/30分)

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

struct E {
int x1, x2, y;

E() {}

E(int x1, int x2, int y) : x1(x1), x2(x2), y(y) {};

bool operator<(const E &other) const {
if (y != other.y) return y < other.y;
else {
if (x1 != other.x1) return x1 < other.x1;
else return x2 < other.x2;
}
}
};

int n, m;
ll f[N];
set<E> s;
string op;

void gao() {
for (int i = 1; i <= n; ++i) f[i] = i;
for (auto it : s) {
swap(f[it.x1], f[it.x2]);
}
ll res = 0;
for (int i = 1; i <= n; ++i) res += i * f[i];
cout << res << endl;
}

void RUN() {
cin >> n >> m;
for (int i = 1, a, b, c; i <= m; ++i) {
cin >> op >> a >> b >> c;
if (op == "+") s.insert(E(a, b, c));
else s.erase(E(a, b, c));
gao();
}
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);

RUN();

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

L3-3 可怜的复杂度 (13/30分)

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

using namespace std;

#define dbg(x...) \
do { \
cout << #x << " -> "; \
err(x); \
} while (0)

void err() {
cout << endl;
}

template<class T, class... Ts>
void err(const T &arg, const Ts &... args) {
cout << arg << ' ';
err(args...);
}

#define endl "\n"
#define all(A) A.begin(), A.end()
using ll = long long;
using db = double;
using pII = pair<int, int>;

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;

int n, m;
int b[N], a[N];
map<vector<int>, int> mp;
ll res;

int f() {
mp.clear();
for (int l = 1; l <= n; ++l) {
vector<int> vec;
for (int r = l; r <= n; ++r) {
vec.push_back(a[r]);
mp[vec]++;
}
}
return mp.size();
}

void gao(int x) {
if (x == n + 1) {
res += f();
return;
}
for (int i = 1; i <= m; ++i) {
a[x] = m * b[x] + i;
gao(x + 1);
}
}

void RUN() {
res = 0;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> b[i];
}
gao(1);
cout << res << endl;
}

int main() {
#ifdef LOCAL_JUDGE
freopen("input", "r", stdin);
#endif

ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);

int _T;
cin >> _T;
while (_T--) {
RUN();
}

#ifdef LOCAL_JUDGE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!