#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#define REP(x,n) for(int x=0;x<(n);++x)
//#define DEBUG_LEVEL 0
#ifdef DEBUG_LEVEL
#define DEBUG(level, x) {if (level >= DEBUG_LEVEL) {x}}
#else
#define DEBUG(level, x)
#endif
using namespace std;
const int MAX_P = 300000;
const int MAX_N = 300000;
long long reversedPancakes[MAX_P];
long long reversedPancakesSum[MAX_P];
int reversedPancakesCount = 0;
vector<long long> normalPancakes[MAX_N];
vector<long long> normalPancakesSum[MAX_N];
int sortedPancakesStacks[MAX_N];
void calculatePrefixSums(long long* array, long long* prefixSums, int count) {
prefixSums[0] = array[0];
for(int x=1; x < count; ++x) {
prefixSums[x] = prefixSums[x-1] + array[x];
}
}
long long inline subSum(long long* prefixSums, int from, int toExclusive) {
DEBUG(0, cerr << " subSum from " << from << " to " << toExclusive << endl;)
if (from == 0) {
return prefixSums[toExclusive-1];
}
return prefixSums[toExclusive-1] - prefixSums[from-1];
}
bool shouldConsumeStack(int stackIndex, int reversedPancakeIndex, int pancakesToConsider) {
if (reversedPancakeIndex >= reversedPancakesCount) {
DEBUG(2, cerr << " No more reversed pancakes, consuming stack " << stackIndex << endl;)
return true;
}
long long massForNormalStack = *normalPancakesSum[stackIndex].rbegin();
long long massForReversedStackPiece = subSum(reversedPancakesSum, reversedPancakeIndex, min(reversedPancakesCount, reversedPancakeIndex + pancakesToConsider));
DEBUG(2, cerr << " Comparing stack " << stackIndex << " with mass " << massForNormalStack << " and reversed pancakes piece with mass " << massForReversedStackPiece << endl;)
return massForNormalStack > massForReversedStackPiece;
}
int main() {
int n,m,k;
cin >> n >> m >> k;
long long first, last;
int index = 0;
REP(x,n) {
normalPancakes[index].resize(m);
REP(y,m) {
cin >> normalPancakes[index][y];
}
first = normalPancakes[index][0];
last = normalPancakes[index][m-1];
if (first >= last) {
// add to reversed pancakes
DEBUG(0,
cerr<< "Stack added to reversed: ";
REP(i,m) {
cerr << normalPancakes[index][i] << " ";
}
cerr << endl;
)
std::copy(normalPancakes[index].begin(), normalPancakes[index].end(), &reversedPancakes[reversedPancakesCount]);
reversedPancakesCount += m;
} else {
DEBUG(0,
cerr<< "Stack applied normally: ";
REP(i,m) {
cerr << normalPancakes[index][i] << " ";
}
cerr << endl;
)
normalPancakesSum[index].resize(m);
calculatePrefixSums(&normalPancakes[index][0], &normalPancakesSum[index][0], m);
sortedPancakesStacks[index] = index;
++index;
}
}
n = index; // only that much stacks are left
sort(reversedPancakes, reversedPancakes + reversedPancakesCount, greater<long long>());
DEBUG(1,
cerr << "Reversed pancakes: " << endl;
REP(x, reversedPancakesCount) {
cerr << reversedPancakes[x] << " ";
}
cerr << endl;
)
calculatePrefixSums(reversedPancakes, reversedPancakesSum, reversedPancakesCount);
// for now it makes sense to consume whole stack at once
sort(sortedPancakesStacks, sortedPancakesStacks + n, [&](int a, int b) {
return normalPancakesSum[a][m-1] > normalPancakesSum[b][m-1];
});
DEBUG(1,
cerr << "Normal pancakes stacks sorted by total mass: " << endl;
REP(x, n) {
int stackIndex = sortedPancakesStacks[x];
cerr << "Stack " << stackIndex << " with total mass " << normalPancakesSum[stackIndex][m-1] << " and pancakes: ";
REP(i, m) {
cerr << normalPancakes[stackIndex][i] << " ";
}
cerr << endl;
}
);
long long consumedMass = 0;
long long consumedCount = 0;
int pancakesStackIndex = 0;
int reversedPancakeIndex = 0;
DEBUG(1, cerr << "best stack is " << sortedPancakesStacks[pancakesStackIndex] << " with mass " << normalPancakesSum[sortedPancakesStacks[pancakesStackIndex]][m-1] << endl;)
while (consumedCount + m <= k) {
if (pancakesStackIndex < n && shouldConsumeStack(sortedPancakesStacks[pancakesStackIndex], reversedPancakeIndex, m)) {
int stackIndex = sortedPancakesStacks[pancakesStackIndex];
DEBUG(2, cerr << "consuming stack " << stackIndex << " with " << m << " pancakes and mass " << normalPancakesSum[stackIndex][m-1] << endl;)
consumedMass += normalPancakesSum[stackIndex][m-1];
consumedCount += m;
++pancakesStackIndex;
DEBUG(2, cerr << " new best stack is " << sortedPancakesStacks[pancakesStackIndex] << " with mass " << normalPancakesSum[sortedPancakesStacks[pancakesStackIndex]][m-1] << endl;)
} else if (reversedPancakeIndex < reversedPancakesCount) {
DEBUG(2, cerr << "consuming single reversed pancake " << reversedPancakeIndex << " with mass " << reversedPancakes[reversedPancakeIndex] << endl;)
consumedMass += reversedPancakes[reversedPancakeIndex];
++consumedCount;
++reversedPancakeIndex;
} else {
cerr << "SHOULD NEVER HAPPEN" << endl;
//should never happen
break;
}
}
if (pancakesStackIndex == n) {
// all stacks are consumed, consume reversed pancakes until k
long long remainingMass = subSum(reversedPancakesSum, reversedPancakeIndex, reversedPancakeIndex + (k - consumedCount));
consumedMass += remainingMass;
DEBUG(2, cerr << "consuming " << (k - consumedCount) << " reversed pancakes from " << reversedPancakeIndex << " with total mass " << remainingMass << endl;)
DEBUG(2, cerr << "Result is " << consumedMass << endl;)
cout << consumedMass;
return 0;
}
//consume reversed pancakes one by one, until single stack becomes larger (which ends the flow)
for (int remainingK = k - consumedCount; remainingK > 0; --remainingK) {
DEBUG(2, cerr << "Remaining pancakes: " << remainingK << endl;)
//it must always exist, because consuming a stack will end the flow
int bestStackIndex = *std::max_element(sortedPancakesStacks + pancakesStackIndex, sortedPancakesStacks + n, [&](int a, int b) {
return normalPancakesSum[a][remainingK-1] < normalPancakesSum[b][remainingK-1];
});
DEBUG(0, cerr << " range is (" << pancakesStackIndex << ", " << n << "), best is " << bestStackIndex << endl;)
long long bestStackMass = normalPancakesSum[bestStackIndex][remainingK-1];
long long reversedPancakesMass = subSum(reversedPancakesSum, reversedPancakeIndex, min(reversedPancakeIndex + remainingK, reversedPancakesCount));
DEBUG(1, cerr << " Best stack for remaining " << remainingK << " pancakes is " << bestStackIndex << " with mass " << bestStackMass << endl;)
DEBUG(1, cerr << " Reversed pancakes for remaining " << remainingK << " pancakes is " << reversedPancakesMass << endl;)
if (reversedPancakesMass >= bestStackMass) {
// consume reversed pancake
DEBUG(2, cerr << " Consuming single reversed pancake " << reversedPancakeIndex << " with mass " << reversedPancakes[reversedPancakeIndex] << endl;)
consumedMass += reversedPancakes[reversedPancakeIndex];
++consumedCount;
++reversedPancakeIndex;
} else if (reversedPancakes[reversedPancakeIndex] >= normalPancakes[bestStackIndex][remainingK-1]) {
// consume reversed pancake
DEBUG(2, cerr << " Consuming single reversed pancake " << reversedPancakeIndex << " with mass " << reversedPancakes[reversedPancakeIndex] << " even though stack is greater" << endl;)
consumedMass += reversedPancakes[reversedPancakeIndex];
++consumedCount;
++reversedPancakeIndex;
} else {
// consume stack
DEBUG(2, cerr << " Consuming stack " << bestStackIndex << " with mass " << bestStackMass << endl;)
consumedMass += bestStackMass;
consumedCount += remainingK;
break;
}
}
DEBUG(2, cerr << "Result is " << consumedMass << endl;)
cout << consumedMass;
return 0;
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <set> #define REP(x,n) for(int x=0;x<(n);++x) //#define DEBUG_LEVEL 0 #ifdef DEBUG_LEVEL #define DEBUG(level, x) {if (level >= DEBUG_LEVEL) {x}} #else #define DEBUG(level, x) #endif using namespace std; const int MAX_P = 300000; const int MAX_N = 300000; long long reversedPancakes[MAX_P]; long long reversedPancakesSum[MAX_P]; int reversedPancakesCount = 0; vector<long long> normalPancakes[MAX_N]; vector<long long> normalPancakesSum[MAX_N]; int sortedPancakesStacks[MAX_N]; void calculatePrefixSums(long long* array, long long* prefixSums, int count) { prefixSums[0] = array[0]; for(int x=1; x < count; ++x) { prefixSums[x] = prefixSums[x-1] + array[x]; } } long long inline subSum(long long* prefixSums, int from, int toExclusive) { DEBUG(0, cerr << " subSum from " << from << " to " << toExclusive << endl;) if (from == 0) { return prefixSums[toExclusive-1]; } return prefixSums[toExclusive-1] - prefixSums[from-1]; } bool shouldConsumeStack(int stackIndex, int reversedPancakeIndex, int pancakesToConsider) { if (reversedPancakeIndex >= reversedPancakesCount) { DEBUG(2, cerr << " No more reversed pancakes, consuming stack " << stackIndex << endl;) return true; } long long massForNormalStack = *normalPancakesSum[stackIndex].rbegin(); long long massForReversedStackPiece = subSum(reversedPancakesSum, reversedPancakeIndex, min(reversedPancakesCount, reversedPancakeIndex + pancakesToConsider)); DEBUG(2, cerr << " Comparing stack " << stackIndex << " with mass " << massForNormalStack << " and reversed pancakes piece with mass " << massForReversedStackPiece << endl;) return massForNormalStack > massForReversedStackPiece; } int main() { int n,m,k; cin >> n >> m >> k; long long first, last; int index = 0; REP(x,n) { normalPancakes[index].resize(m); REP(y,m) { cin >> normalPancakes[index][y]; } first = normalPancakes[index][0]; last = normalPancakes[index][m-1]; if (first >= last) { // add to reversed pancakes DEBUG(0, cerr<< "Stack added to reversed: "; REP(i,m) { cerr << normalPancakes[index][i] << " "; } cerr << endl; ) std::copy(normalPancakes[index].begin(), normalPancakes[index].end(), &reversedPancakes[reversedPancakesCount]); reversedPancakesCount += m; } else { DEBUG(0, cerr<< "Stack applied normally: "; REP(i,m) { cerr << normalPancakes[index][i] << " "; } cerr << endl; ) normalPancakesSum[index].resize(m); calculatePrefixSums(&normalPancakes[index][0], &normalPancakesSum[index][0], m); sortedPancakesStacks[index] = index; ++index; } } n = index; // only that much stacks are left sort(reversedPancakes, reversedPancakes + reversedPancakesCount, greater<long long>()); DEBUG(1, cerr << "Reversed pancakes: " << endl; REP(x, reversedPancakesCount) { cerr << reversedPancakes[x] << " "; } cerr << endl; ) calculatePrefixSums(reversedPancakes, reversedPancakesSum, reversedPancakesCount); // for now it makes sense to consume whole stack at once sort(sortedPancakesStacks, sortedPancakesStacks + n, [&](int a, int b) { return normalPancakesSum[a][m-1] > normalPancakesSum[b][m-1]; }); DEBUG(1, cerr << "Normal pancakes stacks sorted by total mass: " << endl; REP(x, n) { int stackIndex = sortedPancakesStacks[x]; cerr << "Stack " << stackIndex << " with total mass " << normalPancakesSum[stackIndex][m-1] << " and pancakes: "; REP(i, m) { cerr << normalPancakes[stackIndex][i] << " "; } cerr << endl; } ); long long consumedMass = 0; long long consumedCount = 0; int pancakesStackIndex = 0; int reversedPancakeIndex = 0; DEBUG(1, cerr << "best stack is " << sortedPancakesStacks[pancakesStackIndex] << " with mass " << normalPancakesSum[sortedPancakesStacks[pancakesStackIndex]][m-1] << endl;) while (consumedCount + m <= k) { if (pancakesStackIndex < n && shouldConsumeStack(sortedPancakesStacks[pancakesStackIndex], reversedPancakeIndex, m)) { int stackIndex = sortedPancakesStacks[pancakesStackIndex]; DEBUG(2, cerr << "consuming stack " << stackIndex << " with " << m << " pancakes and mass " << normalPancakesSum[stackIndex][m-1] << endl;) consumedMass += normalPancakesSum[stackIndex][m-1]; consumedCount += m; ++pancakesStackIndex; DEBUG(2, cerr << " new best stack is " << sortedPancakesStacks[pancakesStackIndex] << " with mass " << normalPancakesSum[sortedPancakesStacks[pancakesStackIndex]][m-1] << endl;) } else if (reversedPancakeIndex < reversedPancakesCount) { DEBUG(2, cerr << "consuming single reversed pancake " << reversedPancakeIndex << " with mass " << reversedPancakes[reversedPancakeIndex] << endl;) consumedMass += reversedPancakes[reversedPancakeIndex]; ++consumedCount; ++reversedPancakeIndex; } else { cerr << "SHOULD NEVER HAPPEN" << endl; //should never happen break; } } if (pancakesStackIndex == n) { // all stacks are consumed, consume reversed pancakes until k long long remainingMass = subSum(reversedPancakesSum, reversedPancakeIndex, reversedPancakeIndex + (k - consumedCount)); consumedMass += remainingMass; DEBUG(2, cerr << "consuming " << (k - consumedCount) << " reversed pancakes from " << reversedPancakeIndex << " with total mass " << remainingMass << endl;) DEBUG(2, cerr << "Result is " << consumedMass << endl;) cout << consumedMass; return 0; } //consume reversed pancakes one by one, until single stack becomes larger (which ends the flow) for (int remainingK = k - consumedCount; remainingK > 0; --remainingK) { DEBUG(2, cerr << "Remaining pancakes: " << remainingK << endl;) //it must always exist, because consuming a stack will end the flow int bestStackIndex = *std::max_element(sortedPancakesStacks + pancakesStackIndex, sortedPancakesStacks + n, [&](int a, int b) { return normalPancakesSum[a][remainingK-1] < normalPancakesSum[b][remainingK-1]; }); DEBUG(0, cerr << " range is (" << pancakesStackIndex << ", " << n << "), best is " << bestStackIndex << endl;) long long bestStackMass = normalPancakesSum[bestStackIndex][remainingK-1]; long long reversedPancakesMass = subSum(reversedPancakesSum, reversedPancakeIndex, min(reversedPancakeIndex + remainingK, reversedPancakesCount)); DEBUG(1, cerr << " Best stack for remaining " << remainingK << " pancakes is " << bestStackIndex << " with mass " << bestStackMass << endl;) DEBUG(1, cerr << " Reversed pancakes for remaining " << remainingK << " pancakes is " << reversedPancakesMass << endl;) if (reversedPancakesMass >= bestStackMass) { // consume reversed pancake DEBUG(2, cerr << " Consuming single reversed pancake " << reversedPancakeIndex << " with mass " << reversedPancakes[reversedPancakeIndex] << endl;) consumedMass += reversedPancakes[reversedPancakeIndex]; ++consumedCount; ++reversedPancakeIndex; } else if (reversedPancakes[reversedPancakeIndex] >= normalPancakes[bestStackIndex][remainingK-1]) { // consume reversed pancake DEBUG(2, cerr << " Consuming single reversed pancake " << reversedPancakeIndex << " with mass " << reversedPancakes[reversedPancakeIndex] << " even though stack is greater" << endl;) consumedMass += reversedPancakes[reversedPancakeIndex]; ++consumedCount; ++reversedPancakeIndex; } else { // consume stack DEBUG(2, cerr << " Consuming stack " << bestStackIndex << " with mass " << bestStackMass << endl;) consumedMass += bestStackMass; consumedCount += remainingK; break; } } DEBUG(2, cerr << "Result is " << consumedMass << endl;) cout << consumedMass; return 0; } |
English