提出 #17072436
ソースコード 拡げる
Copy
Copy
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define KI_MAX (128 * 128)
-
- int ki[KI_MAX];
-
- void ki_init(void) {
- int i;
- for (i = 0; i < KI_MAX; i++) ki[i] = -1;
- }
-
- int ki_root(int n) {
- #if 0
- if (ki[n] < 0) return n;
- return ki[n] = ki_root(ki[n]);
- #else
- static int ki_root_buffer[KI_MAX];
- int cnt = 0;
- for (;;) {
- if (ki[n] < 0) {
- int i;
- for (i = 0; i < cnt; i++) ki[ki_root_buffer[i]] = n;
- return n;
- } else {
- ki_root_buffer[cnt++] = n;
- n = ki[n];
- }
- }
- #endif
- }
-
- int ki_size(int n) {
- return -ki[ki_root(n)];
- }
-
- void ki_merge(int a, int b) {
- int ar = ki_root(a);
- int br = ki_root(b);
- if (ar != br) {
- int as = ki_size(ar);
- int bs = ki_size(br);
- if (as <= bs) {
- ki[br] += ki[ar];
- ki[ar] = br;
- } else {
- ki[ar] += ki[br];
- ki[br] = ar;
- }
- }
- }
-
- /* [(random.randrange(1, 100), random.randrange(1, 100)) for _ in range(85)] */
- const int rand_starts[][2] = {
- {50, 50},
- {67, 39}, {30, 93}, {65, 81}, { 3, 14}, {21, 57},
- {72, 29}, {41, 11}, {92, 57}, {68, 35}, {57, 44},
- {21, 69}, {76, 12}, {85, 89}, { 9, 44}, {98, 90},
- { 8, 90}, { 5, 29}, { 7, 1}, {47, 58}, {35, 82},
- {66, 30}, {22, 62}, {79, 58}, {73, 97}, {96, 73},
- {98, 90}, {72, 32}, {10, 37}, {73, 81}, {86, 64},
- {44, 56}, {21, 86}, {42, 49}, {44, 39}, {46, 54},
- {85, 53}, {78, 78}, {15, 9}, { 6, 93}, {77, 95},
- { 9, 28}, {87, 30}, {13, 14}, {86, 87}, {37, 46},
- {28, 55}, {19, 47}, {27, 40}, {29, 74}, {60, 82},
- {45, 88}, { 9, 8}, {54, 70}, {47, 13}, {81, 42},
- {60, 26}, {98, 29}, {68, 84}, {70, 17}, {83, 5},
- {98, 40}, {97, 48}, {51, 96}, {69, 52}, {15, 41},
- {76, 1}, {82, 64}, {19, 93}, {58, 63}, {32, 93},
- {53, 3}, {43, 23}, { 3, 74}, {15, 85}, {41, 37},
- {24, 71}, {16, 28}, {30, 79}, {65, 80}, {11, 67},
- { 1, 5}, {51, 19}, {61, 90}, {70, 21}, {67, 2}
- };
-
- int ki_initial[KI_MAX];
-
- int id, N, K;
- char S[128][128];
-
- int ki_id(int y, int x) {
- return y * 128 + x;
- }
-
- int moveCount = 0;
- int moves[128 * 128];
-
- int done[128][128];
-
- int dlist[9][128 * 128][2];
-
- int maxMoveCount = 128 * 128;
- int maxMoves[128 * 128];
- int maxCy, maxCx;
-
- int main(void) {
- int i, j;
- int cid;
- int gen;
- int snum = (int)(sizeof(rand_starts) / sizeof(*rand_starts));
- int scount;
- if (scanf("%d%d%d", &id, &N, &K) != 3) return 1;
- if (N >= (int)(sizeof(S) / sizeof(*S))) {
- puts("0");
- return 0;
- }
- for (i = 0; i < N; i++) {
- if (scanf("%127s", S[i]) != 1) return 1;
- }
-
- ki_init();
- for (i = 0; i < N; i++) {
- for (j = 0; j < N; j++) {
- if (i + 1 < N && S[i][j] == S[i + 1][j]) ki_merge(ki_id(i, j), ki_id(i + 1, j));
- if (j + 1 < N && S[i][j] == S[i][j + 1]) ki_merge(ki_id(i, j), ki_id(i, j + 1));
- }
- }
- memcpy(ki_initial, ki, sizeof(ki));
-
- for (scount = 0; scount < snum; scount++) {
- int cy = rand_starts[scount][0], cx = rand_starts[scount][1];
- if (cy > N) cy = (N + 1) / 2;
- if (cx > N) cx = (N + 1) / 2;
- cid = ki_id(cy - 1, cx - 1);
- moveCount =0;
- memset(done, 0, sizeof(done));
- memcpy(ki, ki_initial, sizeof(ki));
-
- for (gen = 1; ki_size(cid) < N * N; gen++) {
- int score[9] = {0};
- int dnum[9] = {0};
- int smax = 0;
- for (i = 0; i < N; i++) {
- for (j = 0; j < N; j++) {
- if (ki_root(cid) == ki_root(ki_id(i, j))) {
- static const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
- int k;
- for (k = 0; k < 4; k++) {
- int di = d[k][0], dj = d[k][1];
- if (0 <= i + di && i + di < N && 0 <= j + dj && j + dj < N &&
- ki_root(cid) != ki_root(ki_id(i + di, j + dj)) && done[i + di][j + dj] < gen) {
- int sid = S[i + di][j + dj] - '1';
- score[sid] += ki_size(ki_id(i + di, j + dj));
- done[i + di][j + dj] = gen;
- dlist[sid][dnum[sid]][0] = i + di;
- dlist[sid][dnum[sid]][1] = j + dj;
- dnum[sid]++;
- }
- }
- }
- }
- }
- for (i = 1; i < K; i++) {
- if(score[i] > score[smax]) smax = i;
- }
- moves[moveCount++] = smax + 1;
- for (i = 0; i < dnum[smax]; i++) {
- ki_merge(cid, ki_id(dlist[smax][i][0], dlist[smax][i][1]));
- }
- }
- if (moveCount < maxMoveCount) {
- maxMoveCount = moveCount;
- memcpy(maxMoves, moves, sizeof(*moves) * moveCount);
- maxCy = cy;
- maxCx = cx;
- }
- }
-
- printf("%d\n", maxMoveCount);
- for (i = 0; i < maxMoveCount; i++) {
- printf("%d %d %d\n", maxCy, maxCx, maxMoves[i]);
- }
-
- return 0;
- }
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KI_MAX (128 * 128)
int ki[KI_MAX];
void ki_init(void) {
int i;
for (i = 0; i < KI_MAX; i++) ki[i] = -1;
}
int ki_root(int n) {
#if 0
if (ki[n] < 0) return n;
return ki[n] = ki_root(ki[n]);
#else
static int ki_root_buffer[KI_MAX];
int cnt = 0;
for (;;) {
if (ki[n] < 0) {
int i;
for (i = 0; i < cnt; i++) ki[ki_root_buffer[i]] = n;
return n;
} else {
ki_root_buffer[cnt++] = n;
n = ki[n];
}
}
#endif
}
int ki_size(int n) {
return -ki[ki_root(n)];
}
void ki_merge(int a, int b) {
int ar = ki_root(a);
int br = ki_root(b);
if (ar != br) {
int as = ki_size(ar);
int bs = ki_size(br);
if (as <= bs) {
ki[br] += ki[ar];
ki[ar] = br;
} else {
ki[ar] += ki[br];
ki[br] = ar;
}
}
}
/* [(random.randrange(1, 100), random.randrange(1, 100)) for _ in range(85)] */
const int rand_starts[][2] = {
{50, 50},
{67, 39}, {30, 93}, {65, 81}, { 3, 14}, {21, 57},
{72, 29}, {41, 11}, {92, 57}, {68, 35}, {57, 44},
{21, 69}, {76, 12}, {85, 89}, { 9, 44}, {98, 90},
{ 8, 90}, { 5, 29}, { 7, 1}, {47, 58}, {35, 82},
{66, 30}, {22, 62}, {79, 58}, {73, 97}, {96, 73},
{98, 90}, {72, 32}, {10, 37}, {73, 81}, {86, 64},
{44, 56}, {21, 86}, {42, 49}, {44, 39}, {46, 54},
{85, 53}, {78, 78}, {15, 9}, { 6, 93}, {77, 95},
{ 9, 28}, {87, 30}, {13, 14}, {86, 87}, {37, 46},
{28, 55}, {19, 47}, {27, 40}, {29, 74}, {60, 82},
{45, 88}, { 9, 8}, {54, 70}, {47, 13}, {81, 42},
{60, 26}, {98, 29}, {68, 84}, {70, 17}, {83, 5},
{98, 40}, {97, 48}, {51, 96}, {69, 52}, {15, 41},
{76, 1}, {82, 64}, {19, 93}, {58, 63}, {32, 93},
{53, 3}, {43, 23}, { 3, 74}, {15, 85}, {41, 37},
{24, 71}, {16, 28}, {30, 79}, {65, 80}, {11, 67},
{ 1, 5}, {51, 19}, {61, 90}, {70, 21}, {67, 2}
};
int ki_initial[KI_MAX];
int id, N, K;
char S[128][128];
int ki_id(int y, int x) {
return y * 128 + x;
}
int moveCount = 0;
int moves[128 * 128];
int done[128][128];
int dlist[9][128 * 128][2];
int maxMoveCount = 128 * 128;
int maxMoves[128 * 128];
int maxCy, maxCx;
int main(void) {
int i, j;
int cid;
int gen;
int snum = (int)(sizeof(rand_starts) / sizeof(*rand_starts));
int scount;
if (scanf("%d%d%d", &id, &N, &K) != 3) return 1;
if (N >= (int)(sizeof(S) / sizeof(*S))) {
puts("0");
return 0;
}
for (i = 0; i < N; i++) {
if (scanf("%127s", S[i]) != 1) return 1;
}
ki_init();
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (i + 1 < N && S[i][j] == S[i + 1][j]) ki_merge(ki_id(i, j), ki_id(i + 1, j));
if (j + 1 < N && S[i][j] == S[i][j + 1]) ki_merge(ki_id(i, j), ki_id(i, j + 1));
}
}
memcpy(ki_initial, ki, sizeof(ki));
for (scount = 0; scount < snum; scount++) {
int cy = rand_starts[scount][0], cx = rand_starts[scount][1];
if (cy > N) cy = (N + 1) / 2;
if (cx > N) cx = (N + 1) / 2;
cid = ki_id(cy - 1, cx - 1);
moveCount =0;
memset(done, 0, sizeof(done));
memcpy(ki, ki_initial, sizeof(ki));
for (gen = 1; ki_size(cid) < N * N; gen++) {
int score[9] = {0};
int dnum[9] = {0};
int smax = 0;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
if (ki_root(cid) == ki_root(ki_id(i, j))) {
static const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int k;
for (k = 0; k < 4; k++) {
int di = d[k][0], dj = d[k][1];
if (0 <= i + di && i + di < N && 0 <= j + dj && j + dj < N &&
ki_root(cid) != ki_root(ki_id(i + di, j + dj)) && done[i + di][j + dj] < gen) {
int sid = S[i + di][j + dj] - '1';
score[sid] += ki_size(ki_id(i + di, j + dj));
done[i + di][j + dj] = gen;
dlist[sid][dnum[sid]][0] = i + di;
dlist[sid][dnum[sid]][1] = j + dj;
dnum[sid]++;
}
}
}
}
}
for (i = 1; i < K; i++) {
if(score[i] > score[smax]) smax = i;
}
moves[moveCount++] = smax + 1;
for (i = 0; i < dnum[smax]; i++) {
ki_merge(cid, ki_id(dlist[smax][i][0], dlist[smax][i][1]));
}
}
if (moveCount < maxMoveCount) {
maxMoveCount = moveCount;
memcpy(maxMoves, moves, sizeof(*moves) * moveCount);
maxCy = cy;
maxCx = cx;
}
}
printf("%d\n", maxMoveCount);
for (i = 0; i < maxMoveCount; i++) {
printf("%d %d %d\n", maxCy, maxCx, maxMoves[i]);
}
return 0;
}
提出情報
提出日時 |
|
問題 |
A - カラフルパネル |
ユーザ |
mikecat |
言語 |
C (GCC 9.2.1) |
得点 |
49990800 |
コード長 |
4450 Byte |
結果 |
AC |
実行時間 |
2961 ms |
メモリ |
1984 KB |
ジャッジ結果
セット名 |
test_01 |
test_02 |
test_03 |
test_04 |
test_05 |
test_06 |
test_07 |
test_08 |
test_09 |
test_10 |
test_11 |
test_12 |
test_13 |
test_14 |
test_15 |
test_16 |
test_17 |
test_18 |
test_19 |
test_20 |
test_21 |
test_22 |
test_23 |
test_24 |
test_25 |
test_26 |
test_27 |
test_28 |
test_29 |
test_30 |
test_31 |
test_32 |
test_33 |
test_34 |
test_35 |
test_36 |
test_37 |
test_38 |
test_39 |
test_40 |
test_41 |
test_42 |
test_43 |
test_44 |
test_45 |
test_46 |
test_47 |
test_48 |
test_49 |
test_50 |
得点 / 配点 |
999816 / 1000000 |
999815 / 1000000 |
999817 / 1000000 |
999809 / 1000000 |
999821 / 1000000 |
999818 / 1000000 |
999818 / 1000000 |
999823 / 1000000 |
999820 / 1000000 |
999815 / 1000000 |
999819 / 1000000 |
999811 / 1000000 |
999822 / 1000000 |
999813 / 1000000 |
999822 / 1000000 |
999809 / 1000000 |
999818 / 1000000 |
999818 / 1000000 |
999815 / 1000000 |
999821 / 1000000 |
999814 / 1000000 |
999814 / 1000000 |
999808 / 1000000 |
999817 / 1000000 |
999813 / 1000000 |
999815 / 1000000 |
999819 / 1000000 |
999811 / 1000000 |
999811 / 1000000 |
999812 / 1000000 |
999812 / 1000000 |
999824 / 1000000 |
999815 / 1000000 |
999825 / 1000000 |
999811 / 1000000 |
999825 / 1000000 |
999823 / 1000000 |
999816 / 1000000 |
999803 / 1000000 |
999816 / 1000000 |
999814 / 1000000 |
999813 / 1000000 |
999812 / 1000000 |
999821 / 1000000 |
999817 / 1000000 |
999823 / 1000000 |
999804 / 1000000 |
999819 / 1000000 |
999826 / 1000000 |
999807 / 1000000 |
結果 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
セット名 |
テストケース |
test_01 |
subtask_01_01.txt |
test_02 |
subtask_01_02.txt |
test_03 |
subtask_01_03.txt |
test_04 |
subtask_01_04.txt |
test_05 |
subtask_01_05.txt |
test_06 |
subtask_01_06.txt |
test_07 |
subtask_01_07.txt |
test_08 |
subtask_01_08.txt |
test_09 |
subtask_01_09.txt |
test_10 |
subtask_01_10.txt |
test_11 |
subtask_01_11.txt |
test_12 |
subtask_01_12.txt |
test_13 |
subtask_01_13.txt |
test_14 |
subtask_01_14.txt |
test_15 |
subtask_01_15.txt |
test_16 |
subtask_01_16.txt |
test_17 |
subtask_01_17.txt |
test_18 |
subtask_01_18.txt |
test_19 |
subtask_01_19.txt |
test_20 |
subtask_01_20.txt |
test_21 |
subtask_01_21.txt |
test_22 |
subtask_01_22.txt |
test_23 |
subtask_01_23.txt |
test_24 |
subtask_01_24.txt |
test_25 |
subtask_01_25.txt |
test_26 |
subtask_01_26.txt |
test_27 |
subtask_01_27.txt |
test_28 |
subtask_01_28.txt |
test_29 |
subtask_01_29.txt |
test_30 |
subtask_01_30.txt |
test_31 |
subtask_01_31.txt |
test_32 |
subtask_01_32.txt |
test_33 |
subtask_01_33.txt |
test_34 |
subtask_01_34.txt |
test_35 |
subtask_01_35.txt |
test_36 |
subtask_01_36.txt |
test_37 |
subtask_01_37.txt |
test_38 |
subtask_01_38.txt |
test_39 |
subtask_01_39.txt |
test_40 |
subtask_01_40.txt |
test_41 |
subtask_01_41.txt |
test_42 |
subtask_01_42.txt |
test_43 |
subtask_01_43.txt |
test_44 |
subtask_01_44.txt |
test_45 |
subtask_01_45.txt |
test_46 |
subtask_01_46.txt |
test_47 |
subtask_01_47.txt |
test_48 |
subtask_01_48.txt |
test_49 |
subtask_01_49.txt |
test_50 |
subtask_01_50.txt |
ケース名 |
結果 |
実行時間 |
メモリ |
subtask_01_01.txt |
AC |
2825 ms |
1912 KB |
subtask_01_02.txt |
AC |
2847 ms |
1860 KB |
subtask_01_03.txt |
AC |
2863 ms |
1900 KB |
subtask_01_04.txt |
AC |
2835 ms |
1968 KB |
subtask_01_05.txt |
AC |
2869 ms |
1868 KB |
subtask_01_06.txt |
AC |
2869 ms |
1968 KB |
subtask_01_07.txt |
AC |
2856 ms |
1936 KB |
subtask_01_08.txt |
AC |
2896 ms |
1984 KB |
subtask_01_09.txt |
AC |
2815 ms |
1896 KB |
subtask_01_10.txt |
AC |
2847 ms |
1904 KB |
subtask_01_11.txt |
AC |
2826 ms |
1864 KB |
subtask_01_12.txt |
AC |
2880 ms |
1904 KB |
subtask_01_13.txt |
AC |
2830 ms |
1976 KB |
subtask_01_14.txt |
AC |
2845 ms |
1980 KB |
subtask_01_15.txt |
AC |
2851 ms |
1940 KB |
subtask_01_16.txt |
AC |
2906 ms |
1864 KB |
subtask_01_17.txt |
AC |
2836 ms |
1904 KB |
subtask_01_18.txt |
AC |
2906 ms |
1900 KB |
subtask_01_19.txt |
AC |
2828 ms |
1912 KB |
subtask_01_20.txt |
AC |
2818 ms |
1908 KB |
subtask_01_21.txt |
AC |
2909 ms |
1976 KB |
subtask_01_22.txt |
AC |
2901 ms |
1976 KB |
subtask_01_23.txt |
AC |
2887 ms |
1904 KB |
subtask_01_24.txt |
AC |
2900 ms |
1864 KB |
subtask_01_25.txt |
AC |
2880 ms |
1900 KB |
subtask_01_26.txt |
AC |
2733 ms |
1856 KB |
subtask_01_27.txt |
AC |
2961 ms |
1980 KB |
subtask_01_28.txt |
AC |
2847 ms |
1868 KB |
subtask_01_29.txt |
AC |
2908 ms |
1864 KB |
subtask_01_30.txt |
AC |
2854 ms |
1908 KB |
subtask_01_31.txt |
AC |
2862 ms |
1980 KB |
subtask_01_32.txt |
AC |
2781 ms |
1868 KB |
subtask_01_33.txt |
AC |
2848 ms |
1900 KB |
subtask_01_34.txt |
AC |
2818 ms |
1904 KB |
subtask_01_35.txt |
AC |
2845 ms |
1976 KB |
subtask_01_36.txt |
AC |
2815 ms |
1904 KB |
subtask_01_37.txt |
AC |
2835 ms |
1912 KB |
subtask_01_38.txt |
AC |
2901 ms |
1860 KB |
subtask_01_39.txt |
AC |
2945 ms |
1860 KB |
subtask_01_40.txt |
AC |
2884 ms |
1860 KB |
subtask_01_41.txt |
AC |
2874 ms |
1908 KB |
subtask_01_42.txt |
AC |
2890 ms |
1900 KB |
subtask_01_43.txt |
AC |
2888 ms |
1980 KB |
subtask_01_44.txt |
AC |
2813 ms |
1864 KB |
subtask_01_45.txt |
AC |
2860 ms |
1904 KB |
subtask_01_46.txt |
AC |
2870 ms |
1908 KB |
subtask_01_47.txt |
AC |
2833 ms |
1860 KB |
subtask_01_48.txt |
AC |
2829 ms |
1864 KB |
subtask_01_49.txt |
AC |
2961 ms |
1860 KB |
subtask_01_50.txt |
AC |
2942 ms |
1936 KB |