const int MAXN = 1010; int S[MAXN][MAXN]; int ipt[MAXN]; int dp[MAXN]; inline int query(int l, int r) { return S[r][r] - S[l - 1][r] - S[r][l - 1] + S[l - 1][l - 1]; } int xx[MAXN], tot = 0, to[MAXN]; int main() { tot = 1; int n, Max, k; RIII(n, Max, k); REP(i, n) { RI(ipt[i]); xx[tot++] = ipt[i]; } sort(xx, xx + tot); int idx = 1; FF(i, 1, tot) if (xx[i] != xx[i - 1]) xx[idx++] = xx[i]; tot = idx; REP(i, tot) to[xx[i]] = i; Max = tot; REP(i, n) ipt[i] = to[ipt[i]]; for (int i = 0; i <= Max; i++) dp[i] = INF; dp[0] = 0; REP(i, n) FF(j, i + 1, n) if (ipt[i] < ipt[j]) S[ipt[i]][ipt[j]]++; FE(i, 1, Max) FE(j, 1, Max) S[i][j] += S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1]; REP(kase, k) { FED(i, Max, 0) { for (int j = Max; j >= i + 1; j--) { int nxt = dp[i] + query(i + 1, j); if (nxt < dp[j]) dp[j] = nxt; } } } WI(dp[Max]); return 0; }
const int MAXN = 1010; int S[MAXN][MAXN]; int ipt[MAXN]; int dp[MAXN]; inline int query(int l, int r) { return S[r][r] - S[l - 1][r] - S[r][l - 1] + S[l - 1][l - 1]; } int main() { int n, Max, k; scanf("%d%d%d", &n, &Max, &k); REP(i, n) scanf("%d", &ipt[i]); for (int i = 0; i <= Max; i++) dp[i] = INF; dp[0] = 0; REP(i, n) FF(j, i + 1, n) if (ipt[i] < ipt[j]) S[ipt[i]][ipt[j]]++; FE(i, 1, Max) FE(j, 1, Max) S[i][j] += S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1]; REP(kase, k) { FED(i, Max, 0) { for (int j = Max; j >= i + 1; j--) { int nxt = dp[i] + query(i + 1, j); if (nxt < dp[j]) dp[j] = nxt; } } } WI(dp[Max]); return 0; }
Flight Boarding Optimization,布布扣,bubuko.com
原文地址:http://blog.csdn.net/wty__/article/details/38234127