길이
배열의
다음과 같이 경우를 나눌 수 있다.
1)
2)
3)
4)
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
|
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tp;
int main(void) {
cin.tie(NULL);
ios::sync_with_stdio(false);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<int> a(n+1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
a[0] = a[n];
int ans = a[n] - a[1];
for (int i = 2; i <= n; i++) {
ans = max(ans, a[i] - a[1]);
}
for (int i = 1; i <= n-1; i++) {
ans = max(ans, a[n] - a[i]);
}
for (int i = 1; i <= n; i++) {
ans = max(ans, a[i-1] - a[i]);
}
cout << ans << '\n';
}
return 0;
}
|
cs |
B. Mainak and Interesting Sequence
배열
배열
배열
만약 홀수개 존재한다면
같은 이유로
단, 가장 큰 원소는 홀짝 개수 제한이 없다.
다음과 같이 경우를 나눌 수 있다.
1)
2)
3)
3-1)
3-2)
3-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
|
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tp;
int main(void) {
cin.tie(NULL);
ios::sync_with_stdio(false);
int t; cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
if (n > m) {
cout << "No\n";
}
else {
if (n % 2) {
cout << "Yes\n";
for (int i = 0; i < n-1; i++) {
cout << 1 << ' ';
}
cout << m - n + 1 << '\n';
}
else {
if (m % 2) {
cout << "No\n";
}
else {
cout << "Yes\n";
for (int i = 0; i < n-2; i++) {
cout << 1 << ' ';
}
cout << (m - n + 2) / 2 << ' ';
cout << (m - n + 2) / 2 << '\n';
}
}
}
}
return 0;
}
|
cs |
C. Jatayu's Balanced Bracket Sequence
길이가
간선은 양방향이며 가중치가 붙지 않는다.
위 과정을 거쳐 만들어진 그래프의 component 개수를 구하는 문제이다.
1)
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
|
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tp;
int main(void) {
cin.tie(NULL);
ios::sync_with_stdio(false);
int t; cin >> t;
while (t--) {
int n; cin >> n;
string s; cin >> s;
stack<int> st;
vector<int> a;
for (char ch: s) {
if (ch == '(') {
st.push(1);
a.push_back(st.size());
}
else {
st.pop();
}
}
int ans = 1;
for (int i = 1; i < (int)a.size(); i++) {
if (a[i] > a[i-1]) ans++;
}
cout << ans << '\n';
}
return 0;
}
|
cs |
각 간선을 빨간색 또는 파란색 중 하나로 칠한다.
빨간 간선만 고려했을 때 component의 개수를
빨간 간선으로만 이뤄진 그래프를
WLOG,
증명
1.
cycle에 포함된 어떤 간선
이때
2.
이때
이제
DFS spanning tree에 포함된 간선은 tree edge와 back edge로 나뉜다.
따라서, WLOG,
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
|
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tp;
int n, m;
vector<int> par;
vector<int> depth;
vector<vector<pii> > adj;
vector<int> ans;
void dfs(int u, int p, int d) {
par[u] = p;
depth[u] = d;
for (auto [v, idx]: adj[u]) {
if (v != p && depth[v] == -1) {
dfs(v, u, d+1);
ans[idx] = 1;
}
}
}
int main(void) {
cin.tie(NULL);
ios::sync_with_stdio(false);
int t; cin >> t;
while (t--) {
cin >> n >> m;
par.clear(); par.resize(n+1);
depth.clear(); depth.resize(n+1, -1);
adj.clear(); adj.resize(n+1);
ans.clear(); ans.resize(m);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
dfs(1, 0, 0);
if (m == n + 2) {
vector<int> vertex;
for (int i = 1; i <= n; i++) {
for (auto [j, idx]: adj[i]) {
if (ans[idx] == 0) {
vertex.push_back(i);
vertex.push_back(j);
}
}
}
sort(all(vertex));
vertex.erase(unique(all(vertex)), vertex.end());
if (vertex.size() == 3) {
int w = vertex[0];
int v = vertex[1];
if (depth[w] < depth[v]) swap(w, v);
int idx1, idx2;
for (auto [i, idx]: adj[w]) {
if (i == v) idx1 = idx;
if (i == par[w]) idx2 = idx;
}
swap(ans[idx1], ans[idx2]);
}
}
for (int i: ans) {
cout << i;
}
cout << '\n';
}
return 0;
}
|
cs |
'Codeforces' 카테고리의 다른 글
Codeforces #763 (0) | 2022.01.02 |
---|---|
Codeforces Educational #120 (0) | 2022.01.02 |
Codeforces Global #18 (0) | 2021.12.26 |
Codeforces Educational #82 (0) | 2021.12.26 |
Codeforces #619 (0) | 2021.12.23 |