(๋ฐฑ์ค/ C++) 2629 - ์ํ์ ์ธ๐ coding test/โฝ ๋ฐฑ์ค2024. 7. 9. 18:05
Table of Contents
728x90
"(๋ฐฑ์ค/ C++) 2629 - ์ํ์ ์ธ"
https://www.acmicpc.net/problem/2629
์๋ง .. ์ํ์ ์ธ ๋ฌธ์ ์ค๋ช ์ ๋ ์ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํธ๋ ๋ฌธ์ ๋ผ๊ณ ์ ํ ์์ง ์์ผ๋ฉด ์ค๋ซ๋์ ๋ชป ํ์์๊ฑฐ ๊ฐ๋ค.. ^^;;
๋ ์๊ณผ ๊ฐ์ด ๋์ฌ์ ์๋ ๋ฌด๊ฒ์ ๊ฒฝ์ฐ๋ฅผ ๋ง๋๋ ๋ฐฉ์์ผ๋ก ํ์ด๋ฅผ ํ๋ค.
์ ํ์์ (๊ฐ)์ ์ธ์๋ง ์ถ๋ฅผ ๋์ ์๋ค๊ณ ์๊ฐ ํ๊ณ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ณ ๋ คํ์๋ค.
- (๊ฐ)์ A์ถ๊ฐ ์๋ ๊ฒฝ์ฐ → (๊ฐ) A์ถ + ๊ตฌ์ฌ = (๋) B์ถ → (๊ฐ) ๊ตฌ์ฌ = (๋) B์ถ - A์ถ
- (๋)์ A์ถ๊ฐ ์๋ ๊ฒฝ์ฐ → (๊ฐ) ๊ตฌ์ฌ = (๋) B์ถ + A์ถ
- (๊ฐ), (๋)์ A์ถ๊ฐ ๋ชจ๋ ์๋ ๊ฒฝ์ฐ → (๊ฐ) ๊ตฌ์ฌ = (๋) B์ถ
์ฒ์์๋ ์ด๋ถํ์์ผ๋ก ์ฐพ์ผ๋ ค ํ์ผ๋ ๋ฉ๋ชจ๋ฆฌ ๋ฌธ์ ๋ก ์คํจ ! DP[๋น๊ต๋์๊ฐ์][๋ฌด๊ฒ]๋ก ํ์!
#include <iostream>
#include <vector>
#include <algorithm>
#include<cmath>
using namespace std;
int gN, gTC;
int gWeight[31];
//vector<int> gAllSum;
bool dp[31][15001];
void DFS(int pParm01, int pParm02)
{
if (pParm01 > gN || dp[pParm01][pParm02])
{
return;
}
dp[pParm01][pParm02] = true;
DFS(pParm01 + 1, pParm02);
DFS(pParm01 + 1, abs(pParm02 - gWeight[pParm01]));
DFS(pParm01 + 1, pParm02 + gWeight[pParm01]);
}
int main(void)
{
cin >> gN;
for (int idx = 0; idx < gN; ++idx)
{
cin >> gWeight[idx];
}
DFS(0, 0);
//std::sort(gAllSum.begin(), gAllSum.end());
cin >> gTC;
for (int idx = 0; idx < gTC; ++idx)
{
int mTc;
cin >> mTc;
if (mTc > 15'000)
{
cout << "N";
}
else if (dp[gN][mTc])
{
cout << "Y";
}
else
{
cout << "N";
}
}
return 0;
}
728x90
'๐ coding test > โฝ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
@DoctorSunAhna :: ํํฌ์ฝ๋ฅ
์ ํ๋ ๊ฒ ๋ณด๋ค ๋ซ๊ฒ ์ง
ํฌ์คํ ์ด ์ข์๋ค๋ฉด "์ข์์โค๏ธ" ๋๋ "๊ตฌ๋ ๐๐ป" ํด์ฃผ์ธ์!