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

Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)315

[๋ฐฑ์ค€,c++] 14627๋ฒˆ - ํŒŒ๋‹ญํŒŒ๋‹ญ ๋ฌธ์ œ 14627๋ฒˆ: ํŒŒ๋‹ญํŒŒ๋‹ญ ์ฒซ์งธ ์ค„์— ์Šน๊ท ์ด๊ฐ€ ์‹œ์žฅ์—์„œ ์‚ฌ ์˜จ ํŒŒ์˜ ๊ฐœ์ˆ˜ S(1 ≤ S ≤ 1,000,000), ๊ทธ๋ฆฌ๊ณ  ์ฃผ๋ฌธ๋ฐ›์€ ํŒŒ๋‹ญ์˜ ์ˆ˜ C(1 ≤ C ≤ 1,000,000)๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค. ํŒŒ์˜ ๊ฐœ์ˆ˜๋Š” ํ•ญ์ƒ ํŒŒ๋‹ญ์˜ ์ˆ˜๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค. (S ≤ C) ๊ทธ ํ›„, S ์ค„์— www.acmicpc.net ์ฝ”๋“œ #include #include #define ll long long int using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); ll S, C; cin>>S>>C; vector v(S); for(int i=0; i>v[i]; ll start = 1, end = 1e9; ll mod = 0, sum = 0; while(s.. 2022. 9. 13.
[๋ฐฑ์ค€,c++] 2792๋ฒˆ - ๋ณด์„ ์ƒ์ž ๋ฌธ์ œ 2792๋ฒˆ: ๋ณด์„ ์ƒ์ž ๋ณด์„ ๊ณต์žฅ์—์„œ ๋ณด์„ ์ƒ์ž๋ฅผ ์œ ์น˜์›์— ๊ธฐ์ฆํ–ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ณด์„์€ M๊ฐ€์ง€ ์„œ๋กœ ๋‹ค๋ฅธ ์ƒ‰์ƒ ์ค‘ ํ•œ ์ƒ‰์ƒ์ด๋‹ค. ์›์žฅ ์„ ์ƒ๋‹˜์€ ๋ชจ๋“  ๋ณด์„์„ N๋ช…์˜ ํ•™์ƒ๋“ค์—๊ฒŒ ๋‚˜๋ˆ„์–ด ์ฃผ๋ ค๊ณ  ํ•œ๋‹ค. ์ด๋•Œ, ๋ณด์„์„ ๋ฐ›์ง€ ๋ชปํ•˜ www.acmicpc.net ์ฝ”๋“œ #include #include #define ll unsigned long long int using namespace std; int N, M; vector v; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin>>N>>M; ll max_find = 0; for(int i=0; i>inp; v.push_back(inp); max_find += inp; } ll start = 1, end =.. 2022. 9. 8.
[๋ฐฑ์ค€,c++] 6236๋ฒˆ - ์šฉ๋ˆ ๊ด€๋ฆฌ ๋ฌธ์ œ 6236๋ฒˆ: ์šฉ๋ˆ ๊ด€๋ฆฌ ํ˜„์šฐ๋Š” ์šฉ๋ˆ์„ ํšจ์œจ์ ์œผ๋กœ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ณ„ํš์„ ์งœ๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ํ˜„์šฐ๋Š” ์•ž์œผ๋กœ N์ผ ๋™์•ˆ ์ž์‹ ์ด ์‚ฌ์šฉํ•  ๊ธˆ์•ก์„ ๊ณ„์‚ฐํ•˜์˜€๊ณ , ๋ˆ์„ ํŽ‘ํŽ‘ ์“ฐ์ง€ ์•Š๊ธฐ ์œ„ํ•ด ์ •ํ™•ํžˆ M๋ฒˆ๋งŒ ํ†ต์žฅ์—์„œ ๋ˆ์„ ๋นผ์„œ ์“ฐ๊ธฐ๋กœ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; int N, M, max_money; vector v; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin>>N>>M; for(int i=0; i>inp; v.push_back(inp); max_money += inp; } int ans = 0; int start = 1, end = max_money; // ์ตœ๋Œ€ .. 2022. 9. 8.
[๋ฐฑ์ค€,c++] 20040๋ฒˆ - ์‚ฌ์ดํด ๊ฒŒ์ž„ ๋ฌธ์ œ 20040๋ฒˆ: ์‚ฌ์ดํด ๊ฒŒ์ž„ ์‚ฌ์ดํด ๊ฒŒ์ž„์€ ๋‘ ๋ช…์˜ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ฐจ๋ก€๋Œ€๋กœ ๋Œ์•„๊ฐ€๋ฉฐ ์ง„ํ–‰ํ•˜๋Š” ๊ฒŒ์ž„์œผ๋กœ, ์„  ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ํ™€์ˆ˜ ๋ฒˆ์งธ ์ฐจ๋ก€๋ฅผ, ํ›„ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์ง์ˆ˜ ๋ฒˆ์งธ ์ฐจ๋ก€๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. ๊ฒŒ์ž„ ์‹œ์ž‘ ์‹œ 0 ๋ถ€ํ„ฐ n − 1 ๊นŒ์ง€ ๊ณ ์œ ํ•œ www.acmicpc.net ์ฝ”๋“œ #include using namespace std; int parent[500001]; int n, m; int find_parent(int x){ if(x == parent[x]) return x; else return parent[x] = find_parent(parent[x]); } void union_parent(int a, int b){ a = find_parent(a); b = find_parent(b); if(a < b) parent.. 2022. 9. 7.
[๋ฐฑ์ค€,c++] 11561๋ฒˆ - ์ง•๊ฒ€๋‹ค๋ฆฌ ๋ฌธ์ œ 11561๋ฒˆ: ์ง•๊ฒ€๋‹ค๋ฆฌ ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ํ•œ ์ค„์— ์Šนํƒ์ด๊ฐ€ ๋ฐŸ์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ์ง•๊ฒ€๋‹ค๋ฆฌ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ์ฝ”๋“œ #include #include #define ll unsigned long long int using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int T; cin>>T; while(T--){ ll n; cin>>n; ll start = 1, end = 1e16; ll ans = 0; while(start 2022. 9. 7.
[๋ฐฑ์ค€,c++] 11663๋ฒˆ - ์„ ๋ถ„ ์œ„์˜ ์  ๋ฌธ์ œ 11663๋ฒˆ: ์„ ๋ถ„ ์œ„์˜ ์  ์ฒซ์งธ ์ค„์— ์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ์„ ๋ถ„์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N, M ≤ 100,000) ๋‘˜์งธ ์ค„์—๋Š” ์ ์˜ ์ขŒํ‘œ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘ ์ ์ด ๊ฐ™์€ ์ขŒํ‘œ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค. ์…‹์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ์„ ๋ถ„์˜ ์‹œ์ž‘์ ๊ณผ www.acmicpc.net ์ฝ”๋“œ #include #include #include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int N, M; cin>>N>>M; vector v; for(int i=0; i>inp; v.push_back(inp); } sort(v.begin(), v.end()); for(int i=0; i>a>>b; int first = lowe.. 2022. 9. 6.
[๋ฐฑ์ค€,c++] 17136๋ฒˆ - ์ƒ‰์ข…์ด ๋ถ™์ด๊ธฐ ๋ฌธ์ œ 17136๋ฒˆ: ์ƒ‰์ข…์ด ๋ถ™์ด๊ธฐ ๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์„ ํ•œ ๋‹ค์„ฏ ์ข…๋ฅ˜์˜ ์ƒ‰์ข…์ด๊ฐ€ ์žˆ๋‹ค. ์ƒ‰์ข…์ด์˜ ํฌ๊ธฐ๋Š” 1×1, 2×2, 3×3, 4×4, 5×5๋กœ ์ด ๋‹ค์„ฏ ์ข…๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉฐ, ๊ฐ ์ข…๋ฅ˜์˜ ์ƒ‰์ข…์ด๋Š” 5๊ฐœ์”ฉ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ƒ‰์ข…์ด๋ฅผ ํฌ www.acmicpc.net ์ฝ”๋“œ #include #include using namespace std; int N = 10, ans = 1e9; int map[10][10]; int paper_cnt; int blank_cnt; int paper_sum[6] = {5, 5, 5, 5, 5, 5}; bool paper_attach(int x, int y, int size){ for(int i = x; i < x+size; i++){ for(int k = y; k < y+size;.. 2022. 9. 6.
[๋ฐฑ์ค€,c++] 2533๋ฒˆ - ์‚ฌํšŒ๋ง ์„œ๋น„์Šค(SNS) ๋ฌธ์ œ 2533๋ฒˆ: ์‚ฌํšŒ๋ง ์„œ๋น„์Šค(SNS) ํŽ˜์ด์Šค๋ถ, ํŠธ์œ„ํ„ฐ, ์นด์นด์˜คํ†ก๊ณผ ๊ฐ™์€ ์‚ฌํšŒ๋ง ์„œ๋น„์Šค(SNS)๊ฐ€ ๋„๋ฆฌ ์‚ฌ์šฉ๋จ์— ๋”ฐ๋ผ, ์‚ฌํšŒ๋ง์„ ํ†ตํ•˜์—ฌ ์‚ฌ๋žŒ๋“ค์ด ์–ด๋–ป๊ฒŒ ์ƒˆ๋กœ์šด ์•„์ด๋””์–ด๋ฅผ ๋ฐ›์•„๋“ค์ด๊ฒŒ ๋˜๋Š”๊ฐ€๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ค‘์š”ํ•ด์กŒ๋‹ค. ์‚ฌํšŒ๋ง www.acmicpc.net ์ฝ”๋“œ /* ๋ณธ์ธ์ด ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๋ผ๋ฉด ์ž์‹๋…ธ๋“œ๋Š” ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ ์ด๊ฑฐ๋‚˜ ์•„๋‹ˆ์–ด๋„ ๋œ๋‹ค. ๋ณธ์ธ์ด ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์ž์‹ ๋…ธ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์—ฌ์•ผ ํ•œ๋‹ค. */ #include #include using namespace std; vectorgraph[1000001]; int dp[1000001][2]; //๋ณธ์ธ์ด ์–ผ๋ฆฌ๋‹ตํ„ฐ์ผ๋–„, ๋ณธ์ธ์ด ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๊ฐ€ ์•„๋‹๋•Œ int N; void dfs(int node, int before){ dp[node][0] = 0; dp[.. 2022. 9. 4.
[๋ฐฑ์ค€,c++] 15681๋ฒˆ - ํŠธ๋ฆฌ์™€ ์ฟผ๋ฆฌ ๋ฌธ์ œ 15681๋ฒˆ: ํŠธ๋ฆฌ์™€ ์ฟผ๋ฆฌ ํŠธ๋ฆฌ์˜ ์ •์ ์˜ ์ˆ˜ N๊ณผ ๋ฃจํŠธ์˜ ๋ฒˆํ˜ธ R, ์ฟผ๋ฆฌ์˜ ์ˆ˜ Q๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 105, 1 ≤ R ≤ N, 1 ≤ Q ≤ 105) ์ด์–ด N-1์ค„์— ๊ฑธ์ณ, U V์˜ ํ˜•ํƒœ๋กœ ํŠธ๋ฆฌ์— ์†ํ•œ ๊ฐ„์„ ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ U, V ≤ N, U ≠ V) www.acmicpc.net ์ฝ”๋“œ #include #include using namespace std; int N, R, Q; int max_depth = -1, max_node; vectorgraph[100001]; int subnode_cnt[100001]; int dfs(int node, int before){ for(auto next: graph[node]){ if(next != before){ subnode_cnt[.. 2022. 9. 4.