Submission #75537933
Source Code Expand
Copy
#include <stdio.h>#include <stdlib.h>/*!!!!!x, y 逆転注意!!!!!*/int H, W, N;int h[212345], w[212345];int cmp_h(const void* x, const void* y) {int a = *(const int*)x, b = *(const int*)y;return (h[a] > h[b]) - (h[a] < h[b]);}int cmp_w(const void* x, const void* y) {int a = *(const int*)x, b = *(const int*)y;return (w[a] > w[b]) - (w[a] < w[b]);}int hidx[212345], widx[212345];int ans_y[212345], ans_x[212345];void skip_h(int* left_h) {while (*left_h > 0 && ans_y[hidx[*left_h - 1]] > 0) (*left_h)--;}void skip_w(int* left_w) {while (*left_w > 0 && ans_y[widx[*left_w - 1]] > 0) (*left_w)--;}void solve(int height, int width, int offset_y, int offset_x, int left_h, int left_w) {if (skip_h(&left_h), left_h > 0 && h[hidx[left_h - 1]] == height) {int idx = hidx[--left_h];ans_y[idx] = offset_y;ans_x[idx] = offset_x;solve(height, width - w[idx], offset_y, offset_x + w[idx], left_h, left_w);} else if (skip_w(&left_w), left_w > 0 && w[widx[left_w - 1]] == width) {int idx = widx[--left_w];ans_y[idx] = offset_y;ans_x[idx] = offset_x;
#include <stdio.h>
#include <stdlib.h>
/*
!!!!!x, y 逆転注意!!!!!
*/
int H, W, N;
int h[212345], w[212345];
int cmp_h(const void* x, const void* y) {
int a = *(const int*)x, b = *(const int*)y;
return (h[a] > h[b]) - (h[a] < h[b]);
}
int cmp_w(const void* x, const void* y) {
int a = *(const int*)x, b = *(const int*)y;
return (w[a] > w[b]) - (w[a] < w[b]);
}
int hidx[212345], widx[212345];
int ans_y[212345], ans_x[212345];
void skip_h(int* left_h) {
while (*left_h > 0 && ans_y[hidx[*left_h - 1]] > 0) (*left_h)--;
}
void skip_w(int* left_w) {
while (*left_w > 0 && ans_y[widx[*left_w - 1]] > 0) (*left_w)--;
}
void solve(int height, int width, int offset_y, int offset_x, int left_h, int left_w) {
if (skip_h(&left_h), left_h > 0 && h[hidx[left_h - 1]] == height) {
int idx = hidx[--left_h];
ans_y[idx] = offset_y;
ans_x[idx] = offset_x;
solve(height, width - w[idx], offset_y, offset_x + w[idx], left_h, left_w);
} else if (skip_w(&left_w), left_w > 0 && w[widx[left_w - 1]] == width) {
int idx = widx[--left_w];
ans_y[idx] = offset_y;
ans_x[idx] = offset_x;
solve(height - h[idx], width, offset_y + h[idx], offset_x, left_h, left_w);
}
}
int main(void) {
int i;
if (scanf("%d%d%d", &H, &W, &N) != 3) return 1;
for (i = 0; i < N; i++) {
if (scanf("%d%d", &h[i], &w[i]) != 2) return 1;
}
for (i = 0; i < N; i++) {
hidx[i] = i;
widx[i] = i;
}
qsort(hidx, N, sizeof(*hidx), cmp_h);
qsort(widx, N, sizeof(*widx), cmp_w);
solve(H, W, 1, 1, N, N);
for (i = 0; i < N; i++) {
printf("%d %d\n", ans_y[i], ans_x[i]);
}
return 0;
}
/*
必ず「縦幅が全体の縦幅と同じブロック」または「横幅が全体の横幅と同じブロック」があるはず
前者があったら、それを左から配置していき、残りの部分で再帰
後者でも上から配置して同様
このとき、ここで扱った幅はここで全部消費するので、残った部分に配置するのはそれ未満のみのはず
*/
Submission Info
| Submission Time | |
|---|---|
| Task | D - Reconstruct Chocolate |
| User | mikecat |
| Language | C23 (GCC 14.2.0) |
| Score | 425 |
| Code Size | 2087 Byte |
| Status | AC |
| Exec Time | 98 ms |
| Memory | 9628 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 425 / 425 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_01.txt, 00_sample_02.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 02_biased_cut_01.txt, 02_biased_cut_02.txt, 02_biased_cut_03.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_01.txt | AC | 1 ms | 1756 KiB |
| 00_sample_02.txt | AC | 0 ms | 1796 KiB |
| 01_random_01.txt | AC | 98 ms | 9624 KiB |
| 01_random_02.txt | AC | 98 ms | 9628 KiB |
| 01_random_03.txt | AC | 89 ms | 9372 KiB |
| 01_random_04.txt | AC | 94 ms | 9376 KiB |
| 01_random_05.txt | AC | 86 ms | 9116 KiB |
| 01_random_06.txt | AC | 86 ms | 9116 KiB |
| 01_random_07.txt | AC | 96 ms | 9628 KiB |
| 01_random_08.txt | AC | 92 ms | 9628 KiB |
| 01_random_09.txt | AC | 90 ms | 9368 KiB |
| 01_random_10.txt | AC | 90 ms | 9368 KiB |
| 01_random_11.txt | AC | 86 ms | 9112 KiB |
| 01_random_12.txt | AC | 86 ms | 9108 KiB |
| 01_random_13.txt | AC | 91 ms | 9368 KiB |
| 01_random_14.txt | AC | 87 ms | 9376 KiB |
| 02_biased_cut_01.txt | AC | 69 ms | 7828 KiB |
| 02_biased_cut_02.txt | AC | 68 ms | 8228 KiB |
| 02_biased_cut_03.txt | AC | 97 ms | 9628 KiB |