https://www.acmicpc.net/problem/18429
18429๋ฒ: ๊ทผ์์ค
์จ์ดํธ ํธ๋ ์ด๋์ ์ข์ํ๋ ์ด๋ค ๋ํ์์์, ํ์ฌ 3๋ ์ด๋ ์ค๋ 500์ ๊ดด๋ ฅ์ ์์ ํ๊ณ ์๋ค. ๋ค๋ง, ํ๋ฃจ๊ฐ ์ง๋ ๋๋ง๋ค ์ค๋์ด K๋งํผ ๊ฐ์ํ๋ค. ์๋ฅผ ๋ค์ด K=4์ผ ๋, 3์ผ์ด ์ง๋๋ฉด ์ค๋์ด 488๋ก
www.acmicpc.net
๋งค์ผ ์๋ก ๋ค๋ฅธ ์ด๋ค ํคํธ๋ฅผ ์ฌ์ฉํ ์ง ์ ํด์ผ ํ๋ฏ๋ก ์์ด์ด๋ค.
์์ด์ ๋ง๋ค์ด์ ๊ฒฐ๊ณผ ๋ฐฐ์ด์ ์ ์ฅํ๊ณ , ์์ฑ์ด ๋์์ ๋ ์ด๋ ๊ธฐ๊ฐ๋์ ํญ์ ์ค๋์ด 500์ด์์ผ๋ก ์ ์ง๊ฐ ๋๋์ง ํ์ธํ๋ค. ์ ์ง๊ฐ ๋๋ฉด ์นด์ดํธ๋ฅผ ์ฌ๋ ธ๋ค.
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N, K;
static int[] arr;
static boolean[] visited;
static int[] result;
static int count;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
arr = new int[N];
st = new StringTokenizer(br.readLine(), " ");
for(int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
//๊ตฌํ
visited = new boolean[N];
result = new int[N];
count = 0;
permutation(0); //์์ด
//์ถ๋ ฅ
System.out.println(count);
}
public static void permutation(int depth) {
if(depth == N) {
//์ด๋ ๊ธฐ๊ฐ๋์ ํญ์ ์ค๋์ด 500 ์ด์์ด ๋๋๋ก ํ๋ ๊ฒฝ์ฐ์ ์
int muscle = 500;
for(int i = 0; i < N; i++) {
muscle = muscle + result[i] - K;
if(muscle < 500) return; //500๋ณด๋ค ์์์ง๋ฉด X
}
count++;
return;
}
for(int i = 0; i < N; i++) {
if(visited[i]) continue;
visited[i] = true;
result[depth] = arr[i];
permutation(depth + 1);
visited[i] = false;
}
}
}
๊ฒฐ๊ณผ
'Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 9205๋ฒ - ๋งฅ์ฃผ ๋ง์๋ฉด์ ๊ฑธ์ด๊ฐ๊ธฐ (0) | 2022.10.02 |
---|---|
๋ฐฑ์ค 1189๋ฒ - ์ปด๋ฐฑํ (0) | 2022.10.01 |
๋ฐฑ์ค 2596๋ฒ - ๋น๋ฐํธ์ง (0) | 2022.08.28 |
2960๋ฒ - ์๋ผํ ์คํ ๋ค์ค์ ์ฒด (0) | 2022.08.21 |
๋ฐฑ์ค 17608๋ฒ - ๋ง๋๊ธฐ (0) | 2022.08.21 |