Codeforces Round 608 (Div. 2)

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

A. Suits

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

/*
*
* Author: Hsueh-
* Date: 2020-02-11 10:15:51
*
* */

#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 pII = pair<int, int>;
using ll = long long;
using db = double;

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

int gao1(int a, int b, int c, int d, int e, int f) {
int res = 0;
res += min(a, d) * e;
d -= min(a, d);
res += min(b, min(c, d)) * f;
return res;
}

int gao2(int a, int b, int c, int d, int e, int f) {
int res = 0;
res += min(b, min(c, d)) * f;
d -= min(b, min(c, d));
res += min(a, d) * e;
return res;
}

int a, b, c, d, e, f;

void RUN() {
cin >> a >> b >> c >> d >> e >> f;
cout << max(gao1(a, b, c, d, e, f), gao2(a, b, c, d, e, f)) << endl;
}

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

RUN();

return 0;
}

B. Blocks

题意:

给一个01串,每次可以将i,i+1i, i+1翻转,问能否将其翻转成为全黑全白,输出方案(方案次数3n\leq 3\cdot n

思路:

尝试全黑和全白,暴力翻转

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

/*
*
* Author: Hsueh-
* Date: 2020-02-11 10:21:13
*
* */

#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 pII = pair<int, int>;
using ll = long long;
using db = double;

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

int n, a[N];
char s[N];
vector<int> res;

void RUN() {
cin >> n;
cin >> (s + 1);
for (int i = 1; i <= n; ++i) {
if (s[i] == 'W')
a[i] = 0;
else
a[i] = 1;
}
for (int i = 1; i < n; ++i) {
if (!a[i]) {
a[i] ^= 1, a[i + 1] ^= 1;
res.push_back(i);
}
}
if (a[n]) {
cout << res.size() << endl;
for (int i = 0, len = res.size(); i < len; ++i) {
cout << res[i] << " \n"[i==len - 1];
}
return;
}
for (int i = 1; i < n; ++i) {
if (a[i]) {
a[i] ^= 1, a[i + 1] ^= 1;
res.push_back(i);
}
}
if (!a[n]) {
cout << res.size() << endl;
for (int i = 0, len = res.size(); i < len; ++i) {
cout << res[i] << " \n"[i == len - 1];
}
return;
}
cout << -1 << endl;
}

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

RUN();

return 0;
}

C. Shawarma Tent

题意:

有一个学校在(sx,sy)(sx,sy)位置,有nn个学生,每个人家的坐标在(xi,yi)(x_i,y_i)

现在想要开一个$shawarma ,如果这个学生可以经过shawarma 后到达学校依旧是最短距离,那么认定这个学生给shawarma $带来贡献

求最大的贡献值和坐标

思路:

很显然把$shawarma 开在学校的上下左右是最优的,暴力算一遍取max$即可

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

/*
*
* Author: Hsueh-
* Date: 2020-02-11 10:29:54
*
* */

#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 pII = pair<int, int>;
using ll = long long;
using db = double;

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

int n, sx, sy;
int x[N], y[N];
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};

void RUN() {
cin >> n >> sx >> sy;
for (int i = 1; i <= n; ++i) {
cin >> x[i] >> y[i];
}
int Max = -1, _x = 0, _y = 0;
for (int i = 0; i < 4; ++i) {
int ex = sx + dir[i][0], ey = sy + dir[i][1];
int cnt = 0;
for (int i = 1; i <= n; ++i) {
if (abs(x[i] - sx) + abs(y[i] - sy) ==
abs(x[i] - ex) + abs(y[i] - ey) + 1) {
++cnt;
}
}
if (cnt > Max) {
Max = cnt, _x = ex, _y = ey;
}
}
cout << Max << endl;
cout << _x << " " << _y << endl;
}

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

RUN();

return 0;
}

D. Portals

题意:

nn个城堡,刚开始你有kk个军队,有mm条路径

你现在按照1n1 - n的顺序攻打城堡

