Notice
Recent Posts
Recent Comments
- Today
- Total
01-10 22:06
Tags
- ์๋ฐ
- Algorithm
- CS
- ํ๋ก๊ทธ๋๋จธ์ค
- ๋ฐฑ์ค
- ๊ทธ๋ฆฌ๋
- tree
- ๋ฐฑ์๋
- ์๋ฐ์์ ์
- java
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- PS
- OOP
- spring
- ๊ตฌํ
- ์ธํด
- leetcode
- Graph
- dp
- ์์์ ๋ ฌ
- database
- ๋ฌธ๋ฒ
- BFS
- ์กธ์ ์ํ
- ์๋ฃ๊ตฌ์กฐ
- pytorch
- MST
- ๋ฒจ๋งํฌ๋
- ๋ค์ต์คํธ๋ผ
- array
Link
Partially Committed
[๋ฐฑ์ค 5568/2559/1504/11066] ์๋ฐ ๋ณธ๋ฌธ
728x90
๋ฐ์ํ
SMALL
(title: [๋ฐฑ์ค 5568/2559/1504/11066] ์๋ฐ)
A. ์นด๋๋๊ธฐ BOJ 5568
https://www.acmicpc.net/problem/5568
๋์ด๋ | Silver 4 |
ํ์ด ์๊ฐ | 10 ๋ถ |
๋ถ๋ฅ | ๋ฐฑํธ๋ํน |
์๊ฐ๋ณต์ก๋ | O(n^k * k * log n) |
๊ณต๊ฐ๋ณต์ก๋ |
import java.io.*;
import java.util.*;
public class Main {
static int n;
static int k;
static TreeSet<String> treeSet = new TreeSet<>();
static boolean [] visited;
static int [] cards;
static int [] arr;
static void dfs(int depth){
if(depth==k){
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < k ; i++){
sb.append(String.valueOf(cards[arr[i]]));
}
treeSet.add(sb.toString());
return;
} else{
for(int i = 0 ; i < n ; i++){
if(!visited[i]){
visited[i] = true;
arr[depth] = i;
dfs(depth+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());
// n ์ฅ์ ์ ์ค์์
k = Integer.parseInt(br.readLine());
// k ์ฅ์ ์ ํํ์ฌ ์๋ฅผ ๋ง๋ ๋ค.
visited = new boolean[n];
cards = new int[n];
arr = new int[n];
for(int i = 0 ; i < n ; i++){
cards[i] = Integer.parseInt(br.readLine());
}
dfs(0);
bw.write(treeSet.size()+"\n");
bw.flush();
bw.close();
}
}
B. ์์ด BOJ 2559
https://www.acmicpc.net/problem/2559
๋์ด๋ | Silver 3 |
ํ์ด ์๊ฐ | 15 ๋ถ |
๋ถ๋ฅ | ๋์ ํฉ + ์ฌ๋ผ์ด๋ฉ ์๋์ฐ |
์๊ฐ๋ณต์ก๋ | O(N) |
๊ณต๊ฐ๋ณต์ก๋ |
import java.io.*;
import java.util.*;
public class Main {
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());
int n = Integer.parseInt(stk.nextToken());
int k = Integer.parseInt(stk.nextToken());
int [] base = new int[n+1];
int [] prefix = new int[n+1];
stk = new StringTokenizer(br.readLine());
for(int i = 1 ; i <= n; i++){
base[i] = Integer.parseInt(stk.nextToken());
}
prefix[0] = base[1];
for(int i = 1 ; i<=n; i++){
prefix[i] = prefix[i-1] + base[i];
}
int st = 1;
int end = k;
int max = -Integer.MAX_VALUE;
while(end <= n){
int sum = prefix[end] - prefix[st-1];
max = Math.max(sum, max);
st++;
end++;
}
bw.write(max+"\n");
bw.flush();
bw.close();
}
}
C. ํน์ ํ ์ต๋จ ๊ฒฝ๋ก BOJ 1504
https://www.acmicpc.net/problem/1504
๋์ด๋ | Gold 4 |
ํ์ด ์๊ฐ | 1์๊ฐ |
๋ถ๋ฅ | ๋ค์ต์คํธ๋ผ |
์๊ฐ๋ณต์ก๋ | O(EogV) |
๊ณต๊ฐ๋ณต์ก๋ |
์ ๋ ฅ์ ์๋ชป ๋ฐ์๋๊ณ , 40๋ถ๋์ ๋ง์ํํ๋ค ๐ข
import java.io.*;
import java.util.*;
public class Main {
static ArrayList<node>[] graph;
static int n;
static int e;
static int inf = 200000*1000+1;
static class node implements Comparable<node> {
int to;
int w;
public node(int to, int w) {
this.to = to;
this.w = w;
}
@Override
public int compareTo(node o) {
if (this.w > o.w) return -1;
else return 1;
}
}
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());
e = Integer.parseInt(stk.nextToken());
graph = new ArrayList[n + 1];
for (int i = 0; i <= n; i++) {
graph[i] = new ArrayList<>();
}
for (int i = 0; i < e; i++) {
stk = new StringTokenizer(br.readLine());
int a = Integer.parseInt(stk.nextToken());
int b = Integer.parseInt(stk.nextToken());
int c = Integer.parseInt(stk.nextToken());
graph[a].add(new node(b, c));
graph[b].add(new node(a, c));
}
stk = new StringTokenizer(br.readLine());
int v1 = Integer.parseInt(stk.nextToken());
int v2 = Integer.parseInt(stk.nextToken());
int [] dist1 = new int[n+1];
int [] dist2 = new int[n+1];
int [] dist3 = new int[n+1];
dijkstra(1, n, dist1);
dijkstra(v1, v2, dist2);
dijkstra(v2, n, dist3);
long case1 = dist1[v1] + dist2[v2] + dist3[n];
// case 2 : 1->v2->v1->n
long case2 = dist1[v2] + dist3[v1] + dist2[n];
long ans = Math.min(inf, case1);
if(dist2[v2]==inf || ans==inf || dist3[n] == inf){
//v1-n, v2-n ์ผ๋ก ์ฐ๊ฒฐ๋ ๊ฒฝ๋ก๊ฐ ์๋ค๋ฉด
System.out.println("-1\n");
return;
}
ans = Math.min(ans, case2);
bw.write(ans+"\n");
bw.flush();
bw.close();
}
static void dijkstra(int start, int end, int [] dist){
Arrays.fill(dist, inf);
dist[start] = 0;
PriorityQueue<node> pq = new PriorityQueue<>();
pq.add(new node(start, 0));
while(!pq.isEmpty()){
node now = pq.poll();
for(node cur : graph[now.to]){
if(dist[cur.to] >= dist[now.to] + cur.w){
dist[cur.to] = dist[now.to] + cur.w;
pq.add(new node(cur.to, dist[cur.to]));
}
}
}
}
}
D. ํ์ผ ํฉ์น๊ธฐ BOJ 11066
https://www.acmicpc.net/problem/11066
๋์ด๋ | Gold3 |
ํ์ด ์๊ฐ | 1์๊ฐ ์ ๋ ๊ณ ๋ฏผํ๋ค๊ฐ ์ ํ์์ด ๋๋ฌด ์๋ ์ฌ๋ผ์, ๊ตฌ๊ธ๋ง์ ๋์์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ค ๐ข |
๋ถ๋ฅ | DP |
์๊ฐ๋ณต์ก๋ | O(N^3) |
๊ณต๊ฐ๋ณต์ก๋ |
[Bottom-up]
import java.io.*;
import java.util.*;
public class Main {
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 tc = Integer.parseInt(br.readLine());
while (tc > 0) {
tc--;
int k = Integer.parseInt(br.readLine());
int[] arr = new int[k + 1];
stk = new StringTokenizer(br.readLine());
for (int i = 1; i <= k; i++) {
arr[i] = Integer.parseInt(stk.nextToken());
}
long ans = solve(arr, k);
bw.write(ans + "\n");
}
bw.flush();
bw.close();
}
private static int solve(int[] arr, int k) {
int[][] dp = new int[k + 1][k + 1];
int[] prefix = new int[k + 1];
for (int i = 1; i <= k; i++) {
prefix[i] = prefix[i - 1] + arr[i];
} // ๋์ ํฉ
// ํ์ผ ๊ฐ์๊ฐ ์ธ ๊ฐ ์ด์์ด๋ผ๋ฉด ( mid ; ์ค๊ฐ ์ง์ )
// dp[i][j] = min( dp[i][j] , dp[i][mid] + dp[mid+1][j] + sum(i~j) )
for (int div = 1; div < k; div++) {
for (int i = 1; i+div <= k; i++) {
int j = i + div;
dp[i][j] = Integer.MAX_VALUE;
for (int mid = i; mid < j; mid++) {
dp[i][j] = Math.min(dp[i][j], dp[i][mid] + dp[mid + 1][j] + getSum(i, j, prefix));
}
}
}
return dp[1][k];
}
private static int getSum(int st, int end, int[] prefix) {
return prefix[end] - prefix[st - 1];
}
}
[Top-down]
import java.io.*;
import java.util.*;
public class Main {
static int[][] dp;
static int[] arr;
static int[] prefix;
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 tc = Integer.parseInt(br.readLine());
while (tc > 0) {
tc--;
int k = Integer.parseInt(br.readLine());
arr = new int[k + 1];
stk = new StringTokenizer(br.readLine());
for (int i = 1; i <= k; i++) {
arr[i] = Integer.parseInt(stk.nextToken());
}
dp = new int[k + 1][k + 1];
prefix = new int[k + 1];
for (int i = 1; i <= k; i++) {
prefix[i] = prefix[i - 1] + arr[i];
} // ๋์ ํฉ
for(int i = 0 ; i <=k; i++){
for(int j = 0 ; j <=k; j++){
dp[i][j] = Integer.MAX_VALUE;
}
}
long ans = solve_topdown(1, k);
bw.write(ans + "\n");
}
bw.flush();
bw.close();
}
private static int solve_topdown(int left, int right) {
if (dp[left][right] != Integer.MAX_VALUE) {
return dp[left][right];
}
if (left == right) {
return dp[left][right] = 0;
}
if (left + 1 == right) {
return dp[left][right] = arr[left] + arr[right];
}
for (int mid = left; mid < right; ++mid) {
int nleft = solve_topdown(left, mid);
int nright = solve_topdown(mid + 1, right);
dp[left][right] = Math.min(dp[left][right], nleft + nright);
}
return dp[left][right] += getSum(left, right, prefix);
}
private static int getSum(int st, int end, int[] prefix) {
return prefix[end] - prefix[st - 1];
}
}
728x90
๋ฐ์ํ
LIST
'๐ฅ Algorithm || ๋ฌธ์ ํ์ด > PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์นด๋๋ญ์น (JAVA) (1) | 2023.02.20 |
---|---|
[๋ฐฑ์ค 9251/1520/9370] ์๋ฐ (0) | 2023.02.20 |
[๋ฐฑ์ค 18870/2565/2470/4195] ์๋ฐ (0) | 2023.02.17 |
[๋ฐฑ์ค 1436/10815/11054/16139/16928] ์๋ฐ (0) | 2023.02.16 |
[๋ฐฑ์ค 7568/1904/1018/25682/2110] ์๋ฐ (1) | 2023.02.15 |
Comments