際際滷

際際滷Share a Scribd company logo
STUDENTSKO
BOJ 10547
2020-12-04
蟾覺
SAMPLE INPUT/OUTPUT
9 12 5 13 5 9 12 13
16 2 1 7 5 10 2 1 7 5 16 10
7 9 8 3 6 5 7 9 8
3 6 5
4 1
9 12 5 13
6 2
16 2 1 7 5 10
6 3
7 9 8 3 6 5
1
1
3
SOLUTION
 牛 螳 螳 覈 覯讌 觚襦朱 螳 讌 螻壱.
(觚襦 伎  覦企 觚襦 覦讌 .)
企  襭  讌 螳 .
磯殊 企  襭  螳襯 碁 旧 谿場  .
螳  觚襦 豕 讀螳 覿覿 (LIS) 蠍語願 企  襭  螳企.
CODE
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int N, K;
vector<int> v;
int main() {
cin >> N >> K;
v.assign(N, 0);
for (int i = 0; i < N; i++) {
cin >> v[i];
}
vector<pair<int, int>> sorted;
vector<pair<int, int>> unsorted;
for (int i = 0; i < N; i++) {
sorted.push_back(make_pair(v[i], i));
unsorted.push_back(make_pair(v[i], i));
}
sort(sorted.begin(), sorted.end());
for (int i = 0; i < sorted.size(); i++) {
unsorted[sorted[i].second].second = i / K + 1;
}
int max_idx = 0; //LIS(Longest Increasing Subsequence)
vector<int> dp(N);
for (int i = 0; i < N; i++) {
int idx = upper_bound(dp.begin(), dp.begin() + max_idx, unsorted[i].second) - dp.begin();
if(idx == max_idx)max_idx++;
dp[idx] = unsorted[i].second;
}
cout << N - max_idx << endl;
return 0;
}
EXPLANATION
豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS
[3, 5, 1, 9, 2, 1, 4, 8]螻 螳 伎  襯 蟇壱   覿覿 伎 蟲 蠍語願 豕螳  覿
覿 伎 谿城 覓語螳 豕 讀螳 覿覿  覓語企.
螻襴讀 襴覃  螳.
 襯 DP 覦一伎  襦 伎企. (曙 X)
襯 れ DP 覦一伎 [1, 4, 8] る 3 企 伎  讌螳 螳企. 4襯 3朱 豌危る [1, 3,
8]   讌  .
DP 覦一伎 蠍語願 豕 讀螳 覿覿 伎 蠍語伎企.
EXPLANATION
豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS
idx
max
dp覦一伎 蠍磯 1伎螻 3 れ願 螻糾  覦 朱 蠏碁襦 曙.
EXPLANATION
豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS
idx
max
dp 覦一伎 [3][ ] 伎螻 5 襷讌襷 螳 伎 .
EXPLANATION
豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS
idx
max
dp 覦一伎 [3][5][ ] 伎螻, 1 襷 豌 螳 伎 .
襷 れ れ願 蟆曙郁 覃 dp 覦一伎 蠍語企 企讌 .
EXPLANATION
豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS
idx
max
dp 覦一伎 [1][5][ ] 伎螻, 9 襷讌襷 螳 伎 .
EXPLANATION
豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS
idx
max
dp 覦一伎 [1][5][9][ ] 伎螻 2 5 襴襯 豌危覃 .
EXPLANATION
豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS
idx
max
dp 覦一伎 [1][2][9][ ] 伎螻 1 襷 朱 螳覃 .
EXPLANATION
豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS
idx
max
dp 覦一伎 [1][2][9][ ] 伎螻 4 9 襴襯 豌危.
EXPLANATION
豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS
idx
max
dp 覦一伎 [1][2][4][ ] 伎螻 8 襷讌襷 襴襦 螳覃 .
豕 讀螳 覿覿 伎 蠍語願 4企.

More Related Content

Similar to BOJ10547_STUDENTSKO_LIS(Longest Increasing Subsequence) Algorithm (20)

