#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
// #define DEBUG
struct Stos {
Stos(vector<long long> placki) {
this->suma = suma;
for (int i = 0; i < placki.size(); i++) {
this->placki.push_back(placki[i]);
}
long long prefSum = 0;
pref.push_back(0);
for (int i = 0; i < placki.size(); i++) {
prefSum += placki[i];
pref.push_back(prefSum);
}
suma = prefSum;
}
long long suma;
vector<long long> placki;
vector<long long> pref;
const bool operator<(const Stos &stos) {
return suma >= stos.suma;
}
};
int n, m, k;
long long temp;
vector<long long> odwr, odwrSums;
vector<long long> tempV;
vector<Stos> stosy;
map<pair<int, int>, long long> placki_polowiczne;
vector<long long> pelne_stosy;
map<pair<int, int>, long long> najmniejsza_strata;
void handle1() {
for (int i = 0; i < n; i++) {
cin >> temp;
odwr.push_back(temp);
}
sort(odwr.begin(), odwr.end(), greater());
long long result = 0;
for (int i = 0; i < k; i++ ) {
result += odwr[i];
}
cout << result << endl;
}
void policz_placki_polowiczne() {
if (stosy.size() == 0) {
return;
}
for (int j = 1; j < m; j++) {
placki_polowiczne[make_pair(stosy.size()-1,j)] = stosy[stosy.size()-1].pref[j];
}
#ifdef DEBUG
cout << "PLACKI POLOWICZNE RUNDA PIERWSZA" << endl;
#endif // DEBUG
for (int i = stosy.size() - 2; i >=0; i--) {
for (int j = 1; j < m; j++) {
#ifdef DEBUG
cout << i << " " << j << " = max" << placki_polowiczne[make_pair(i+1, j)] << " LUB " << stosy[i].pref[j] << endl;
#endif // DEBUG
placki_polowiczne[make_pair(i,j)] = max(placki_polowiczne[make_pair(i+1, j)], stosy[i].pref[j]);
}
}
}
void policz_najmniejsze_straty() {
if (stosy.size() == 0) {
return;
}
for (int j = 1; j < m; j++) {
najmniejsza_strata[make_pair(0, j)] = stosy[0].pref[j] - stosy[0].suma;
}
for (int i = 1; i < stosy.size(); i++) {
for (int j = 1; j < m; j++) {
najmniejsza_strata[make_pair(i, j)] = max(najmniejsza_strata[make_pair(i-1, j)], stosy[i].pref[j] - stosy[i].suma);
}
}
}
long long odwrocone(int k) {
if (odwr.size() < k) {
return 0;
}
return odwrSums[k];
}
long long normalne(int k) {
#ifdef DEBUG
cout << endl << "NORMALNE " << k << endl;
#endif // DEBUG
if (k == 0 || stosy.size() < k/m) {
#ifdef DEBUG
cout << "JEST LIPA" << endl;
#endif // DEBUG
return 0;
}
if (k < m) {
return placki_polowiczne[make_pair(0, k)];
}
if (k % m == 0) {
return pelne_stosy[k/m];
}
if (stosy.size() == k/m) {
#ifdef DEBUG
cout << pelne_stosy[k/m] << "+" << placki_polowiczne[make_pair(k/m, k%m)] << endl;
#endif // DEBUG
return pelne_stosy[k/m] + placki_polowiczne[make_pair(k/m, k%m)];
}
#ifdef DEBUG
cout << pelne_stosy[k/m] << "+" << placki_polowiczne[make_pair(k/m, k%m)] << endl;
cout << pelne_stosy[k/m+1] << "+" << najmniejsza_strata[make_pair(k/m, k%m)] << endl;
#endif // DEBUG
return max(
pelne_stosy[k/m] + placki_polowiczne[make_pair(k/m, k%m)],
pelne_stosy[k/m+1] + najmniejsza_strata[make_pair(k/m, k%m)]
);
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m >> k;
if (m == 1) {
handle1();
return 0;
}
for (int i = 0; i < n; i++) {
bool normalny = false;
cin >> temp;
tempV.push_back(temp);
for (int j = 1; j < m; j++) {
cin >> temp;
if (temp > tempV[j-1]) {
normalny = true;
}
tempV.push_back(temp);
}
if (normalny) {
Stos stos(tempV);
stosy.push_back(stos);
} else {
for (int j = 0; j < m; j++) {
odwr.push_back(tempV[j]);
}
}
tempV.clear();
}
#ifdef DEBUG
cout << "DANE WCZYTANE" << endl;
#endif // DEBUG
sort(odwr.begin(), odwr.end(), greater());
sort(stosy.begin(), stosy.end());
#ifdef DEBUG
cout << "POSORTOWANE" << endl;
#endif // DEBUG
temp = 0;
odwrSums.push_back(0);
for (int i = 0; i < odwr.size(); i++) {
temp += odwr[i];
odwrSums.push_back(temp);
}
temp = 0;
pelne_stosy.push_back(0);
for (int i = 0; i < stosy.size(); i++) {
temp += stosy[i].suma;
pelne_stosy.push_back(temp);
}
#ifdef DEBUG
cout << "PELNE STOSY POLICZONE" << endl;
#endif // DEBUG
policz_placki_polowiczne();
#ifdef DEBUG
cout << "PLACKI POLOWICZNE POLICZONE" << endl;
#endif // DEBUG
policz_najmniejsze_straty();
#ifdef DEBUG
cout << "NAJMNIEJSZE STRATY POLICZONE" << endl;
#endif // DEBUG
#ifdef DEBUG
cout << "ODWR" << endl;
for (int i = 0; i < odwr.size(); i++) {
cout << odwr[i] << " ";
}
cout << endl << endl << "PLACKI POLOWICZNE" << endl;
for (int i = 0; i < stosy.size(); i++) {
for (int j = 1; j < m; j++) {
cout << placki_polowiczne[make_pair(i,j)] << " ";
}
cout << endl;
}
cout << endl << "NAJMNIEJSZE STRATY" << endl;
for (int i = 0; i < stosy.size(); i++) {
for (int j = 1; j < m; j++) {
cout << najmniejsza_strata[make_pair(i,j)] << " ";
}
cout << endl;
}
cout << endl;
#endif // DEBUG
long long best = 0;
for (int i = 0; i <= k && i/m<=stosy.size(); i++) {
long long a = normalne(i), b = odwrocone(k-i);
best = max(a+b, best);
#ifdef DEBUG
cout << "K: " << i << ", " << a << " " << b << " = " << a+b << endl;
#endif // DEBUG
}
cout << best << endl;
}
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | #include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; // #define DEBUG struct Stos { Stos(vector<long long> placki) { this->suma = suma; for (int i = 0; i < placki.size(); i++) { this->placki.push_back(placki[i]); } long long prefSum = 0; pref.push_back(0); for (int i = 0; i < placki.size(); i++) { prefSum += placki[i]; pref.push_back(prefSum); } suma = prefSum; } long long suma; vector<long long> placki; vector<long long> pref; const bool operator<(const Stos &stos) { return suma >= stos.suma; } }; int n, m, k; long long temp; vector<long long> odwr, odwrSums; vector<long long> tempV; vector<Stos> stosy; map<pair<int, int>, long long> placki_polowiczne; vector<long long> pelne_stosy; map<pair<int, int>, long long> najmniejsza_strata; void handle1() { for (int i = 0; i < n; i++) { cin >> temp; odwr.push_back(temp); } sort(odwr.begin(), odwr.end(), greater()); long long result = 0; for (int i = 0; i < k; i++ ) { result += odwr[i]; } cout << result << endl; } void policz_placki_polowiczne() { if (stosy.size() == 0) { return; } for (int j = 1; j < m; j++) { placki_polowiczne[make_pair(stosy.size()-1,j)] = stosy[stosy.size()-1].pref[j]; } #ifdef DEBUG cout << "PLACKI POLOWICZNE RUNDA PIERWSZA" << endl; #endif // DEBUG for (int i = stosy.size() - 2; i >=0; i--) { for (int j = 1; j < m; j++) { #ifdef DEBUG cout << i << " " << j << " = max" << placki_polowiczne[make_pair(i+1, j)] << " LUB " << stosy[i].pref[j] << endl; #endif // DEBUG placki_polowiczne[make_pair(i,j)] = max(placki_polowiczne[make_pair(i+1, j)], stosy[i].pref[j]); } } } void policz_najmniejsze_straty() { if (stosy.size() == 0) { return; } for (int j = 1; j < m; j++) { najmniejsza_strata[make_pair(0, j)] = stosy[0].pref[j] - stosy[0].suma; } for (int i = 1; i < stosy.size(); i++) { for (int j = 1; j < m; j++) { najmniejsza_strata[make_pair(i, j)] = max(najmniejsza_strata[make_pair(i-1, j)], stosy[i].pref[j] - stosy[i].suma); } } } long long odwrocone(int k) { if (odwr.size() < k) { return 0; } return odwrSums[k]; } long long normalne(int k) { #ifdef DEBUG cout << endl << "NORMALNE " << k << endl; #endif // DEBUG if (k == 0 || stosy.size() < k/m) { #ifdef DEBUG cout << "JEST LIPA" << endl; #endif // DEBUG return 0; } if (k < m) { return placki_polowiczne[make_pair(0, k)]; } if (k % m == 0) { return pelne_stosy[k/m]; } if (stosy.size() == k/m) { #ifdef DEBUG cout << pelne_stosy[k/m] << "+" << placki_polowiczne[make_pair(k/m, k%m)] << endl; #endif // DEBUG return pelne_stosy[k/m] + placki_polowiczne[make_pair(k/m, k%m)]; } #ifdef DEBUG cout << pelne_stosy[k/m] << "+" << placki_polowiczne[make_pair(k/m, k%m)] << endl; cout << pelne_stosy[k/m+1] << "+" << najmniejsza_strata[make_pair(k/m, k%m)] << endl; #endif // DEBUG return max( pelne_stosy[k/m] + placki_polowiczne[make_pair(k/m, k%m)], pelne_stosy[k/m+1] + najmniejsza_strata[make_pair(k/m, k%m)] ); } int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> k; if (m == 1) { handle1(); return 0; } for (int i = 0; i < n; i++) { bool normalny = false; cin >> temp; tempV.push_back(temp); for (int j = 1; j < m; j++) { cin >> temp; if (temp > tempV[j-1]) { normalny = true; } tempV.push_back(temp); } if (normalny) { Stos stos(tempV); stosy.push_back(stos); } else { for (int j = 0; j < m; j++) { odwr.push_back(tempV[j]); } } tempV.clear(); } #ifdef DEBUG cout << "DANE WCZYTANE" << endl; #endif // DEBUG sort(odwr.begin(), odwr.end(), greater()); sort(stosy.begin(), stosy.end()); #ifdef DEBUG cout << "POSORTOWANE" << endl; #endif // DEBUG temp = 0; odwrSums.push_back(0); for (int i = 0; i < odwr.size(); i++) { temp += odwr[i]; odwrSums.push_back(temp); } temp = 0; pelne_stosy.push_back(0); for (int i = 0; i < stosy.size(); i++) { temp += stosy[i].suma; pelne_stosy.push_back(temp); } #ifdef DEBUG cout << "PELNE STOSY POLICZONE" << endl; #endif // DEBUG policz_placki_polowiczne(); #ifdef DEBUG cout << "PLACKI POLOWICZNE POLICZONE" << endl; #endif // DEBUG policz_najmniejsze_straty(); #ifdef DEBUG cout << "NAJMNIEJSZE STRATY POLICZONE" << endl; #endif // DEBUG #ifdef DEBUG cout << "ODWR" << endl; for (int i = 0; i < odwr.size(); i++) { cout << odwr[i] << " "; } cout << endl << endl << "PLACKI POLOWICZNE" << endl; for (int i = 0; i < stosy.size(); i++) { for (int j = 1; j < m; j++) { cout << placki_polowiczne[make_pair(i,j)] << " "; } cout << endl; } cout << endl << "NAJMNIEJSZE STRATY" << endl; for (int i = 0; i < stosy.size(); i++) { for (int j = 1; j < m; j++) { cout << najmniejsza_strata[make_pair(i,j)] << " "; } cout << endl; } cout << endl; #endif // DEBUG long long best = 0; for (int i = 0; i <= k && i/m<=stosy.size(); i++) { long long a = normalne(i), b = odwrocone(k-i); best = max(a+b, best); #ifdef DEBUG cout << "K: " << i << ", " << a << " " << b << " = " << a+b << endl; #endif // DEBUG } cout << best << endl; } |
English