본문 바로가기

분류 전체보기

(154)
[Hello 2023] B. MKnez's ConstructiveForces Task ※. n이 짝수일 때 항상 답이 존재한다. (-1 1 -1 1 ...) ※. n이 5이상의 홀수일 때 항상 답이 존재한다. 두번째 조건을 만족하려면 S(i-1) + S(i) = S(i) + S(i+1)은 같아야 한다. S1 = a, S2 = b로 가정하면 배열은 [a, b, a, b ....] 가 된다. k가 n/2라 할 때 배열의 합은 (k+1)a + kb 이다. (k+1)a + kb = a+b -> ka + (k-1)b = 0 -> a = k-1, b = -k ( (k(2) - k) (-k(2) + k) ) [k-1, -k, k-1, -k ....] (k가 1일때 1*a + (1-1)b = 0 -> a= 0이 된다.) (k가 2일떄 2*a + (2-1)b = 0 -> 2a + b = 0 -> a = ..
[Codeforces Round #835 (Div. 4)] F. Quests https://codeforces.com/blog/entry/109348 Codeforces Round #835 (Div. 4) Editorial - Codeforces codeforces.com #include using namespace std; const int MAX = 200007; const int MOD = 1000000007; void solve() { int n, d; long long c; cin >> n >> c >> d; long long a[n]; for (int i = 0; i > a[i]; } sort(a, a + n, greater()); int l = 0, r = d + 2; while (l < r) { int m = l + (r - l + 1..
[백준] 26071 오락실에 간 총총이 [자바] https://upload.acmicpc.net/241319de-a09a-41ad-aad9-360e3cbbc391/ ※. G가 하나일 때 -> 0 ※. 떨어져 있는 G가 모일려면 화면 끝(모서리) 까지 가야함 ※. G가 한 줄에 있을 때 -> 한쪽으로만 움직이면 됨 -> max(x) -1 (위쪽으로 모임) 과 N-min(x) (아래쪽으로 모임) 중 작은것 ※. 나머지의 경우 G가 모이는 경우는 네 구석밖에 없음 -> 위에서 max(y)-1과 N-min(y)도 구해준다. import java.io.*; import java.util.*; class Main { public static void main(String[] args) throws Exception { BufferedReader br = new ..
[Codeforces Round #834 (Div. 3)] B. Lost Permutation ※. 순열에 비어있는 수가 있다면 확실하게 잃어버린 요소 [1,3,4] -> 2 ※.비어있는 수를 다 더한다음 더한 값이 s보다 작으면 뒤의 요소들을 더한다. (요소들의 input값이 50까지, 요소값은 51이후부터의 합이 1000이 되기 전까지 올 수 있다.) import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args..
[프로그래머스 Level 4] 올바른 괄호의 갯수 [자바] ※. 열린 괄호, 닫힌 괄호 를 구별하고 괄호쌍의 조건에 맞게 재귀 (n * 2 -> 괄호쌍의 갯수 * (열린괄호 + 닫힌괄호) = 2 class Solution { public int solution(int n) { int answer = bracket(0,0,n*2); return answer; } static int bracket(int open, int close, int n) { if(open + close == n) { return 1; } int answer = 0; if(open close) { answer += bracket(open, close+1, n); } return answer..
[프로그래머스 Level 2] 게임 맵 최단거리 [자바] ※. BFS로 풀어야함 ※. 0 이 벽이 있는 자리 ※. (1,1) 시작위치도 지나야 하는 칸에 포함된다. import java.util.*; class Position { int x,y; Position(int x, int y) { this.x = x; this.y = y; } boolean isValid(int w, int h) { if(x = h) return false; if(y = w) return false; return true; } } class Solution { public int solution(int[][] maps) { int answer = -1; int height = maps.length; int width = maps[0].length; ..
[프로그래머스 Level 2] 가장 큰 수 [자바] ※. 비교하기 쉽게 숫자 배열 -> 문자 배열 ※. 배열 원소끼리 더한(s1+s2, s2+s1)것을 비교해준다. (Arrays.sort 사용) ※. 정렬한 원소들을 합쳤을 때 '0'으로 시작한다면 "0"을 반환해준다. ( 000000 이 올 수 있음) import java.util.*; class Solution { public String solution(int[] numbers) { String[] copy = new String[numbers.length]; for(int i = 0; i < numbers.length; i++) { copy[i] = String.valueOf(numbers[i]); } Arrays.sort(copy, new Comparator(){ @Override public i..
[프로그래머스 Level 3] 기지국 설치 [Java] ※. 현재위치(position)을 만들고 현재위치가 기지국 왼쪽 범위(i-w)보다 작으면 설치 ※. 최소로 설치하기 위해서 현재위치에 전파가 없다면 (현재위치 + w) 에 설치한다. 현재위치에도 전파가 도달 ※. 현재위치가 설치된 기지국 범위에 포함된다면 현재위치를 기지국 오른쪽 범위 밖으로 변경(i+w+1) ※. 기지국을 새로 설치했다면 다음 위치는 기지국의 오른쪽 범위 밖(position + w + 1 + w) class Solution { public int solution(int n, int[] stations, int w) { int position = 1; int si = 0; int answer = 0; while(position