๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป457

[๋ฐฑ์ค€,c++] 2812๋ฒˆ - ํฌ๊ฒŒ ๋งŒ๋“ค๊ธฐ ๋ฌธ์ œ 2812๋ฒˆ: ํฌ๊ฒŒ ๋งŒ๋“ค๊ธฐ N์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์—ฌ๊ธฐ์„œ ์ˆซ์ž K๊ฐœ๋ฅผ ์ง€์›Œ์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int N, K; cin>>N>>K; string num; cin>>num; stack st; int remove_cnt = 0; int i=0; for(i; inum[i]) st.push(num[i]); else{ while(true){ if(!st.empty() && num[i]>st.top()){ if(remove_cnt == K) br.. 2022. 7. 29.
[๋ฐฑ์ค€,c++] 13164๋ฒˆ - ํ–‰๋ณต ์œ ์น˜์› ๋ฌธ์ œ 13164๋ฒˆ: ํ–‰๋ณต ์œ ์น˜์› ํ–‰๋ณต ์œ ์น˜์› ์›์žฅ์ธ ํƒœ์–‘์ด๋Š” ์–ด๋Š ๋‚  N๋ช…์˜ ์›์ƒ๋“ค์„ ํ‚ค ์ˆœ์„œ๋Œ€๋กœ ์ผ๋ ฌ๋กœ ์ค„ ์„ธ์šฐ๊ณ , ์ด K๊ฐœ์˜ ์กฐ๋กœ ๋‚˜๋ˆ„๋ ค๊ณ  ํ•œ๋‹ค. ๊ฐ ์กฐ์—๋Š” ์›์ƒ์ด ์ ์–ด๋„ ํ•œ ๋ช… ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ๊ฐ™์€ ์กฐ์— ์†ํ•œ ์›์ƒ๋“ค์€ ์„œ๋กœ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int N, K; cin>>N>>K; vector v(N), cost(N-1); for(int i=0; i>v[i]; sort(v.begin(), v.end()); for(int i=1; i 2022. 7. 27.
[๋ฐฑ์ค€,c++] 2138๋ฒˆ - ์ „๊ตฌ์™€ ์Šค์œ„์น˜ ๋ฌธ์ œ 2138๋ฒˆ: ์ „๊ตฌ์™€ ์Šค์œ„์น˜ N๊ฐœ์˜ ์Šค์œ„์น˜์™€ N๊ฐœ์˜ ์ „๊ตฌ๊ฐ€ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ „๊ตฌ๋Š” ์ผœ์ ธ ์žˆ๋Š” ์ƒํƒœ์™€ ๊บผ์ ธ ์žˆ๋Š” ์ƒํƒœ ์ค‘ ํ•˜๋‚˜์˜ ์ƒํƒœ๋ฅผ ๊ฐ€์ง„๋‹ค. i(1 N; string blub, make; cin>>blub>>make; //0๋ฒˆ ์•ˆ๋ˆŒ๋ ธ์„ ๋•Œ bool flag1 = check(blub, make, 1); if.. 2022. 7. 27.
[๋ฐฑ์ค€,c++] 1167๋ฒˆ - ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ ๋ฌธ์ œ 1167๋ฒˆ: ํŠธ๋ฆฌ์˜ ์ง€๋ฆ„ ํŠธ๋ฆฌ๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒซ ๋ฒˆ์งธ ์ค„์—์„œ๋Š” ํŠธ๋ฆฌ์˜ ์ •์ ์˜ ๊ฐœ์ˆ˜ V๊ฐ€ ์ฃผ์–ด์ง€๊ณ  (2 ≤ V ≤ 100,000)๋‘˜์งธ ์ค„๋ถ€ํ„ฐ V๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ„์„ ์˜ ์ •๋ณด๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฃผ์–ด์ง„๋‹ค. ์ •์  ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ V๊นŒ์ง€ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; vector graph[100001]; int max_len = 0, max_node; bool visited[100001]; void find_max_len(int node, int len){ visited[node] = true; if(len>max_len){ max_node = node; max_len = len; } for(auto next: .. 2022. 7. 26.
[๋ฐฑ์ค€,c++] 9489๋ฒˆ - ์‚ฌ์ดŒ ๋ฌธ์ œ 9489๋ฒˆ: ์‚ฌ์ดŒ ์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ๋…ธ๋“œ์˜ ์ˆ˜ n๊ณผ ์‚ฌ์ดŒ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ k๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ n ≤ 1,000, 1 ≤ k ≤ 1,000,000) ๋‹ค์Œ ์ค„ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; int graph[1000001]; vector idx; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n, k; while(cin>>n>>k){ if(n==0 && k==0) break; int parent_idx = -1, before = 0, ans = 0; for(int i.. 2022. 7. 26.
[๋ฐฑ์ค€,c++] 20924๋ฒˆ - ํŠธ๋ฆฌ์˜ ๊ธฐ๋‘ฅ๊ณผ ๊ฐ€์ง€ ๋ฌธ์ œ 20924๋ฒˆ: ํŠธ๋ฆฌ์˜ ๊ธฐ๋‘ฅ๊ณผ ๊ฐ€์ง€ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ $N$($1 \le N \le 200\,000$)๊ณผ ๋ฃจํŠธ ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ $R$($1 \le R \le N$)์ด ์ฃผ์–ด์ง„๋‹ค. ์ดํ›„ $N-1$๊ฐœ์˜ ์ค„์— ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ $a$, $b$, $d$($1 \le a, b \le N$, $ a \ne b$)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” $a$๋ฒˆ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; vectortree[200001]; bool visited[200001]; int trunk_len, start_branch, branch_len, end_branch; void find_trunk(int node, int len){ visited[node.. 2022. 7. 24.
[๋ฐฑ์ค€,c++] 20365๋ฒˆ - ๋ธ”๋กœ๊ทธ2 ๋ฌธ์ œ 20365๋ฒˆ: ๋ธ”๋กœ๊ทธ2 neighbor ๋ธ”๋กœ๊ทธ๋ฅผ ์šด์˜ํ•˜๋Š” ์ผ์šฐ๋Š” ๋งค์ผ ์•„์นจ ํ’€๊ณ  ์‹ถ์€ ๋ฌธ์ œ๋ฅผ ๋ฏธ๋ฆฌ ์ •ํ•ด๋†“๊ณ  ๊ธ€์„ ์˜ฌ๋ฆฐ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งค์ผ ๋ฐค ๊ฐ๊ฐ์˜ ๋ฌธ์ œ์— ๋Œ€ํ•˜์—ฌ, ํ•ด๊ฒฐํ•œ ๊ฒฝ์šฐ ํŒŒ๋ž€์ƒ‰, ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ์น ํ•œ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int N; cin>>N; string s; cin>>s; char before = 'B'; bool flag = true; int job = 1; for(int i=0; i 2022. 7. 24.
[๋ฐฑ์ค€,c++] 12933๋ฒˆ - ์˜ค๋ฆฌ ๋ฌธ์ œ 12933๋ฒˆ: ์˜ค๋ฆฌ ์ฒซ์งธ ์ค„์— ์˜์„ ์ด๊ฐ€ ๋…น์Œํ•œ ์†Œ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์†Œ๋ฆฌ์˜ ๊ธธ์ด๋Š” 5๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 2500๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , 'q','u','a','c','k'๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. www.acmicpc.net ์ฝ”๋“œ #include #include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); string inp; cin>>inp; int ans = 0; for(int i=0; i 2022. 7. 19.
[๋ฐฑ์ค€,c++] 1343๋ฒˆ - ํด๋ฆฌ์˜ค๋ฏธ๋…ธ ๋ฌธ์ œ 1343๋ฒˆ: ํด๋ฆฌ์˜ค๋ฏธ๋…ธ ์ฒซ์งธ ์ค„์— ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์„œ๋Š” ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ๋ฎ์„ ์ˆ˜ ์—†์œผ๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ์ฝ”๋“œ #include #include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); string s; cin>>s; s = regex_replace(s, regex("XXXX"), "AAAA"); s = regex_replace(s, regex("XX"), "BB"); if(s.find("X") != -1) cout 2022. 7. 14.