#include <iostream>
#include <cstdint>
#include <vector>
#include <unordered_map>
using namespace std;
struct Row
{
vector<int64_t> m_va;
};
static bool fReverseOfBabylon;
static int iRows, iCols, k, iLastCol, iLastRow;
static int64_t iMinMaxSum = 0;
static int64_t iTotal = 0;
static vector<Row> vRows;
static vector<int64_t> vAggrSumFromTheBottom;
static unordered_map<int64_t, int64_t> mDP;
static void ReadData()
{
int64_t a;
cin >> iRows >> iCols >> k;
iLastCol = iCols - 1;
iLastRow = iRows - 1;
fReverseOfBabylon = (k > (iRows * iCols) / 2);
vRows.resize(iRows);
for (int i = 0; i < iRows; ++i)
{
Row & row = vRows[i];
row.m_va.resize(iCols);
for (int j = 0; j < iCols; ++j)
{
cin >> a;
row.m_va[j] = a;
iTotal += a;
vAggrSumFromTheBottom.push_back(a);
}
if (fReverseOfBabylon)
{
reverse(row.m_va.begin(), row.m_va.end());
}
}
}
static void Init()
{
reverse(vAggrSumFromTheBottom.begin(), vAggrSumFromTheBottom.end());
for (size_t i = 1; i < vAggrSumFromTheBottom.size(); ++i)
{
vAggrSumFromTheBottom[i] += vAggrSumFromTheBottom[i - 1];
}
}
static inline bool IsLeftBetter(int64_t iLeft, int64_t iRight)
{
if (fReverseOfBabylon)
{
return iLeft < iRight;
}
return iLeft > iRight;
}
static int64_t Recursive(int iRow, int iCol, int iTakes, int iToBottom);
static inline int64_t RecursiveInternal(int iRow, int iCol, int iTakes, int iToBottom)
{
int64_t iBestVal = -1;
int64_t iCandidate;
// take + right
if (iCol < iLastCol)
{
iCandidate = Recursive(iRow, iCol + 1, iTakes - 1, iToBottom - 1);
if (iCandidate != -1)
{
iBestVal = vRows[iRow].m_va[iCol] + iCandidate;
}
}
if (iRow == iLastRow)
return iBestVal;
iToBottom = (iLastRow - iRow) * iCols;
// take + down
if (iToBottom >= iTakes - 1)
{
iCandidate = Recursive(iRow + 1, 0, iTakes - 1, iToBottom);
if (iCandidate != -1)
{
iCandidate += vRows[iRow].m_va[iCol];
}
if (iBestVal == -1)
{
iBestVal = iCandidate;
}
else if (iCandidate != -1 && IsLeftBetter(iCandidate, iBestVal))
{
iBestVal = iCandidate;
}
}
// no take + down
if (iToBottom >= iTakes)
{
iCandidate = Recursive(iRow + 1, 0, iTakes, iToBottom);
if (iBestVal == -1)
{
iBestVal = iCandidate;
}
else if (iCandidate != -1 && IsLeftBetter(iCandidate, iBestVal))
{
iBestVal = iCandidate;
}
}
return iBestVal;
}
static int64_t Recursive(int iRow, int iCol, int iTakes, int iToBottom)
{
if (iTakes == 0)
return 0;
if (iTakes == iToBottom)
{
return vAggrSumFromTheBottom[iTakes - 1];
}
int64_t iKey = iRow;
iKey <<= 20;
iKey |= iCol;
iKey <<= 20;
iKey |= iTakes;
if (mDP.count(iKey) != 0)
{
return mDP[iKey];
}
int64_t iRet = RecursiveInternal(iRow, iCol, iTakes, iToBottom);
mDP[iKey] = iRet;
return iRet;
}
static void Solve()
{
int iTakes = fReverseOfBabylon ? iRows * iCols - k : k;
iMinMaxSum = Recursive(0, 0, iTakes, iRows * iCols);
if (fReverseOfBabylon)
{
iMinMaxSum = iTotal - iMinMaxSum;
}
}
int main()
{
// freopen("sample_input.txt", "r", stdin);
// freopen("sample_output.txt", "w", stdout);
ReadData();
Init();
Solve();
cout << iMinMaxSum;
}
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 <iostream> #include <cstdint> #include <vector> #include <unordered_map> using namespace std; struct Row { vector<int64_t> m_va; }; static bool fReverseOfBabylon; static int iRows, iCols, k, iLastCol, iLastRow; static int64_t iMinMaxSum = 0; static int64_t iTotal = 0; static vector<Row> vRows; static vector<int64_t> vAggrSumFromTheBottom; static unordered_map<int64_t, int64_t> mDP; static void ReadData() { int64_t a; cin >> iRows >> iCols >> k; iLastCol = iCols - 1; iLastRow = iRows - 1; fReverseOfBabylon = (k > (iRows * iCols) / 2); vRows.resize(iRows); for (int i = 0; i < iRows; ++i) { Row & row = vRows[i]; row.m_va.resize(iCols); for (int j = 0; j < iCols; ++j) { cin >> a; row.m_va[j] = a; iTotal += a; vAggrSumFromTheBottom.push_back(a); } if (fReverseOfBabylon) { reverse(row.m_va.begin(), row.m_va.end()); } } } static void Init() { reverse(vAggrSumFromTheBottom.begin(), vAggrSumFromTheBottom.end()); for (size_t i = 1; i < vAggrSumFromTheBottom.size(); ++i) { vAggrSumFromTheBottom[i] += vAggrSumFromTheBottom[i - 1]; } } static inline bool IsLeftBetter(int64_t iLeft, int64_t iRight) { if (fReverseOfBabylon) { return iLeft < iRight; } return iLeft > iRight; } static int64_t Recursive(int iRow, int iCol, int iTakes, int iToBottom); static inline int64_t RecursiveInternal(int iRow, int iCol, int iTakes, int iToBottom) { int64_t iBestVal = -1; int64_t iCandidate; // take + right if (iCol < iLastCol) { iCandidate = Recursive(iRow, iCol + 1, iTakes - 1, iToBottom - 1); if (iCandidate != -1) { iBestVal = vRows[iRow].m_va[iCol] + iCandidate; } } if (iRow == iLastRow) return iBestVal; iToBottom = (iLastRow - iRow) * iCols; // take + down if (iToBottom >= iTakes - 1) { iCandidate = Recursive(iRow + 1, 0, iTakes - 1, iToBottom); if (iCandidate != -1) { iCandidate += vRows[iRow].m_va[iCol]; } if (iBestVal == -1) { iBestVal = iCandidate; } else if (iCandidate != -1 && IsLeftBetter(iCandidate, iBestVal)) { iBestVal = iCandidate; } } // no take + down if (iToBottom >= iTakes) { iCandidate = Recursive(iRow + 1, 0, iTakes, iToBottom); if (iBestVal == -1) { iBestVal = iCandidate; } else if (iCandidate != -1 && IsLeftBetter(iCandidate, iBestVal)) { iBestVal = iCandidate; } } return iBestVal; } static int64_t Recursive(int iRow, int iCol, int iTakes, int iToBottom) { if (iTakes == 0) return 0; if (iTakes == iToBottom) { return vAggrSumFromTheBottom[iTakes - 1]; } int64_t iKey = iRow; iKey <<= 20; iKey |= iCol; iKey <<= 20; iKey |= iTakes; if (mDP.count(iKey) != 0) { return mDP[iKey]; } int64_t iRet = RecursiveInternal(iRow, iCol, iTakes, iToBottom); mDP[iKey] = iRet; return iRet; } static void Solve() { int iTakes = fReverseOfBabylon ? iRows * iCols - k : k; iMinMaxSum = Recursive(0, 0, iTakes, iRows * iCols); if (fReverseOfBabylon) { iMinMaxSum = iTotal - iMinMaxSum; } } int main() { // freopen("sample_input.txt", "r", stdin); // freopen("sample_output.txt", "w", stdout); ReadData(); Init(); Solve(); cout << iMinMaxSum; } |
English