Notice
Recent Posts
Recent Comments
- Today
- Total
01-11 01:45
Tags
- java
- BFS
- CS
- ํ๋ก๊ทธ๋๋จธ์ค
- database
- Algorithm
- ์๋ฐ
- ๊ตฌํ
- ์๋ฐ์์ ์
- ์ธํด
- MST
- ์์์ ๋ ฌ
- pytorch
- ๋ฐฑ์๋
- ๋ฌธ๋ฒ
- PS
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- OOP
- ๊ทธ๋ฆฌ๋
- ๋ค์ต์คํธ๋ผ
- dp
- ์กธ์ ์ํ
- Graph
- array
- ๋ฐฑ์ค
- spring
- ์๋ฃ๊ตฌ์กฐ
- leetcode
- ๋ฒจ๋งํฌ๋
- tree
Link
Partially Committed
[๋ฐฑ์ค 7568/1904/1018/25682/2110] ์๋ฐ ๋ณธ๋ฌธ
๐ฅ Algorithm || ๋ฌธ์ ํ์ด/PS
[๋ฐฑ์ค 7568/1904/1018/25682/2110] ์๋ฐ
WonderJay 2023. 2. 15. 16:53728x90
๋ฐ์ํ
SMALL
(title: [๋ฐฑ์ค 7568/1904/1018/25682/2110] ์๋ฐ)
A. ๋ฉ์น BOJ 7568
https://www.acmicpc.net/problem/7568
๋์ด๋ | Silver 5 |
ํ์ด ์๊ฐ | 9 ๋ถ |
๋ถ๋ฅ | ๋ธ๋ฃจํธํฌ์ค, ๊ตฌํ |
์๊ฐ๋ณต์ก๋ | O(N^2) |
๊ณต๊ฐ๋ณต์ก๋ | O(50+50) |
import java.io.*;
import java.util.StringTokenizer;
public class Main {
private static class info {
int w;
int h;
int idx;
public info(int w, int h) {
this.w = w;
this.h = h;
}
}
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());
info[] arr = new info[n];
int[] rank = new int[n];
for (int i = 0; i < n; i++) {
stk = new StringTokenizer(br.readLine());
int w = Integer.parseInt(stk.nextToken());
int h = Integer.parseInt(stk.nextToken());
arr[i] = new info(w,h);
}
for(int i = 0 ; i < n ; i++){
int cnt = 0;
info now = arr[i];
for(int j = 0; j<n; j++){
info cur = arr[j];
if(now.w<cur.w&&now.h<cur.h){
cnt++;
}
}
rank[i] = cnt+1;
}
for(int i = 0; i <n; i++){
bw.write(rank[i]+" ");
}
bw.flush();
bw.close();
}
}
B. 01ํ์ผ BOJ 1904
https://www.acmicpc.net/problem/1904
๋์ด๋ | Silver 3 |
ํ์ด ์๊ฐ | 20 ๋ถ |
๋ถ๋ฅ | DP |
์๊ฐ๋ณต์ก๋ | O(N) |
๊ณต๊ฐ๋ณต์ก๋ | O(1000000) |
import java.io.*;
import java.util.StringTokenizer;
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 n = Integer.parseInt(br.readLine());
final int mod = 15746;
long [] dp = new long[n+1];
if(n==1){
bw.write(1+"\n");
bw.flush();
bw.close();
return;
}
dp[1] = 1;
dp[2] = 2;
for(int i = 3; i<=n; i++){
dp[i] = (dp[i-1] + dp[i-2])%mod;
}
bw.write((dp[n])%mod+"\n");
bw.flush();
bw.close();
}
}
C. ์ฒด์คํ ๋ค์ ์น ํ๊ธฐ BOJ 1018
https://www.acmicpc.net/problem/1018
๋์ด๋ | Silver 4 |
ํ์ด ์๊ฐ | 50 ๋ถ |
๋ถ๋ฅ | ๊ตฌํ |
์๊ฐ๋ณต์ก๋ | O(N^4) |
๊ณต๊ฐ๋ณต์ก๋ | O(50*50) |
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static int n;
static int m;
static int[][] board;
static final int black = 1;
static final int white = 0;
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 row = Integer.parseInt(stk.nextToken());
int col = Integer.parseInt(stk.nextToken());
board = new int[row][col];
for (int i = 0; i < row; i++) {
char[] in = br.readLine().toCharArray();
for (int j = 0; j < col; j++) {
if (in[j] == 'W') {
board[i][j] = 0;
} else {
board[i][j] = 1;
}
}
}
int count = 0; int ans = Integer.MAX_VALUE;
for (int i = 0; i < row-7; i++) {
for (int j = 0; j < col-7; j++) { // ์์์ ํ์
// (j, i) ์ ๋ํ์ฌ
count = 0;
int leftTop = board[i][j];
for (int y = i; y < i + 8; y++) {
for (int x = j; x < j + 8; x++) {
if((x+y)%2==0){
// leftTop ๊ณผ ์์ด ๋์ผํด์ผํจ
if(leftTop != board[y][x]){
// ๊ทผ๋ฐ ๋ค๋ฅด๋ฉด?
count++;
}
} else{
// leftTop ๊ณผ ์์ด ๋ฌ๋ผ์ผ ํจ
if(leftTop==board[y][x]){
// ๊ทผ๋ฐ ๊ฐ์ผ๋ฉด?
count++;
}
}
}
}
int rev = 64-count;
ans = Math.min(Math.min(count, rev), ans);
}
}
bw.write(ans + "\n");
bw.flush();
bw.close();
}
}
D. ์ฒด์คํ ๋ค์ ์น ํ๊ธฐ2 BOJ 25682
https://www.acmicpc.net/problem/25682
๋์ด๋ | Gold 5 |
ํ์ด ์๊ฐ | 50 ๋ถ |
๋ถ๋ฅ | ๋์ ํฉ |
์๊ฐ๋ณต์ก๋ | O(NM) |
๊ณต๊ฐ๋ณต์ก๋ | O(2000*2000*2) |
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static int [][] dp;
static int[][] board;
static int row;
static int col;
static int k;
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());
row = Integer.parseInt(stk.nextToken());
col = Integer.parseInt(stk.nextToken());
k = Integer.parseInt(stk.nextToken());
board = new int[row][col];
for (int i = 0; i < row; i++) {
char[] in = br.readLine().toCharArray();
for (int j = 0; j < col; j++) {
board[i][j] = in[j];
}
}
int ans = Math.min(getCnt('W'), getCnt('B'));
bw.write(ans+"\n");
bw.flush();
bw.close();
}
private static int getCnt(int color){
dp = new int[row+1][col+1];
int ret = Integer.MAX_VALUE;
int val = 0;
for(int i = 0 ; i < row; i++){
for(int j = 0 ; j < col; j++){
if((i+j)%2==0){
val = color!=board[i][j] ? 1 : 0;
} else {
val = color==board[i][j] ? 1 : 0;
}
dp[i+1][j+1] = dp[i][j+1] + dp[i+1][j] - dp[i][j] + val;
} // calc 2-d prefix
}
for(int i = 1 ; i <= row-k+1; i++){
for(int j = 1; j <= col-k+1; j++){
ret = Math.min(ret, dp[i+k-1][j+k-1] - dp[i+k-1][j-1] - dp[i-1][j+k-1] + dp[i-1][j-1]);
}
}
return ret;
}
}
E. ๊ณต์ ๊ธฐ ์ค์น BOJ 2110
https://www.acmicpc.net/problem/2110
๋์ด๋ | Gold 4 |
ํ์ด ์๊ฐ | 20๋ถ |
๋ถ๋ฅ | parametric search, ์ด๋ถ ํ์ |
์๊ฐ๋ณต์ก๋ | O(nlogn) |
๊ณต๊ฐ๋ณต์ก๋ | O(200000) |
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
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 c = Integer.parseInt(stk.nextToken());
int[] house = new int[n];
for (int i = 0; i < n; i++) {
house[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(house);
int low = 1;
int high = house[n - 1] - house[0] + 1;
int mid = 0;
int cnt = 0;
while (low < high) {
mid = (low + high) / 2;
cnt = getCnt(mid, house); // mid ๊ฐ๊ฒฉ์ผ๋ก ์ค์น ๊ฐ๋ฅํ ๊ณต์ ๊ธฐ์ ๊ฐ์๋ฅผ ๊ณ์ฐ
if (cnt < c) {
// mid ๊ฐ๊ฒฉ์ผ๋ก ์ค์นํ๋ฉด, ๊ฐ์ง๊ณ ์๋ ๊ณต์ ๊ธฐ๋ณด๋ค ๋ ์ ๊ฒ ์ค์นํ ์ ๋ฐ์ ์๋ค๋ฉด
// ๊ฐ๊ฒฉ์ ์ค์ฌ์ผ ํ๋ค.
high = mid;
} else {
// mid ๊ฐ๊ฒฉ์ผ๋ก ์ค์นํ๋ฉด, ๊ฐ์ง๊ณ ์๋ ๊ณต์ ๊ธฐ๋ฅผ ๋ชจ๋ ์ฌ์ฉํ ์ ์๊ฑฐ๋
// ๋ ํ์ํ ์ํฉ์ด๋ผ๋ฉด ๊ฐ๊ฒฉ์ ๋๋ ค์ผ ํ๋ค.
low = mid + 1;
}
}
bw.write((low - 1) + "\n");
bw.flush();
bw.close();
}
private static int getCnt(int diff, int[] house) {
int ret = 0;
int cnt = 1;
int t = house[0];
for (int i = 1; i < house.length; i++) {
if (diff <= house[i] - t) {
cnt++;
t = house[i];
}
}
return ret = cnt;
}
}
728x90
๋ฐ์ํ
LIST
'๐ฅ Algorithm || ๋ฌธ์ ํ์ด > PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค 18870/2565/2470/4195] ์๋ฐ (0) | 2023.02.17 |
---|---|
[๋ฐฑ์ค 1436/10815/11054/16139/16928] ์๋ฐ (0) | 2023.02.16 |
[๋ฐฑ์ค 2798/2231/14889/2606] ์๋ฐ (0) | 2023.02.14 |
[๋ฐฑ์ค 2580/14888/13305/10844] JAVA (0) | 2023.02.13 |
[๋ฐฑ์ค 1463] 1๋ก ๋ง๋ค๊ธฐ (JAVA) (0) | 2023.01.27 |
Comments