Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2071d9b

Browse files
committed
Longest increasing subsequence, Dynamic programming
1 parent 4dcf135 commit 2071d9b

File tree

8 files changed

+317
-4
lines changed

8 files changed

+317
-4
lines changed

‎Baekjoon/Cpp/11053.cpp‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int main()
6+
{
7+
ios::sync_with_stdio(false);
8+
cin.tie(NULL);
9+
cout.tie(NULL);
10+
int n, i, j, res = 1, arr[1001], dp[1001] = {};
11+
cin >> n;
12+
for (i = 0; i < n; i++)
13+
{
14+
cin >> arr[i];
15+
for (j = i - 1; j >= 0; j--)
16+
{
17+
if (arr[i] > arr[j] && dp[j] + 1 > dp[i])
18+
{
19+
dp[i] = dp[j] + 1;
20+
}
21+
else if (arr[i] == arr[j] && dp[j] > dp[i])
22+
{
23+
dp[i] = dp[j];
24+
}
25+
}
26+
res = max(res, dp[i] + 1);
27+
}
28+
cout << res;
29+
return 0;
30+
}

‎Baekjoon/Cpp/11054.cpp‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
#define MAX 1001
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
ios::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
cout.tie(NULL);
11+
int n, i, j, res = 1, arr[MAX], fdp[MAX], bdp[MAX];
12+
cin >> n;
13+
for (i = 0; i < n; i++)
14+
{
15+
cin >> arr[i];
16+
fdp[i] = 1;
17+
for (j = i - 1; j >= 0; j--)
18+
{
19+
if (arr[j] < arr[i] && fdp[j] + 1 > fdp[i])
20+
{
21+
fdp[i] = fdp[j] + 1;
22+
}
23+
else if (arr[j] == arr[i] && fdp[j] > fdp[i])
24+
{
25+
fdp[i] = fdp[j];
26+
}
27+
}
28+
}
29+
for (i = n - 1; i >= 0; i--)
30+
{
31+
bdp[i] = 1;
32+
for (j = i + 1; j < n; j++)
33+
{
34+
if (arr[j] < arr[i] && bdp[j] + 1 > bdp[i])
35+
{
36+
bdp[i] = bdp[j] + 1;
37+
}
38+
else if (arr[j] == arr[i] && bdp[j] > bdp[i])
39+
{
40+
bdp[i] = bdp[j];
41+
}
42+
}
43+
}
44+
for (i = 0; i < n; i++)
45+
{
46+
res = max(res, bdp[i] + fdp[i] - 1);
47+
}
48+
cout << res;
49+
return 0;
50+
}

‎Baekjoon/Cpp/11055.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int main()
6+
{
7+
ios::sync_with_stdio(false);
8+
cin.tie(NULL);
9+
cout.tie(NULL);
10+
int n, i, j, res = 1, arr[1001], dp[1001] = {};
11+
cin >> n;
12+
for (i = 0; i < n; i++)
13+
{
14+
cin >> arr[i];
15+
dp[i] = arr[i];
16+
for (j = i - 1; j >= 0; j--)
17+
{
18+
if (arr[i] > arr[j] && dp[j] + arr[i] > dp[i])
19+
{
20+
dp[i] = dp[j] + arr[i];
21+
}
22+
else if (arr[i] == arr[j] && dp[j] > dp[i])
23+
{
24+
dp[i] = dp[j];
25+
}
26+
}
27+
res = max(res, dp[i]);
28+
}
29+
cout << res;
30+
return 0;
31+
}

‎Baekjoon/Cpp/11722.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int main()
6+
{
7+
ios::sync_with_stdio(false);
8+
cin.tie(NULL);
9+
cout.tie(NULL);
10+
int i, j, n, res = 1, arr[1001], dp[1001];
11+
cin >> n;
12+
for (i = 0; i < n; i++)
13+
{
14+
cin >> arr[i];
15+
dp[i] = 1;
16+
for (j = n - 1; j >= 0; j--)
17+
{
18+
if (arr[i] < arr[j] && dp[j] + 1 > dp[i])
19+
{
20+
dp[i] = dp[j] + 1;
21+
}
22+
else if (arr[i] == arr[j] && dp[j] > dp[i])
23+
{
24+
dp[i] = dp[j];
25+
}
26+
}
27+
res = max(res, dp[i]);
28+
}
29+
cout << res;
30+
return 0;
31+
}

