본문 바로가기

알고리즘

(401)
8501 #define MOD 1000000007 static int N; static int table[1001]; static void table_init() { long long cur = 0; long long factorial = 1; for (int i = 1; i = 1 에 대해서 f(n) = n * f(n - 1) + [n / 2] * (n - 1)! 이와 같이 식을 세운 이유는 아래와 같다. 먼저 N = 4에 대해서 4를 놓는 위치에 대해 생각해보자. (x, y, z 는 1 ~ 3 중 중복되지 않는 하나의 값) 4 x y x x 4 y z x y 4 z x y z 4 첫번째 경우에 대해서 4는 절대 뒤집히지 않는다. case1 == f(3) 두번째 경우에서 4는 한번 뒤집힌다. case2 == f..
2383 static int N; static int board[10][10]; static int ans; static int stairs[2][3]; //계단의 개수는 두개, y, x, 계단 길이 static int persons[10]; //어느 계단을 선택 할 지, static int simulation(void) { int cpy[10][10]; int target[10][10]; int tot = 0, t = 0; queue q; //계단입구로 향하는 좌표 queue qq[2]; //계단을 내려가는 좌표 memcpy(cpy, board, sizeof(cpy)); for (int y = 0; y < N && t < N; y++) { for (int x = 0; x < N && t < N; x++) { i..
8458 static int N; static int x, y; static int solution(void) { queue q[2]; int max = 0, sum = 0, cond = 0; int ret; scanf("%d", &N); for (int i = 0; i max) max = sum; } if (q[0].size() && q[1].size()) return -1; if (q[1].size()) cond = 1; sum = 0; for (ret = 0; ; ret++) { sum += ret..
8500 using namespace std; static int N; static int AN[10000]; static int solution(void) { int ret = 0, max = 0; scanf("%d", &N); for (int i = 0; i max) max = AN[i]; ret += AN[i]; } ret += (N + max); return ret; } 코드는 간단한데, 생각하는 방법이 중요했던 것 같다. 초기에 생각했던 컨셉은 다음과 같다. 먼저 AN배열을 오름차순으로 정렬한다. 그리고 그 순서대로 배치를 해본다. 그래서 [5][4][3][2][1]로 되어 있으면, *****[5]*****[4]****[3]..
2477 static int N, M, K, A, B; static int reception_time[10]; static int repair_time[10]; static queue customer_time; static int reception_desk[10][2]; static int repair_desk[10][2]; static bool customer_table[1001]; static queue wq; //wait queue static bool is_end() { bool end = true; for (int i = 1; i
2105 static int N; static int board[20][20]; static int table[101]; static int d[4][2] = { 1, 1, 1, -1, -1, -1, -1, 1 }; static bool can_go(int y, int x) { if (y = N) return false; if (x = N) return false; if (table[board[y][x]]) return false; return true; } static int travel(int y, int x) { int ret = -1; for (int vert = 2; vert < N; vert++) { for (int hori = 2; hori < N; hori++) ..
4014 static int N, X; static int board[20][20]; static bool is_available(int * arr) { int priv = arr[0], length = 1; for (int x = 1; x 1) //높이차가 2이상인 경우, { return false; } else if (!diff) //같은 경우, { length += 1; } else if (diff == 1) //위로 올라가는 경사로를 설치할 수 있는지 확인. { if (length < X) return false; priv = arr[x]; length = 1; } else if (diff == ..
2115 static int N, M, C; static int board[10][10]; static int table[10]; static bool used[10][10]; static int alice[5]; static int bob[5]; //해당 라인에서 얻을 수 있는 최대 이윤을 구함. static void get_tot(int y, int x, int last, int v, int tot) { if (x >= last) return; for (int st = x; st < last; st++) { int nv = v + board[y][st]; int ntot = tot + (board[y][st] * board[y][st]); if (nv table[y]) table[y] = ntot; get_t..
7508 static int N; static list graph[1001]; static queue q; static int solution(void) { int x, y, ret; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d %d", &x, &y); graph[y].push_back(x); graph[x].push_back(y); } ret = N; for (int y = 1; y
8424 static int N; static list graph[1001]; static queue q; static int solution(void) { int x, y, ret; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d %d", &x, &y); graph[y].push_back(x); graph[x].push_back(y); } ret = N; for (int y = 1; y