每个城堡你需要至少aia_i个军队,但是攻打城堡不耗费军队,攻打后你可以招bib_i个军队,同时每个城堡有cic_i的重要性

当你派至少一个军队留在城堡的时候算占领

占领只有当你刚攻打城堡的时候可以占领或者通过mm条路径占领

mm条有向路径,u>vu>v

问最后占领的城堡的最大重要性

如果不能攻打所有城堡输出1-1

思路:

首先判断1-1

其次对每个城堡维护一个MaxiMax_i,表示最远的可以通过路径可以占领的城堡下标

对每个城堡按照(ci,i)(c_i,i)排序,一个城堡的重要性有价值只有Maxi+1nMax_i+1-n的城堡在少一个士兵的情况下依旧可以攻打,然后贪心即可

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

/*
*
* Author: Hsueh-
* Date: 2020-02-11 10:35:29
*
* */

#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 pII = pair<int, int>;
using ll = long long;
using db = double;

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

struct node {
int a, b, c;

void input() { cin >> a >> b >> c; }
} a[N];

int n, m, k;
int Max[N], remind[N];

void RUN() {
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i) {
a[i].input();
Max[i] = i;
}
for (int i = 1, u, v; i <= m; ++i) {
cin >> u >> v;
Max[v] = max(Max[v], u);
}
remind[1] = k;
for (int i = 1; i <= n; ++i) {
if (remind[i] < a[i].a) {
cout << "-1" << endl;
return;
}
remind[i + 1] = remind[i] + a[i].b;
}
priority_queue<pII> pq;
for (int i = 1; i <= n; ++i) {
pq.push(pII(a[i].c, i));
}
int res = 0;
while (!pq.empty()) {
pII t = pq.top();
pq.pop();
bool F = true;
for (int i = Max[t.second] + 1; i <= n + 1; ++i) {
if (remind[i] - 1 < a[i].a) {
F = false;
break;
}
}
if (F) {
res += t.first;
for (int i = Max[t.second] + 1; i <= n + 1; ++i) {
remind[i]--;
}
}
}
cout << res << endl;
}

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

RUN();

return 0;
}

E. Common Number

题意:

定义

f(x)= \begin{equation} \left\{ \begin{array}{**lr**} \frac{x}{2} \quad & if \; x \; is \; even \\ x-1 \quad & otherwise \end{array} \right. \end{equation}

定义path{x}path\{x\}为通过一系列计算得到的结果

求最大的yy满足{x1xn,ypath{x}k|\{x|1\leq x \leq n, y \in path\{x\}|\geq k

思路:

很显然这是一棵树

其中2x2x的父亲是xx2x+12x+1的父亲是2x2x

所以就变成了最大的数,它的子树大小k\geq k

那么很显然可以二分

我们分奇偶进行计算

偶数的时候要算上偶数加一的子树大小

对于一个数计算他的子树大小可以让他不断乘2计算

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

/*
*
* Author: Hsueh-
* Date: 2020-02-11 11:13:58
*
* */

#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 pII = pair<int, int>;
using ll = long long;
using db = double;

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

ll n, k;

ll f(ll x) {
ll res = 0, t = 1;
while (x <= n) {
res += min(t, n - x + 1);
x <<= 1, t <<= 1;
}
return res;
}

void RUN() {
cin >> n >> k;
ll res = -1;
ll l = 1, r = n / 2, x = -1;
while (r - l >= 0) {
ll mid = (l + r) >> 1;
if (f(mid << 1) + f(mid << 1 | 1) >= k) {
l = mid + 1;
x = mid << 1;
} else {
r = mid - 1;
}
}
res = max(res, x);
l = 1, r = (n + 1) / 2, x = -1;
while (r - l >= 0) {
ll mid = (l + r) >> 1;
if (f(2 * mid - 1) >= k) {
l = mid + 1;
x = 2 * mid - 1;
} else {
r = mid - 1;
}
}
cout << max(res, x) << endl;
}

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

RUN();

return 0;
}


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