‎Baekjoon/Cpp/12015.cpp‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <bits/stdc++.h>
2+
#define MAX 1000001
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
ios::sync_with_stdio(0);
9+
cin.tie(0);
10+
cout.tie(0);
11+
int n, i, size, s, e, m, arr[MAX], lis[MAX];
12+
cin >> n;
13+
size = 0;
14+
for (i = 0; i < n; i++)
15+
{
16+
cin >> arr[i];
17+
if (i)
18+
{
19+
if (lis[size - 1] < arr[i])
20+
{
21+
lis[size] = arr[i];
22+
size += 1;
23+
}
24+
else
25+
{
26+
s = 0;
27+
e = size - 1;
28+
while (s < e)
29+
{
30+
m = (s + e) / 2;
31+
if (lis[m] >= arr[i])
32+
{
33+
e = m;
34+
}
35+
else
36+
{
37+
s = m + 1;
38+
}
39+
}
40+
lis[e] = arr[i];
41+
}
42+
}
43+
else
44+
{
45+
size += 1;
46+
lis[0] = arr[0];
47+
}
48+
}
49+
cout << size;
50+
return 0;
51+
}

‎Baekjoon/Cpp/12738.cpp‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <bits/stdc++.h>
2+
#define MAX 1000001
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
ios::sync_with_stdio(0);
9+
cin.tie(0);
10+
cout.tie(0);
11+
int n, i, size, s, e, m, arr[MAX], lis[MAX];
12+
cin >> n;
13+
size = 0;
14+
for (i = 0; i < n; i++)
15+
{
16+
cin >> arr[i];
17+
if (i)
18+
{
19+
if (lis[size - 1] < arr[i])
20+
{
21+
lis[size] = arr[i];
22+
size += 1;
23+
}
24+
else
25+
{
26+
s = 0;
27+
e = size - 1;
28+
while (s < e)
29+
{
30+
m = (s + e) / 2;
31+
if (lis[m] >= arr[i])
32+
{
33+
e = m;
34+
}
35+
else
36+
{
37+
s = m + 1;
38+
}
39+
}
40+
lis[e] = arr[i];
41+
}
42+
}
43+
else
44+
{
45+
size += 1;
46+
lis[0] = arr[0];
47+
}
48+
}
49+
cout << size;
50+
return 0;
51+
}

‎Baekjoon/Cpp/14003.cpp‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int main()
6+
{
7+
ios::sync_with_stdio(0);
8+
cin.tie(0);
9+
cout.tie(0);
10+
int n, i, s, e, m, size;
11+
vector<int> res;
12+
cin >> n;
13+
int arr[n + 1], lis[n + 1], dp[n + 1];
14+
size = 0;
15+
for (i = 0; i < n; i++)
16+
{
17+
cin >> arr[i];
18+
if (i > 0)
19+
{
20+
if (lis[size - 1] < arr[i])
21+
{
22+
size += 1;
23+
lis[size - 1] = arr[i];
24+
dp[i] = size - 1;
25+
}
26+
else
27+
{
28+
s = 0;
29+
e = size - 1;
30+
while (s < e)
31+
{
32+
m = (s + e) / 2;
33+
if (lis[m] >= arr[i])
34+
{
35+
e = m;
36+
}
37+
else
38+
{
39+
s = m + 1;
40+
}
41+
}
42+
lis[e] = arr[i];
43+
dp[i] = e;
44+
}
45+
}
46+
else
47+
{
48+
size += 1;
49+
lis[0] = arr[0];
50+
dp[0] = 0;
51+
}
52+
}
53+
cout << size << "\n";
54+
i = n - 1;
55+
while (size--)
56+
{
57+
while (dp[i] != size)
58+
{
59+
i -= 1;
60+
}
61+
res.push_back(arr[i]);
62+
}
63+
for (i = res.size() - 1; i >= 0; i--)
64+
{
65+
cout << res[i] << " ";
66+
}
67+
return 0;
68+
}

‎templates/example.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
using namespace std;
44
using ll = long long;
5-
using pll = pair<ll, ll>;
65

7-
int main() {
8-
ios::sync_with_stdio(0);
9-
cin.tie(0);
6+
int main()
7+
{
8+
ios::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
cout.tie(NULL);
1011

1112
return 0;
1213
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /