Notice
Recent Posts
Recent Comments
- Today
- Total
01-10 22:06
Tags
- Graph
- ๊ตฌํ
- ๋ฌธ๋ฒ
- tree
- BFS
- ๋ฐฑ์๋
- ์ธํด
- ์์์ ๋ ฌ
- ๊ทธ๋ฆฌ๋
- PS
- MST
- ํ๋ก๊ทธ๋๋จธ์ค
- pytorch
- Algorithm
- ์๋ฐ
- leetcode
- OOP
- java
- ๋ค์ต์คํธ๋ผ
- spring
- ์กธ์ ์ํ
- ์๋ฃ๊ตฌ์กฐ
- ๋ฐฑ์ค
- array
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ๋ฒจ๋งํฌ๋
- dp
- ์๋ฐ์์ ์
- database
- CS
Link
Partially Committed
[๋ฐฑ์ค 2798/2231/14889/2606] ์๋ฐ ๋ณธ๋ฌธ
728x90
๋ฐ์ํ
SMALL
A. ๋ธ๋์ญ BOJ 2798
https://www.acmicpc.net/problem/2798
๋์ด๋ | Bronze 2 |
ํ์ด ์๊ฐ | 10 ๋ถ |
๋ถ๋ฅ | ๋ธ๋ฃจํธํฌ์ค |
์๊ฐ๋ณต์ก๋ | |
๊ณต๊ฐ๋ณต์ก๋ | O(100+100) |
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static int ans = -1;
static int m;
static int n;
static int [] arr;
static int [] nums = new int[3];
static boolean [] visited;
private static void dfs(int k){
if(k==3){
int sum = 0;
for(int i = 0 ; i < 3; i++){
sum += nums[i];
}
if(sum > m) {
return;
} else { // sum <=m
ans = Math.max(ans, sum);
return;
}
} else {
for(int i = 0 ; i < n ; i ++){
if(!visited[i]){
visited[i] = true;
nums[k] = arr[i];
dfs(k+1);
visited[i] = false;
}
}
}
}
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
stk = new StringTokenizer(br.readLine());
n = Integer.parseInt(stk.nextToken());
m = Integer.parseInt(stk.nextToken());
arr = new int[n];
visited = new boolean[n];
stk = new StringTokenizer(br.readLine());
for(int i = 0 ; i < n ; i ++){
arr[i] = Integer.parseInt(stk.nextToken());
}
dfs(0);
bw.write(ans+"\n");
bw.flush();
bw.close();
}
}
B. ๋ถํดํฉ BOJ 2231
https://www.acmicpc.net/problem/2231
๋์ด๋ | Bronze 2 |
ํ์ด ์๊ฐ | 5 ๋ถ |
๋ถ๋ฅ | ๋ธ๋ฃจํธํฌ์ค |
์๊ฐ๋ณต์ก๋ | O(N) |
๊ณต๊ฐ๋ณต์ก๋ | O(1) |
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static int ans = -1;
static int m;
static int n;
static int [] arr;
static int [] nums = new int[3];
static boolean [] visited;
private static void dfs(int k){
if(k==3){
int sum = 0;
for(int i = 0 ; i < 3; i++){
sum += nums[i];
}
if(sum > m) {
return;
} else { // sum <=m
ans = Math.max(ans, sum);
return;
}
} else {
for(int i = 0 ; i < n ; i ++){
if(!visited[i]){
visited[i] = true;
nums[k] = arr[i];
dfs(k+1);
visited[i] = false;
}
}
}
}
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
stk = new StringTokenizer(br.readLine());
n = Integer.parseInt(stk.nextToken());
int ans = n+1;
for(int i = 1 ; i<=n-1; i++){
int target = i;
int save = target;
int pSum=target;
while(target>0){
pSum += target%10;
target = target/10;
}
if(pSum == n){
ans = Math.min(ans, save);
}
}
if(ans==n+1){
bw.write(0+"\n");
} else {
bw.write(ans+"\n");
}
bw.flush();
bw.close();
}
}
C. ์คํํธ์ ๋งํฌ BOJ 14889
https://www.acmicpc.net/problem/14889
๋์ด๋ | Silver 2 |
ํ์ด ์๊ฐ | 30 ๋ถ |
๋ถ๋ฅ | ๋ฐฑํธ๋ํน |
์๊ฐ๋ณต์ก๋ | O(nC_n/2 + n^2) |
๊ณต๊ฐ๋ณต์ก๋ | O(20*20+20*2) |
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static int n;
static long ans = Integer.MAX_VALUE;
static int[][] power;
static int[] entry;
static boolean[] visited;
private static void getScore() {
long ret = 0;
long A = 0;
long B = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i; j < n; j++) {
if (visited[i] == true && visited[j] == true) {
A+=(power[i][j]+power[j][i]);
} else if (visited[i] == false && visited[j] == false) {
B+=(power[i][j]+power[j][i]);
}
}
}
ans = Math.min(ans, Math.abs(A-B));
}
private static void dfs(int k, int idx) {
if (k == n/2) {
// ์ ์ ๊ณ์ฐ
getScore();
return;
} else {
for (int i = idx; i < n; i++) {
if (!visited[i]) {
visited[i] = true;
dfs(k + 1, i + 1);
visited[i] = false;
}
}
}
}
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
n = Integer.parseInt(br.readLine());
power = new int[n][n];
visited = new boolean[n];
for (int i = 0; i < n; i++) {
stk = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {
power[i][j] = Integer.parseInt(stk.nextToken());
}
}
dfs(0, 0);
bw.write(ans + "\n");
bw.flush();
bw.close();
}
}
D. ๋ฐ์ด๋ฌ์ค BOJ 2606
https://www.acmicpc.net/problem/2606
๋์ด๋ | Silver 3 |
ํ์ด ์๊ฐ | 5 ๋ถ |
๋ถ๋ฅ | ๊ทธ๋ํ ํ์ |
์๊ฐ๋ณต์ก๋ | O(V+E) |
๊ณต๊ฐ๋ณต์ก๋ | O(V+E) |
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static ArrayList<Integer>[] graph;
static boolean [] visited;
private static void dfs(int v){
if(!visited[v])
visited[v] = true;
for(int cur : graph[v]){
// ์ฐ๊ฒฐ ๋
ธ๋ ํ์
if(!visited[cur]){
visited[cur] = true;
dfs(cur);
}
}
}
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("src/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer stk;
int n = Integer.parseInt(br.readLine()); // ๋
ธ๋์ ์
int m = Integer.parseInt(br.readLine()); // ์ฃ์ง์ ์
graph = new ArrayList[n + 1];
for (int i = 0; i <= n; i++)
graph[i] = new ArrayList<>();
for (int i = 0; i < m; i++) {
stk = new StringTokenizer(br.readLine());
int a = Integer.parseInt(stk.nextToken());
int b = Integer.parseInt(stk.nextToken());
graph[a].add(b);
graph[b].add(a);
}
visited = new boolean[n+1];
dfs(1);
int cnt = 0;
for(int i = 2 ; i<=n; i++){
if(visited[i]){
cnt++;
}
}
bw.write(cnt+"\n");
bw.flush();
bw.close();
}
}
728x90
๋ฐ์ํ
LIST
'๐ฅ Algorithm || ๋ฌธ์ ํ์ด > PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค 1436/10815/11054/16139/16928] ์๋ฐ (0) | 2023.02.16 |
---|---|
[๋ฐฑ์ค 7568/1904/1018/25682/2110] ์๋ฐ (1) | 2023.02.15 |
[๋ฐฑ์ค 2580/14888/13305/10844] JAVA (0) | 2023.02.13 |
[๋ฐฑ์ค 1463] 1๋ก ๋ง๋ค๊ธฐ (JAVA) (0) | 2023.01.27 |
[๋ฐฑ์ค 1947] ์ ๋ฌผ ์ ๋ฌ (JAVA) (0) | 2023.01.27 |
Comments