[BOJ] 2787 흔한 수열 문제

흔한 수열 문제

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

#define MAX_N 202
#define ll long long
#define vll vector<ll>
#define all(a) begin(a), end(a)
using namespace std;

ll n, m;
ll match[MAX_N], seen[MAX_N];

vector<vll> pad(MAX_N, vll(MAX_N, 1));
vll mi(MAX_N, 0), ma(MAX_N, MAX_N - 1);


ll dfs(ll u, ll s, vll& res) {
seen[u] = s;
for(ll i = 1; i <= n; i++) {
if(!pad[u][i]) continue;
if(match[i] == -1 or (seen[match[i]] != s and dfs(match[i], s, res))) {
res[u] = i;
match[i] = u;
return 1;
}
}
return 0;
}


void init() {
for(ll i = 1; i <= n; i++) {
for(ll j = 1; j < mi[i]; j++) pad[i][j] = 0;
for(ll j = ma[i] + 1; j <= n; j++) pad[i][j] = 0;
}
memset(match, -1, sizeof(match));
memset(seen, -1, sizeof(seen));

for(int i = 1; i <= n; i++) {
for(int j = 1; j<= n; j++) {
cout<<pad[i][j]<<' ';
}cout<<endl;
}
}

vector<ll> solve() {
init();

vll res(n + 1, -1);

for(ll i = 1; i <= n; i++) {
if(res[i] == -1)
if(!dfs(i,i,res))
return {-1};
}

return vll(begin(res) + 1, end(res));
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;

while(m--) {
ll op, f, e, v;
cin>>op>>f>>e>>v;
auto& ref = op == 1 ? ma : mi;
for(ll i = 1; i <= n; i++) {
if(f <= i and i <= e) ref[i] = op == 1 ? min(ref[i], v) : max(ref[i], v);
else pad[i][v] = 0;
}
}

auto res = solve();
for(auto& v : res) cout<<v<<' ';
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/22/PS/BOJ/2787/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.