// Wojciech Sidor // #define DBG #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <deque> #include <list> #include <set> #include <map> #include <cstdlib> #include <cassert> #include <cmath> #include <cstdarg> using namespace std; #define PB push_back #define IT iterator #define MP make_pair #define F first #define S second #define X first #define Y second #define NL printf("\n") #define SZ(v) ((int)(v).size()) #define ALL(x) (x).begin(), (x).end() #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FORE(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define FORC(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i) #define W(x) do { if (DBG) printf("%s:%d %s = %d\n", __LINE__, __func__, #x, x); } while (0) typedef long double LD; typedef double DL; typedef long long LL; typedef unsigned long long ULL; typedef pair<LD,LD> PLD; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VPII; void memcheck() { #ifdef DBG char c[100]; FILE *p = fopen("/proc/self/status","r"); while (fgets(c,100,p) ) if (strncmp("VmPeak",c,6)==0) fputs(c, stderr); fclose(p); #endif } //------ /headers -------- void __printf__(const char *s, ...) { #ifdef DBG va_list vl; va_start(vl, s); vfprintf(stderr, s, vl); #endif } #define debprint(format, ...) do { __printf__("[%s:%d] " format "\n", __func__, __LINE__, __VA_ARGS__); } while (0); const int MAXN = 2003; int input[MAXN][MAXN]; int mins_up[MAXN][MAXN]; int mins_right[MAXN][MAXN]; ULL result[MAXN][MAXN]; int size; void read_input() { scanf("%d", &size); for (int i = 0; i < size; ++i) { for (int j = i; j < size; ++j) { scanf("%d", &input[i][j]); mins_up[i][j] = input[i][j]; mins_right[i][j] = input[i][j]; result[i][j] = input[i][j]; } } } void preprocess() { for (int i = 0; i < size; ++i) { for (int j = i - 1; j >= 0; --j) { mins_up[j][i] = min(mins_up[j + 1][i], input[j][i]); } } for (int i = 0; i < size; ++i) { for (int j = i + 1; j < size; ++j) { mins_right[i][j] = min(mins_right[i][j - 1], input[i][j]); } } } ULL get_result(int i, int j) { if (i > j) { return 0; } return result[i][j]; } int get_min(int begin, int end, int middle) { int mn = 1000000010; for (int i = begin; i <= middle; ++i) { for (int j = middle; j <= end; ++j) { mn = min(mn, input[i][j]); } } return mn; } void compute() { for (int len = 2; len <= size; ++len) { for (int begin = 0; begin <= size - len; ++begin) { ULL min_tmp = -1; int end = begin + len - 1; for (int middle = begin; middle <= end; ++middle) { debprint("[%d-%d-%d] bef: %llu middle: %d aft: %llu", begin, middle, len, get_result(begin, middle - 1), get_min(begin, end, middle), get_result(middle + 1, end)); min_tmp = min( min_tmp, get_result(begin, middle - 1) + get_min(begin, end, middle) + get_result(middle + 1, end)); } result[begin][end] = min_tmp; debprint("result[%d][%d] = %llu", begin, end, result[begin][end]); } } } int main() { read_input(); preprocess(); compute(); printf("%llu\n", result[0][size - 1]); }
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 | // Wojciech Sidor // #define DBG #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <deque> #include <list> #include <set> #include <map> #include <cstdlib> #include <cassert> #include <cmath> #include <cstdarg> using namespace std; #define PB push_back #define IT iterator #define MP make_pair #define F first #define S second #define X first #define Y second #define NL printf("\n") #define SZ(v) ((int)(v).size()) #define ALL(x) (x).begin(), (x).end() #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FORE(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define FORC(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i) #define W(x) do { if (DBG) printf("%s:%d %s = %d\n", __LINE__, __func__, #x, x); } while (0) typedef long double LD; typedef double DL; typedef long long LL; typedef unsigned long long ULL; typedef pair<LD,LD> PLD; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<PII> VPII; void memcheck() { #ifdef DBG char c[100]; FILE *p = fopen("/proc/self/status","r"); while (fgets(c,100,p) ) if (strncmp("VmPeak",c,6)==0) fputs(c, stderr); fclose(p); #endif } //------ /headers -------- void __printf__(const char *s, ...) { #ifdef DBG va_list vl; va_start(vl, s); vfprintf(stderr, s, vl); #endif } #define debprint(format, ...) do { __printf__("[%s:%d] " format "\n", __func__, __LINE__, __VA_ARGS__); } while (0); const int MAXN = 2003; int input[MAXN][MAXN]; int mins_up[MAXN][MAXN]; int mins_right[MAXN][MAXN]; ULL result[MAXN][MAXN]; int size; void read_input() { scanf("%d", &size); for (int i = 0; i < size; ++i) { for (int j = i; j < size; ++j) { scanf("%d", &input[i][j]); mins_up[i][j] = input[i][j]; mins_right[i][j] = input[i][j]; result[i][j] = input[i][j]; } } } void preprocess() { for (int i = 0; i < size; ++i) { for (int j = i - 1; j >= 0; --j) { mins_up[j][i] = min(mins_up[j + 1][i], input[j][i]); } } for (int i = 0; i < size; ++i) { for (int j = i + 1; j < size; ++j) { mins_right[i][j] = min(mins_right[i][j - 1], input[i][j]); } } } ULL get_result(int i, int j) { if (i > j) { return 0; } return result[i][j]; } int get_min(int begin, int end, int middle) { int mn = 1000000010; for (int i = begin; i <= middle; ++i) { for (int j = middle; j <= end; ++j) { mn = min(mn, input[i][j]); } } return mn; } void compute() { for (int len = 2; len <= size; ++len) { for (int begin = 0; begin <= size - len; ++begin) { ULL min_tmp = -1; int end = begin + len - 1; for (int middle = begin; middle <= end; ++middle) { debprint("[%d-%d-%d] bef: %llu middle: %d aft: %llu", begin, middle, len, get_result(begin, middle - 1), get_min(begin, end, middle), get_result(middle + 1, end)); min_tmp = min( min_tmp, get_result(begin, middle - 1) + get_min(begin, end, middle) + get_result(middle + 1, end)); } result[begin][end] = min_tmp; debprint("result[%d][%d] = %llu", begin, end, result[begin][end]); } } } int main() { read_input(); preprocess(); compute(); printf("%llu\n", result[0][size - 1]); } |