Submission #71354817
Source Code Expand
Copy
#include <stdio.h>#include <stdlib.h>#include <inttypes.h>#define INF INT64_C(5050505050505050505)int N, K;int X[16], Y[16];int64_t score_memo[1 << 16];int64_t get_score(int points) {if (score_memo[points]) {return ~score_memo[points];} else {int64_t ans = 0;int i, j;for (i = 0; i < N; i++) {if ((points >> i) & 1) {for (j = i + 1; j < N; j++) {if ((points >> j) & 1) {
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#define INF INT64_C(5050505050505050505)
int N, K;
int X[16], Y[16];
int64_t score_memo[1 << 16];
int64_t get_score(int points) {
if (score_memo[points]) {
return ~score_memo[points];
} else {
int64_t ans = 0;
int i, j;
for (i = 0; i < N; i++) {
if ((points >> i) & 1) {
for (j = i + 1; j < N; j++) {
if ((points >> j) & 1) {
int64_t candidate = (int64_t)(X[i] - X[j]) * (X[i] - X[j]) + (int64_t)(Y[i] - Y[j]) * (Y[i] - Y[j]);
if (candidate > ans) ans = candidate;
}
}
}
}
return ~(score_memo[points] = ~ans);
}
}
#define MOD_BY 8475787
#define MULT 5644889
#define MULT2 4207433
struct memo_s {
int cur, left;
int64_t ans;
struct memo_s* next;
};
struct memo_s* memos[MOD_BY];
int64_t* get_memo(int group_left, int cur, int left) {
int hash = (int)(((long long) group_left * MULT + (long long)cur * MULT2 + left) % MOD_BY);
struct memo_s** pmemo = &memos[hash];
while (*pmemo != NULL) {
if ((*pmemo)->cur == cur && (*pmemo)->left == left) {
return &(*pmemo)->ans;
}
pmemo = &(*pmemo)->next;
}
*pmemo = malloc(sizeof(**pmemo));
if (*pmemo == NULL) exit(2);
**pmemo = (struct memo_s){ cur, left, -1, NULL };
return &(*pmemo)->ans;
}
int64_t tiisakunaihou(int64_t a, int64_t b) {
return a >= b ? a : b;
}
int64_t calc(int group_left, int cur, int left) {
int64_t *pmemo = get_memo(group_left, cur, left);
if (*pmemo >= 0) return *pmemo;
if (cur == 0) {
if (left == 0) {
/* 全部グループに分け終わった or グループ枠が余った */
return *pmemo = group_left == 0 ? 0 : INF;
} else {
if (group_left <= 0) {
/* グループ枠が足りない */
return *pmemo = INF;
} else {
/* 新規グループの作成開始 */
int i;
for (i = 0; ((left >> i) & 1) == 0; i++);
return *pmemo = calc(group_left - 1, 1 << i, left & ~(1 << i));
}
}
} else {
/* 現在のグループの内容で精算する */
int64_t ans = tiisakunaihou(calc(group_left, 0, left), get_score(cur));
/* 現在のグループに点を追加する */
int i;
for (i = 0; i < N; i++) {
if ((left >> i) & 1) {
int64_t candidate = calc(group_left, cur | (1 << i), left & ~(1 << i));
if (candidate < ans) ans = candidate;
}
}
return *pmemo = ans;
}
}
int main(void) {
int i;
if (scanf("%d%d", &N, &K) != 2) return 1;
for (i = 0; i < N; i++) {
if (scanf("%d%d", &X[i], &Y[i]) != 2) return 1;
}
printf("%" PRId64 "\n", calc(K, 0, (1 << N) - 1));
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | 045 - Simple Grouping(★6) |
| User | mikecat |
| Language | C (gcc 12.2.0) |
| Score | 6 |
| Code Size | 2660 Byte |
| Status | AC |
| Exec Time | 678 ms |
| Memory | 181752 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 6 / 6 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample01.txt, sample02.txt, sample03.txt, sample04.txt |
| All | dense00.txt, dense01.txt, dense02.txt, dense03.txt, mid_random00.txt, mid_random01.txt, mid_random02.txt, mid_random03.txt, mid_random04.txt, mid_random05.txt, random00.txt, random01.txt, random02.txt, random03.txt, random04.txt, random05.txt, random06.txt, sample01.txt, sample02.txt, sample03.txt, sample04.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| dense00.txt | AC | 7 ms | 11632 KiB |
| dense01.txt | AC | 7 ms | 11512 KiB |
| dense02.txt | AC | 0 ms | 1772 KiB |
| dense03.txt | AC | 431 ms | 144204 KiB |
| mid_random00.txt | AC | 7 ms | 11532 KiB |
| mid_random01.txt | AC | 1 ms | 1856 KiB |
| mid_random02.txt | AC | 0 ms | 1724 KiB |
| mid_random03.txt | AC | 1 ms | 1896 KiB |
| mid_random04.txt | AC | 92 ms | 80324 KiB |
| mid_random05.txt | AC | 0 ms | 1660 KiB |
| random00.txt | AC | 678 ms | 181752 KiB |
| random01.txt | AC | 7 ms | 11604 KiB |
| random02.txt | AC | 43 ms | 52972 KiB |
| random03.txt | AC | 28 ms | 32660 KiB |
| random04.txt | AC | 18 ms | 28516 KiB |
| random05.txt | AC | 1 ms | 3412 KiB |
| random06.txt | AC | 161 ms | 93488 KiB |
| sample01.txt | AC | 0 ms | 1760 KiB |
| sample02.txt | AC | 0 ms | 1800 KiB |
| sample03.txt | AC | 7 ms | 10996 KiB |
| sample04.txt | AC | 0 ms | 1764 KiB |