[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 : (蠍磯蓋, , 豐
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 :  (蠍磯蓋, , 豐[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 :  (蠍磯蓋, , 豐
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 : (蠍磯蓋, , 豐
S.O.P.T - Shout Our Passion Together
Project#2襷 Hwp
Project#2襷 HwpProject#2襷 Hwp
Project#2襷 Hwp
Kimjeongmoo
02. data structure and stl
02. data structure and stl02. data structure and stl
02. data structure and stl
麹 譟
貊ろ 蟆 蠍 C++ 06_07 ろ螻 螳 .
貊ろ 蟆 蠍 C++ 06_07 ろ螻   螳 .貊ろ 蟆 蠍 C++ 06_07 ろ螻   螳 .
貊ろ 蟆 蠍 C++ 06_07 ろ螻 螳 .
ultrasuperrok
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
skku_npc
[ aloha] 襦蠏碁覦 蟆曙 覓語_Advanced part
[ aloha] 襦蠏碁覦 蟆曙 覓語_Advanced part[ aloha] 襦蠏碁覦 蟆曙 覓語_Advanced part
[ aloha] 襦蠏碁覦 蟆曙 覓語_Advanced part
NAVER D2
Tensorflow
TensorflowTensorflow
Tensorflow
chs71
襭蟲譟2覲願
襭蟲譟2覲願襭蟲譟2覲願
襭蟲譟2覲願
KimChangHoen
2012 Ds 01
2012 Ds 012012 Ds 01
2012 Ds 01
Jungyerin
[D2 CAMPUS] 覿磯 Alcall 襦蠏碁覦 蟆曙 覓語
[D2 CAMPUS] 覿磯 Alcall 襦蠏碁覦 蟆曙 覓語 [D2 CAMPUS] 覿磯 Alcall 襦蠏碁覦 蟆曙 覓語
[D2 CAMPUS] 覿磯 Alcall 襦蠏碁覦 蟆曙 覓語
NAVER D2
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Eunjin Song
Programming java day2
Programming java day2Programming java day2
Programming java day2
Jaehoonyam
襭蟲譟 Project5
襭蟲譟 Project5襭蟲譟 Project5
襭蟲譟 Project5
KoChungWook
Erlang梶 求釈= swap メ
Erlang梶 求釈= swap メErlang梶 求釈= swap メ
Erlang梶 求釈= swap メ
Jaejin Yun
Clean code(02)
Clean code(02)Clean code(02)
Clean code(02)
蠏 蟾
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
S.O.P.T - Shout Our Passion Together
R 襦蠏碁覦-レ 一危 譟一
R 襦蠏碁覦-レ 一危 譟一R 襦蠏碁覦-レ 一危 譟一
R 襦蠏碁覦-レ 一危 譟一
Terry Cho
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 : (蠍磯蓋, , 豐
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 :  (蠍磯蓋, , 豐[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 :  (蠍磯蓋, , 豐
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #03 : (蠍磯蓋, , 豐
S.O.P.T - Shout Our Passion Together
Project#2襷 Hwp
Project#2襷 HwpProject#2襷 Hwp
Project#2襷 Hwp
Kimjeongmoo
02. data structure and stl
02. data structure and stl02. data structure and stl
02. data structure and stl
麹 譟
貊ろ 蟆 蠍 C++ 06_07 ろ螻 螳 .
貊ろ 蟆 蠍 C++ 06_07 ろ螻   螳 .貊ろ 蟆 蠍 C++ 06_07 ろ螻   螳 .
貊ろ 蟆 蠍 C++ 06_07 ろ螻 螳 .
ultrasuperrok
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
skku_npc
[ aloha] 襦蠏碁覦 蟆曙 覓語_Advanced part
[ aloha] 襦蠏碁覦 蟆曙 覓語_Advanced part[ aloha] 襦蠏碁覦 蟆曙 覓語_Advanced part
[ aloha] 襦蠏碁覦 蟆曙 覓語_Advanced part
NAVER D2
Tensorflow
TensorflowTensorflow
Tensorflow
chs71
襭蟲譟2覲願
襭蟲譟2覲願襭蟲譟2覲願
襭蟲譟2覲願
KimChangHoen
2012 Ds 01
2012 Ds 012012 Ds 01
2012 Ds 01
Jungyerin
[D2 CAMPUS] 覿磯 Alcall 襦蠏碁覦 蟆曙 覓語
[D2 CAMPUS] 覿磯 Alcall 襦蠏碁覦 蟆曙 覓語 [D2 CAMPUS] 覿磯 Alcall 襦蠏碁覦 蟆曙 覓語
[D2 CAMPUS] 覿磯 Alcall 襦蠏碁覦 蟆曙 覓語
NAVER D2
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Eunjin Song
Programming java day2
Programming java day2Programming java day2
Programming java day2
Jaehoonyam
襭蟲譟 Project5
襭蟲譟 Project5襭蟲譟 Project5
襭蟲譟 Project5
KoChungWook
Erlang梶 求釈= swap メ
Erlang梶 求釈= swap メErlang梶 求釈= swap メ
Erlang梶 求釈= swap メ
Jaejin Yun
Clean code(02)
Clean code(02)Clean code(02)
Clean code(02)
蠏 蟾
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
[SOPT] 一危 蟲譟 覦 螻襴讀 ろ磯 - #01 : 螳, 蠏殊 覲旧°, 覦一, 郁屋襴ろ
S.O.P.T - Shout Our Passion Together
R 襦蠏碁覦-レ 一危 譟一
R 襦蠏碁覦-レ 一危 譟一R 襦蠏碁覦-レ 一危 譟一
R 襦蠏碁覦-レ 一危 譟一
Terry Cho

More from Bomm Kim (6)

ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slideML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
Bomm Kim
[UNET]Segmentation model, a representative UNet, and a slide for understanding
[UNET]Segmentation model, a representative UNet, and a slide for understanding[UNET]Segmentation model, a representative UNet, and a slide for understanding
[UNET]Segmentation model, a representative UNet, and a slide for understanding
Bomm Kim
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
Bomm Kim
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
Bomm Kim
SegmentTree Datastructure description and implementation slides
SegmentTree Datastructure description and implementation slidesSegmentTree Datastructure description and implementation slides
SegmentTree Datastructure description and implementation slides
Bomm Kim
BOJ4743_ConvexHull,Polygon Algorithm and C++ code
BOJ4743_ConvexHull,Polygon Algorithm and C++ codeBOJ4743_ConvexHull,Polygon Algorithm and C++ code
BOJ4743_ConvexHull,Polygon Algorithm and C++ code
Bomm Kim
ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slideML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
ML(KNN, Decision Tree), DL(RCNN, YOLO) educational slide
Bomm Kim
[UNET]Segmentation model, a representative UNet, and a slide for understanding
[UNET]Segmentation model, a representative UNet, and a slide for understanding[UNET]Segmentation model, a representative UNet, and a slide for understanding
[UNET]Segmentation model, a representative UNet, and a slide for understanding
Bomm Kim
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
[FreivaldsAlgorithm]The Freivalds Algorithm is an algorithm that determines w...
Bomm Kim
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
[Miller-Rabin] Detailed explanation of the Miller-Rabin prime number test and...
Bomm Kim
SegmentTree Datastructure description and implementation slides
SegmentTree Datastructure description and implementation slidesSegmentTree Datastructure description and implementation slides
SegmentTree Datastructure description and implementation slides
Bomm Kim
BOJ4743_ConvexHull,Polygon Algorithm and C++ code
BOJ4743_ConvexHull,Polygon Algorithm and C++ codeBOJ4743_ConvexHull,Polygon Algorithm and C++ code
BOJ4743_ConvexHull,Polygon Algorithm and C++ code
Bomm Kim

BOJ10547_STUDENTSKO_LIS(Longest Increasing Subsequence) Algorithm

  • 2. SAMPLE INPUT/OUTPUT 9 12 5 13 5 9 12 13 16 2 1 7 5 10 2 1 7 5 16 10 7 9 8 3 6 5 7 9 8 3 6 5 4 1 9 12 5 13 6 2 16 2 1 7 5 10 6 3 7 9 8 3 6 5 1 1 3
  • 3. SOLUTION 牛 螳 螳 覈 覯讌 觚襦朱 螳 讌 螻壱. (觚襦 伎 覦企 觚襦 覦讌 .) 企 襭 讌 螳 . 磯殊 企 襭 螳襯 碁 旧 谿場 . 螳 觚襦 豕 讀螳 覿覿 (LIS) 蠍語願 企 襭 螳企.
  • 4. CODE #include<iostream> #include<vector> #include<algorithm> using namespace std; int N, K; vector<int> v; int main() { cin >> N >> K; v.assign(N, 0); for (int i = 0; i < N; i++) { cin >> v[i]; } vector<pair<int, int>> sorted; vector<pair<int, int>> unsorted; for (int i = 0; i < N; i++) { sorted.push_back(make_pair(v[i], i)); unsorted.push_back(make_pair(v[i], i)); } sort(sorted.begin(), sorted.end()); for (int i = 0; i < sorted.size(); i++) { unsorted[sorted[i].second].second = i / K + 1; } int max_idx = 0; //LIS(Longest Increasing Subsequence) vector<int> dp(N); for (int i = 0; i < N; i++) { int idx = upper_bound(dp.begin(), dp.begin() + max_idx, unsorted[i].second) - dp.begin(); if(idx == max_idx)max_idx++; dp[idx] = unsorted[i].second; } cout << N - max_idx << endl; return 0; }
  • 5. EXPLANATION 豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS [3, 5, 1, 9, 2, 1, 4, 8]螻 螳 伎 襯 蟇壱 覿覿 伎 蟲 蠍語願 豕螳 覿 覿 伎 谿城 覓語螳 豕 讀螳 覿覿 覓語企. 螻襴讀 襴覃 螳. 襯 DP 覦一伎 襦 伎企. (曙 X) 襯 れ DP 覦一伎 [1, 4, 8] る 3 企 伎 讌螳 螳企. 4襯 3朱 豌危る [1, 3, 8] 讌 . DP 覦一伎 蠍語願 豕 讀螳 覿覿 伎 蠍語伎企.
  • 6. EXPLANATION 豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS idx max dp覦一伎 蠍磯 1伎螻 3 れ願 螻糾 覦 朱 蠏碁襦 曙.
  • 7. EXPLANATION 豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS idx max dp 覦一伎 [3][ ] 伎螻 5 襷讌襷 螳 伎 .
  • 8. EXPLANATION 豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS idx max dp 覦一伎 [3][5][ ] 伎螻, 1 襷 豌 螳 伎 . 襷 れ れ願 蟆曙郁 覃 dp 覦一伎 蠍語企 企讌 .
  • 9. EXPLANATION 豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS idx max dp 覦一伎 [1][5][ ] 伎螻, 9 襷讌襷 螳 伎 .
  • 10. EXPLANATION 豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS idx max dp 覦一伎 [1][5][9][ ] 伎螻 2 5 襴襯 豌危覃 .
  • 11. EXPLANATION 豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS idx max dp 覦一伎 [1][2][9][ ] 伎螻 1 襷 朱 螳覃 .
  • 12. EXPLANATION 豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS idx max dp 覦一伎 [1][2][9][ ] 伎螻 4 9 襴襯 豌危.
  • 13. EXPLANATION 豕 讀螳 覿覿 , Longest Increasing Subsequence, LIS idx max dp 覦一伎 [1][2][4][ ] 伎螻 8 襷讌襷 襴襦 螳覃 . 豕 讀螳 覿覿 伎 蠍語願 4企.