Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[week-13] 19637, 17822 #88

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed week13/clean2001/.gitkeep
Empty file.
119 changes: 119 additions & 0 deletions week13/clean2001/B17822.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import java.io.*;
import java.util.*;

// 17822. 원판 돌리기

class Main {
static int N, M, T;
static int[][] arr;
static int zeroNum;
static int total = 0, cnt = 0;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
T = Integer.parseInt(st.nextToken());

arr = new int[N+1][M];

for(int i=1; i<=N; ++i) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<M; ++j) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}

int remainCnt = N*M;
for(int i=0; i<T; ++i) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());

// 회전 시키기
for(int r=x; r<=N; r+=x) {
arr[r] = rotate(r, d, k);
}

// 수가 남아있는지 확인하기
if(remainCnt > 0) {
zeroNum = 0;
total = 0;
cnt = 0;
boolean[][] zero = makeZero(arr);


if(zeroNum == 0) { // 평균 구해서 수를 바꾸어주기
double avg = (double)total / (double)cnt;

for(int r=1; r<=N; ++r) {
for(int c=0; c<M; ++c) {
if(arr[r][c] == 0) continue;
if((double)arr[r][c] < avg) arr[r][c]++;
else if((double)arr[r][c] > avg) arr[r][c]--;
}
}
} else { // 수를 0으로 바꾸어 지워줍니다.
for(int r=1; r<=N; ++r) {
for (int c = 0; c < M; ++c) {
if(zero[r][c]) arr[r][c] = 0;
}
}

}
}
}

int ans = 0;
for(int r=1; r<=N; ++r) {
for(int c=0; c<M; ++c) {
ans += arr[r][c];
}
}

System.out.println(ans);
}

static int[] rotate(int r, int d, int k) {
int[] newArr = new int[M];
if(d == 0) { // 시계방향
for(int i=0; i<M; ++i) {
newArr[i] = arr[r][(i+M-k) % M];
}
} else {
for(int i=0; i<M; ++i) {
newArr[i] = arr[r][(i+k) % M];
}
}

return newArr;
}

static boolean[][] makeZero(int[][] arr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 3개씩 조사하느라 코드가 깔끔하지 않아졌었는데,
2개씩 하니까 훨씬 좋네요 😊

boolean[][] zero = new boolean[N+1][M];
for(int i=1; i<=N; ++i) {
for(int j=0; j<M; ++j) {
if(arr[i][j] == 0) continue;

cnt++;
total += arr[i][j];

// 오른쪽을 확인
if(arr[i][j] == arr[i][(j+1)%M]) {
zero[i][j] = zero[i][(j+1)%M] = true;
zeroNum++;
}

// 다음 원판을 확인
if(i<N && arr[i][j] == arr[i+1][j]) {
zero[i][j] = zero[i+1][j] = true;
zeroNum++;
Comment on lines +103 to +112
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인접한 점들을 체크하는 것을 이렇게 두개의 if문으로 깔끔하게 정리하다니,,멋지네요🔥

}
}
}

return zero;
}
}
45 changes: 45 additions & 0 deletions week13/clean2001/B19637.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.io.*;
import java.util.*;
// 19637. IF문 좀 대신 써줘
class Main {
static int[] power;
static String[] name;
static int N, M;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

name = new String[N+1];
power = new int[N+1];

for(int i=0; i<N; ++i) {
st = new StringTokenizer(br.readLine());
name[i] = st.nextToken();
power[i] = Integer.parseInt(st.nextToken());
}

for(int i=0; i<M; ++i) {
int characterPower = Integer.parseInt(br.readLine());

bw.write(findName(characterPower) + "\n");
}

bw.flush();
}

private static String findName(int characterPower) {
int s = 0, e = N-1;
while(s <= e) {
int mid = (s + e) / 2;

if(power[mid] < characterPower) s = mid+1;
else e = mid-1;
}

return name[s];
}
}