Editorial for Đường sắt Bắc-Nam
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của flashmt
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; long long a[555][555], f[555][555]; int n, k; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%lld", a[i] + j); for (int i = 1; i < n; i++) for (int j = n; j > i; j--) a[i][j] += a[j][i] - a[i - 1][j + 1] + a[i - 1][j] + a[i][j + 1]; for (int i = 1; i < n; i++) for (int j = 1; j <= k; j++) for (int ii = 0; ii < i; ii++) f[i][j] = max(f[i][j], f[ii][j - 1] + a[i][i + 1] - a[ii][i + 1]); long long ans = 0; for (int i = 1; i < n; i++) ans = max(ans, f[i][k]); cout << ans << endl; }
Code mẫu của happyboy99x
#include <iostream> #include <cassert> #include <cstring> #include <cstdio> using namespace std; const int N = 500; long long total[N][N], f[N][N]; int a[N][N], n, special; template<class T> bool checkMax(T &a, const T &b) { return a < b ? a = b, true : false; } int main() { #ifndef ONLINE_JUDGE assert(freopen("nsrail.in", "r", stdin)); #endif ios::sync_with_stdio(false); cin >> n >> special; for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) cin >> a[i][j]; for(int j = 0; j < n; ++j) { for(int i = j - 1; i >= 0; --i) { total[i][j] = total[i + 1][j]; for(int k = j; k < n; ++k) total[i][j] += a[i][k] + a[k][i]; } } f[0][0] = 0; for(int last = 0; last < n; ++last) { for(int put = 0; put < special; ++put) if(f[last][put] >= 0) { for(int next = last + 1; next < n; ++next) { checkMax(f[next][put + 1], f[last][put] + total[last][next]); } } } long long res = 0; for(int last = 0; last < n; ++last) checkMax(res, f[last][special]); printf("%lld\n", res); return 0; }
Code mẫu của ladpro98
#include <bits/stdc++.h> using namespace std; typedef long long word; const int N = 505; int n, k; word a[N][N], sumCost[N][N], cost[N][N]; word dp[2][N][N]; word temp[N]; void maximize(word &a, word b) { a = a < b ? b : a; } void genTest() { ofstream fout ("NSRAIL.txt"); int n = 500, k = 200; fout << n << ' ' << k << endl; srand(time(NULL)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != j) fout << rand() % 100000 << ' '; else fout << "0 "; } fout << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE genTest(); freopen("NSRAIL.txt", "r", stdin); #endif cin >> n >> k; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { cin >> a[i][j]; sumCost[min(i, j)][max(i, j)] += a[i][j]; if (i != j) { temp[min(i, j)] += a[i][j]; temp[max(i, j)] -= a[i][j]; } } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) sumCost[i][j] += sumCost[i - 1][j]; for (int i = 1; i <= n; ++i) temp[i] += temp[i - 1]; for (int i = 1; i < n; ++i) { cost[i][i] = temp[i]; for (int j = i + 1; j < n; ++j) cost[i][j] = cost[i][j - 1] - sumCost[i][j]; } int cur = 0, nxt = 1; for (int i = 1; i < n; ++i) { for (int j = 0; j <= i && j <= k; ++j) for (int p = 0; p <= i; ++p) dp[nxt][j][p] = 0; for (int j = 0; j < i && j <= k; ++j) for (int p = 0; p < i; ++p) { maximize(dp[nxt][j][p], dp[cur][j][p]); maximize(dp[nxt][j + 1][i], dp[cur][j][p] + cost[i][i] - cost[p][i]); } cur ^= 1; nxt ^= 1; } word ans = 0; for (int i = 0; i < n; ++i) maximize(ans, dp[cur][k][i]); cout << ans << endl; return 0; }
Code mẫu của RR
#include <sstream> #include <iomanip> #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <string> #include <deque> #include <complex> #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++) #define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--) #define SET(a,v) memset(a,v,sizeof(a)) #define sqr(x) ((x)*(x)) #define ll long long #define F first #define S second #define PB push_back #define MP make_pair #define DEBUG(x) cout << #x << " = "; cout << x << endl; #define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; #define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; using namespace std; //Buffer reading int INP,AM,REACHEOF; const int BUFSIZE = (1<<12) + 17; char BUF[BUFSIZE+1], *inp=BUF; #define GETCHAR(INP) { \ if(!*inp && !REACHEOF) { \ memset(BUF,0,sizeof BUF);\ int inpzzz = fread(BUF,1,BUFSIZE,stdin);\ if (inpzzz != BUFSIZE) REACHEOF = true;\ inp=BUF; \ } \ INP=*inp++; \ } #define DIG(a) (((a)>='0')&&((a)<='9')) #define GN(j) { \ AM=0;\ GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\ if (INP=='-') {AM=1;GETCHAR(INP);} \ j=INP-'0'; GETCHAR(INP); \ while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \ if (AM) j=-j;\ } //End of buffer reading const long double PI = acos((long double) -1.0); int n, k; int a[511][511]; long long sum[511][511], f[511][511]; int main() { while (scanf("%d%d", &n, &k) == 2) { FOR(i,1,n) FOR(j,1,n) { scanf("%d", &a[i][j]); } FOR(i,1,n) FOR(j,i+1,n) a[i][j] += a[j][i]; memset(sum, 0, sizeof sum); memset(f, 0, sizeof f); FORD(j,n,1) FOR(i,j,n) { sum[j][i] = sum[j+1][i]; FOR(x,i+1,n) sum[j][i] += a[j][x]; } long long res = 0; FOR(i,1,n-1) FOR(x,1,k) { FOR(j,0,i-1) f[i][x] = max(f[i][x], f[j][x-1] + sum[j+1][i]); res = max(res, f[i][x]); } cout << res << endl; } return 0; }
Code mẫu của hieult
#include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <sstream> #include <iomanip> #include <iostream> #include <algorithm> #include <ctime> #include <deque> #include <bitset> #include <cctype> #include <utility> #include <cassert> using namespace std; typedef long long ll; typedef double ld; typedef unsigned int ui; typedef unsigned long long ull; #define Rep(i,n) for(int i = 0; i < (n); ++i) #define Repd(i,n) for(int i = (n)-1; i >= 0; --i) #define For(i,a,b) for(int i = (a); i <= (b); ++i) #define Ford(i,a,b) for(int i = (a); i >= (b); --i) #define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i) //#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i) #define mp make_pair #define pb push_back #define fi first #define se second #define sz(a) ((int)(a).size()) #define all(a) (a).begin(), (a).end() #define ms(a,x) memset(a, x, sizeof(a)) template<class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template<class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> T sqr(T x) { return x * x; } template<class T> T cube(T x) { return x * x * x; } template<class T> int getbit(T s, int i) { return (s >> i) & 1; } template<class T> T onbit(T s, int i) { return s | (T(1) << i); } template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template<class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); } const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0; int ptr = 0; char gc() { if (rsz <= 0) { ptr = 0; rsz = (int) fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; } void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); } int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; } template<class T> bool gi(T &v) { v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc(); while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true; } typedef pair<int, int> II; const ld PI = acos(-1.0); const ld eps = 1e-9; const int dr[] = {0, +1, 0, -1}; const int dc[] = {+1, 0, -1, 0}; const int inf = (int)1e9 + 5; const ll linf = (ll)1e16 + 5; //const ll mod = (ll)1e9 + 7; #define maxn 505 ll f[505][505], l[505][505]; int a[505][505], d[505][505], c[505]; int n, num; int main(){ // freopen("in.txt", "r", stdin); ms(d, 0); ms(f, 0); ms(c, 0); ms(l, 0); gi(n); gi(num); For(i, 1, n) { For(j, 1, n){ gi(a[i][j]); if(j < i){ a[j][i] += a[i][j]; a[i][j] = 0; } } } For(i, 1, n) For(j, i + 1, n){ a[i][j - 1] = a[i][j]; } n--; For(i, 1, n) For(j, i, n) d[i][j] = d[i][j - 1] + a[i][j]; For(i, 1, n){ For(j, 1, i) c[i] += a[j][i]; } For(i, 1, n){ l[1][i] = d[1][i]; For(j, 2, i) l[j][i] += l[j - 1][i] + d[j][i] - c[j - 1]; } For(i, 1, n){ For(j, 1, i) if(l[j][i] > f[1][i]){ f[1][i] = l[j][i]; } } For(run, 2, num){ For(i, 1, n){ f[run][i] = f[run][i - 1]; For(j, 1, i) if(f[run][i] < f[run - 1][j - 1] + l[j][i]){ f[run][i] = f[run - 1][j - 1] + l[j][i]; } } } cout << f[num][n] << endl; }
Comments