#include <stdlib.h> //---===--- #include <stdio.h> #include <unistd.h> typedef unsigned int uint; char buf_in[4096000]; int buf_in_head = 0; int buf_in_tail = 0; static int getChar(void) { while (buf_in_head == buf_in_tail) { int rv = read(0, buf_in, sizeof(buf_in)); if (rv > 0) { buf_in_head = 0; buf_in_tail = rv; break; }; if (rv == 0) return EOF; } return buf_in[buf_in_head++]; } // only 1 call is safe, and only if previously getChar() had been called. static void ungetChar(void) { --buf_in_head; } static uint getUInt() { uint v; int c; for (;;) { c = getChar(); if (c < '0') continue; if (c > '9') continue; v = c - '0'; break; }; for (;;) { c = getChar(); if (c < '0') break; if (c > '9') break; v *= 10; v += c - '0'; }; ungetChar(); return v; } //---===--- #define INF 1222333444 #define C2000 2048 int n; // 0 <= i <= j < n // 1 <= c[i][j] <= 1,000,000,000 int c[C2000][C2000]; // 16 MB int join(int i, int j) { return -i * C2000 - j -1; } int first(int v) { return (-v-1) / C2000; } int second(int v) { return (-v-1) % C2000; } int find_min(void) { int i, j; int m = INF, k = 0; for (i = 0; i < n; ++i) { for (j = i; j < n; ++j) { if (c[i][j] <= 0) continue; if (c[i][j] > m) continue; m = c[i][j]; k = join(i, j); }; }; return k; } int lookup(int i, int j) { int a = i; int b = j; int w = join(a, b); for (;;) { int v = c[a][b]; if (v >= 0) break; a = first(v); b = second(v); w = v; }; // w == join(a,b) ---> ultimate parent for (;;) { int v = c[i][j]; if (v >= 0) break; c[i][j] = w; i = first(v); j = second(v); }; return w; }; void resolve(int i1, int j1, int i2, int j2) { int ij1, v1, ij2, v2; ij1 = lookup(i1, j1); i1 = first(ij1); j1 = second(ij1); v1 = c[i1][j1]; ij2 = lookup(i2, j2); i2 = first(ij2); j2 = second(ij2); v2 = c[i2][j2]; if (ij1 == ij2) return; if (v1 == 0) if (v2 == 0) return; if (v1 == 0) { c[i2][j2] = ij1; return; }; if (v2 == 0) { c[i1][j1] = ij2; return; }; if (v1 < v2) { c[i2][j2] = ij1; } else { c[i1][j1] = ij2; }; } typedef struct { int i, j, c; } tt; tt T[C2000 * C2000 / 2]; // 24 MB int cnt = 0; int cmp(const tt * a, const tt * b) { if (a->c < b->c) return -1; if (a->c > b->c) return +1; if (a->i < b->i) return -1; if (a->i > b->i) return +1; if (a->j < b->j) return -1; if (a->j > b->j) return +1; return 0; }; int void_cmp(const void * a, const void * b) { return -cmp((const tt*)a, (const tt*)b); } int main (void) { int i, j; long long sum = 0; // 1 <= n <= 2000 //scanf("%d", &n); n = getUInt(); for (i = 0; i < n; ++i) { for (j = i; j < n; ++j) { int v; //scanf("%d", &v); v = getUInt(); c[i][j] = v; T[cnt].i = i; T[cnt].j = j; T[cnt].c = v; ++cnt; }; }; // liczymy na to ze to nie ja pisalem testy qsort(T, cnt, sizeof(tt), void_cmp); for (i = 0; i < n; ++i) { int a, b; //int v = find_min(); //a = first(v); //b = second(v); --cnt; a = T[cnt].i; b = T[cnt].j; //printf("%d,%d -> %d <=0 or ==%d\n", a,b, c[a][b], T[cnt].c); if (c[a][b] <= 0) { --i; continue; }; //printf("%d @ %d,%d\n", c[a][b], a, b); sum += c[a][b]; c[a][b] = 0; for (j = a; j < b; ++j) { //printf("%d,%d -- %d,%d\n", a,j, j+1,b); resolve(a,j, j+1,b); }; for (j = a-1; j >= 0; --j) { //printf("%d,%d =- %d,%d\n", j,a-1, j,b); resolve(j,a-1, j,b); }; for (j = b+1; j < n; ++j) { //printf("%d,%d -= %d,%d\n", a,j, b+1,j); resolve(a,j, b+1,j); }; //putchar('\n'); }; printf("%lld\n", sum); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | #include <stdlib.h> //---===--- #include <stdio.h> #include <unistd.h> typedef unsigned int uint; char buf_in[4096000]; int buf_in_head = 0; int buf_in_tail = 0; static int getChar(void) { while (buf_in_head == buf_in_tail) { int rv = read(0, buf_in, sizeof(buf_in)); if (rv > 0) { buf_in_head = 0; buf_in_tail = rv; break; }; if (rv == 0) return EOF; } return buf_in[buf_in_head++]; } // only 1 call is safe, and only if previously getChar() had been called. static void ungetChar(void) { --buf_in_head; } static uint getUInt() { uint v; int c; for (;;) { c = getChar(); if (c < '0') continue; if (c > '9') continue; v = c - '0'; break; }; for (;;) { c = getChar(); if (c < '0') break; if (c > '9') break; v *= 10; v += c - '0'; }; ungetChar(); return v; } //---===--- #define INF 1222333444 #define C2000 2048 int n; // 0 <= i <= j < n // 1 <= c[i][j] <= 1,000,000,000 int c[C2000][C2000]; // 16 MB int join(int i, int j) { return -i * C2000 - j -1; } int first(int v) { return (-v-1) / C2000; } int second(int v) { return (-v-1) % C2000; } int find_min(void) { int i, j; int m = INF, k = 0; for (i = 0; i < n; ++i) { for (j = i; j < n; ++j) { if (c[i][j] <= 0) continue; if (c[i][j] > m) continue; m = c[i][j]; k = join(i, j); }; }; return k; } int lookup(int i, int j) { int a = i; int b = j; int w = join(a, b); for (;;) { int v = c[a][b]; if (v >= 0) break; a = first(v); b = second(v); w = v; }; // w == join(a,b) ---> ultimate parent for (;;) { int v = c[i][j]; if (v >= 0) break; c[i][j] = w; i = first(v); j = second(v); }; return w; }; void resolve(int i1, int j1, int i2, int j2) { int ij1, v1, ij2, v2; ij1 = lookup(i1, j1); i1 = first(ij1); j1 = second(ij1); v1 = c[i1][j1]; ij2 = lookup(i2, j2); i2 = first(ij2); j2 = second(ij2); v2 = c[i2][j2]; if (ij1 == ij2) return; if (v1 == 0) if (v2 == 0) return; if (v1 == 0) { c[i2][j2] = ij1; return; }; if (v2 == 0) { c[i1][j1] = ij2; return; }; if (v1 < v2) { c[i2][j2] = ij1; } else { c[i1][j1] = ij2; }; } typedef struct { int i, j, c; } tt; tt T[C2000 * C2000 / 2]; // 24 MB int cnt = 0; int cmp(const tt * a, const tt * b) { if (a->c < b->c) return -1; if (a->c > b->c) return +1; if (a->i < b->i) return -1; if (a->i > b->i) return +1; if (a->j < b->j) return -1; if (a->j > b->j) return +1; return 0; }; int void_cmp(const void * a, const void * b) { return -cmp((const tt*)a, (const tt*)b); } int main (void) { int i, j; long long sum = 0; // 1 <= n <= 2000 //scanf("%d", &n); n = getUInt(); for (i = 0; i < n; ++i) { for (j = i; j < n; ++j) { int v; //scanf("%d", &v); v = getUInt(); c[i][j] = v; T[cnt].i = i; T[cnt].j = j; T[cnt].c = v; ++cnt; }; }; // liczymy na to ze to nie ja pisalem testy qsort(T, cnt, sizeof(tt), void_cmp); for (i = 0; i < n; ++i) { int a, b; //int v = find_min(); //a = first(v); //b = second(v); --cnt; a = T[cnt].i; b = T[cnt].j; //printf("%d,%d -> %d <=0 or ==%d\n", a,b, c[a][b], T[cnt].c); if (c[a][b] <= 0) { --i; continue; }; //printf("%d @ %d,%d\n", c[a][b], a, b); sum += c[a][b]; c[a][b] = 0; for (j = a; j < b; ++j) { //printf("%d,%d -- %d,%d\n", a,j, j+1,b); resolve(a,j, j+1,b); }; for (j = a-1; j >= 0; --j) { //printf("%d,%d =- %d,%d\n", j,a-1, j,b); resolve(j,a-1, j,b); }; for (j = b+1; j < n; ++j) { //printf("%d,%d -= %d,%d\n", a,j, b+1,j); resolve(a,j, b+1,j); }; //putchar('\n'); }; printf("%lld\n", sum); return 0; } |