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

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

[๋ฐฑ์ค€,c++] 1991๋ฒˆ - ํŠธ๋ฆฌ ์ˆœํšŒ ๋ฌธ์ œ 1991๋ฒˆ: ํŠธ๋ฆฌ ์ˆœํšŒ ์ฒซ์งธ ์ค„์—๋Š” ์ด์ง„ ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 26)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ๋…ธ๋“œ์™€ ๊ทธ์˜ ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋…ธ๋“œ์˜ ์ด๋ฆ„์€ A๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์•ŒํŒŒ www.acmicpc.net ์ฝ”๋“œ #include using namespace std; int N; int tree[52][2]; void preorder(int curr){ if(curr==-1) return; coutb>>c; a = a - 65; if(b =='.') tree[a][0]= -1; else tree[a][0]=b-65; if(c =='.') tree[a][1]= -1; else tree[a][1]=c-65; } preorder(0); //์ „์œ„ ์ˆœํšŒ cou.. 2022. 3. 8.
[๋ฐฑ์ค€,c++] 1240๋ฒˆ - ๋…ธ๋“œ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ ๋ฌธ์ œ 1240๋ฒˆ: ๋…ธ๋“œ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ N(2≤N≤1,000)๊ฐœ์˜ ๋…ธ๋“œ๋กœ ์ด๋ฃจ์–ด์ง„ ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง€๊ณ  M(M≤1,000)๊ฐœ์˜ ๋‘ ๋…ธ๋“œ ์Œ์„ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ๋‘ ๋…ธ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ. www.acmicpc.net ์ฝ”๋“œ #include #include #include #define MAX 1e9 using namespace std; int N,M; vectorgraph[1001]; int visited[1001]; int ans; void dfs(int x, int y, int dist){ visited[x]=1; if(x==y){ ans = dist; return; } for(int i=0; i>N>>M; for(int i=0; i>a>>b>>dist; graph[a].push_back({b,dist}); gr.. 2022. 3. 8.
[๋ฐฑ์ค€,c++] 21918๋ฒˆ - ์ „๊ตฌ ๋ฌธ์ œ 21918๋ฒˆ: ์ „๊ตฌ $N$๊ฐœ์˜ ์ „๊ตฌ๊ฐ€ ์žˆ๊ณ  ๋งจ ์™ผ์ชฝ์— ์žˆ๋Š” ์ „๊ตฌ๋ฅผ ์ฒซ ๋ฒˆ์งธ๋ผ๊ณ  ํ•˜์ž. ์ „๊ตฌ์˜ ์ƒํƒœ๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์œผ๋ฉฐ ์ด๋ฅผ ์ˆซ์ž๋กœ ํ‘œํ˜„ํ•œ๋‹ค. $1$์€ ์ „๊ตฌ๊ฐ€ ์ผœ์ ธ ์žˆ๋Š” ์ƒํƒœ๋ฅผ ์˜๋ฏธํ•˜๊ณ , $0$์€ ์ „๊ตฌ๊ฐ€ ๊บผ์ ธ ์žˆ๋Š” ์ƒํƒœ๋ฅผ www.acmicpc.net ์ฝ”๋“œ #include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int N, M; cin>>N>>M; int bulb[4001]={0, }; for(int i=1; i>bulb[i]; } for(int i=0; i>a>>b>>c; if(a==1) bulb[b]=c; //1๋ฒˆ ๋ช…๋ น์–ด else if(a==2){ //2๋ฒˆ ๋ช…๋ น์–ด for(int i=b; i 2022. 3. 1.
[๋ฐฑ์ค€,c++] 14888๋ฒˆ - ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ ๋ฌธ์ œ 14888๋ฒˆ: ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ ์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(2 ≤ N ≤ 11)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” A1, A2, ..., AN์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ai ≤ 100) ์…‹์งธ ์ค„์—๋Š” ํ•ฉ์ด N-1์ธ 4๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ฐจ๋ก€๋Œ€๋กœ ๋ง์…ˆ(+)์˜ ๊ฐœ์ˆ˜, ๋บ„์…ˆ(-)์˜ ๊ฐœ์ˆ˜, www.acmicpc.net ์ฝ”๋“œ #include #include using namespace std; int N,add,sub,mul,divd; int max_v=-1e9-1, min_v = 1e9+1; void backtrack(int result,int index, vector&num, int add, int sub, int mul, int divd){ if(index==N){ max_v = max(max_v, result);.. 2022. 2. 17.
[๋ฐฑ์ค€,c++] 10211๋ฒˆ - Maximum Subarray ๋ฌธ์ œ 10211๋ฒˆ: Maximum Subarray ํฌ๊ธฐ N์ธ ์ •์ˆ˜ํ˜• ๋ฐฐ์—ด X๊ฐ€ ์žˆ์„ ๋•Œ, X์˜ ๋ถ€๋ถ„ ๋ฐฐ์—ด(X์˜ ์—ฐ์†ํ•œ ์ผ๋ถ€๋ถ„) ์ค‘ ๊ฐ ์›์†Œ์˜ ํ•ฉ์ด ๊ฐ€์žฅ ํฐ ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ์ฐพ๋Š” Maximum subarray problem(์ตœ๋Œ€ ๋ถ€๋ถ„๋ฐฐ์—ด ๋ฌธ์ œ)์€ ์ปดํ“จํ„ฐ ๊ณผํ•™์—์„œ ๋งค์šฐ ์ž˜ ์•Œ๋ ค์ ธ ์žˆ www.acmicpc.net ์ฝ”๋“œ #include #include int dp[1001],arr[1001]; using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int T; cin>>T; while(T--){ int n; cin>>n; for(int i=0; i>arr[i]; } int ans = arr[0]; dp[0]= arr[0]; for.. 2022. 1. 4.
[๋ฐฑ์ค€,c++] 9625๋ฒˆ - BABBA ๋ฌธ์ œ 9625๋ฒˆ: BABBA ์ƒ๊ทผ์ด๋Š” ๊ธธ์„ ๊ฑท๋‹ค๊ฐ€ ์‹ ๊ธฐํ•œ ๊ธฐ๊ณ„๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋‹ค. ๊ธฐ๊ณ„๋Š” ๋งค์šฐ ๋งค์šฐ ํฐ ํ™”๋ฉด๊ณผ ๋ฒ„ํŠผ ํ•˜๋‚˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊ธฐ๊ณ„๋ฅผ ๋ฐœ๊ฒฌํ–ˆ์„ ๋•Œ, ํ™”๋ฉด์—๋Š” A๋งŒ ํ‘œ์‹œ๋˜์–ด์ ธ ์žˆ์—ˆ๋‹ค. ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋‹ˆ ๊ธ€์ž๊ฐ€ B๋กœ ๋ณ€ํ–ˆ www.acmicpc.net ์ฝ”๋“œ #include using namespace std; int A[46],B[46]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin>>n; A[0]=1; B[0]=0; A[1]=0; B[1]=1; A[2]=1; B[2]=1; for(int i=3; i 2021. 12. 29.
[๋ฐฑ์ค€,c++] 15688๋ฒˆ - ์ˆ˜ ์ •๋ ฌํ•˜๊ธฐ5 ๋ฌธ์ œ https://www.acmicpc.net/problem/15688 15688๋ฒˆ: ์ˆ˜ ์ •๋ ฌํ•˜๊ธฐ 5 ์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด ์ˆ˜๋Š” ์ ˆ๋Œ“๊ฐ’์ด 1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋ฉฐ, ๊ฐ™์€ ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ์ค‘๋ณต๋  ์ˆ˜๋„ ์žˆ๋‹ค. www.acmicpc.net #include #include #include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vectorv; while (N--) { int num; cin >> num; v.push_back(num); } sort(v.begin().. 2021. 12. 14.
[๋ฐฑ์ค€,c++] 15666๋ฒˆ - N๊ณผ M (12) ๋ฌธ์ œ https://www.acmicpc.net/problem/15666 15666๋ฒˆ: N๊ณผ M (12) ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋˜๋ฉฐ, ๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด www.acmicpc.net ์ฝ”๋“œ #include #include #include #define max 8 using namespace std; int N, M; int arr[max]; bool visited[max]; vectorv; void dfs(int num, int start) { if (start == M) { for (int i = 0; i < M; i++) { cout M; for (int i = 0.. 2021. 12. 4.
[๋ฐฑ์ค€,c++] 15665๋ฒˆ - N๊ณผ M (11) ๋ฌธ์ œ https://www.acmicpc.net/problem/15665 15665๋ฒˆ: N๊ณผ M (11) ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋˜๋ฉฐ, ๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด www.acmicpc.net ์ฝ”๋“œ #include #include #include #define max 8 using namespace std; int N, M; int arr[max]; bool visited[max]; vectorv; void dfs(int start) { if (start == M) { for (int i = 0; i < M; i++) { cout M; for (int i = 0; i < N; .. 2021. 12. 4.