提出 #73317509
ソースコード 拡げる
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int h, w, id;
} Piece;
typedef struct {
long long x, y;
} Result;
// Comparator for sorting by height (descending) then width
int compareH(const void* a, const void* b) {
Piece* p1 = (Piece*)a;
Piece* p2 = (Piece*)b;
if (p1->h != p2->h) return p2->h - p1->h;
return p2->w - p1->w;
}
// Comparator for sorting by width (descending) then height
int compareW(const void* a, const void* b) {
Piece* p1 = (Piece*)a;
Piece* p2 = (Piece*)b;
if (p1->w != p2->w) return p2->w - p1->w;
return p2->h - p1->h;
}
int main() {
long long H, W;
int N;
if (scanf("%lld %lld %d", &H, &W, &N) != 3) return 0;
Piece *byH = malloc(sizeof(Piece) * N);
Piece *byW = malloc(sizeof(Piece) * N);
int *used = calloc(N, sizeof(int));
Result *results = malloc(sizeof(Result) * N);
for (int i = 0; i < N; i++) {
int h, w;
scanf("%d %d", &h, &w);
byH[i] = (Piece){h, w, i};
byW[i] = (Piece){h, w, i};
}
qsort(byH, N, sizeof(Piece), compareH);
qsort(byW, N, sizeof(Piece), compareW);
long long currH = H, currW = W;
long long currX = 1, currY = 1;
int ptrH = 0, ptrW = 0;
for (int i = 0; i < N; i++) {
// Look for a piece that matches current bounding box height
while (ptrH < N && (used[byH[ptrH].id] || byH[ptrH].h > currH)) ptrH++;
if (ptrH < N && byH[ptrH].h == currH) {
int idx = byH[ptrH].id;
results[idx].x = currX;
results[idx].y = currY;
currY += byH[ptrH].w;
currW -= byH[ptrH].w;
used[idx] = 1;
} else {
// Otherwise, find a piece that matches current bounding box width
while (ptrW < N && (used[byW[ptrW].id] || byW[ptrW].w > currW)) ptrW++;
int idx = byW[ptrW].id;
results[idx].x = currX;
results[idx].y = currY;
currX += byW[ptrW].h;
currH -= byW[ptrW].h;
used[idx] = 1;
}
}
for (int i = 0; i < N; i++) {
printf("%lld %lld\n", results[i].x, results[i].y);
}
free(byH);
free(byW);
free(used);
free(results);
return 0;
}
提出情報
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
425 / 425 |
| 結果 |
|
|
| セット名 |
テストケース |
| 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 |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 00_sample_01.txt |
AC |
0 ms |
1956 KiB |
| 00_sample_02.txt |
AC |
0 ms |
2024 KiB |
| 01_random_01.txt |
AC |
99 ms |
15080 KiB |
| 01_random_02.txt |
AC |
97 ms |
15228 KiB |
| 01_random_03.txt |
AC |
97 ms |
14716 KiB |
| 01_random_04.txt |
AC |
97 ms |
14856 KiB |
| 01_random_05.txt |
AC |
97 ms |
14556 KiB |
| 01_random_06.txt |
AC |
97 ms |
14640 KiB |
| 01_random_07.txt |
AC |
97 ms |
15112 KiB |
| 01_random_08.txt |
AC |
96 ms |
15040 KiB |
| 01_random_09.txt |
AC |
97 ms |
14744 KiB |
| 01_random_10.txt |
AC |
96 ms |
14708 KiB |
| 01_random_11.txt |
AC |
97 ms |
14716 KiB |
| 01_random_12.txt |
AC |
96 ms |
14744 KiB |
| 01_random_13.txt |
AC |
97 ms |
14688 KiB |
| 01_random_14.txt |
AC |
97 ms |
14832 KiB |
| 02_biased_cut_01.txt |
AC |
89 ms |
13812 KiB |
| 02_biased_cut_02.txt |
AC |
89 ms |
13680 KiB |
| 02_biased_cut_03.txt |
AC |
96 ms |
15136 KiB |