#include <bits/stdc++.h>
using namespace std;
struct pancake{
long long val;
int stos;
int ix;
pancake(long long val_, int stos_, int ix_){
this->val=val_;
this->stos=stos_;
this->ix=ix_;
}
};
long long INF = 1e17;
bool operator<(pancake a,pancake b){
return (a.val<b.val);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,m,k;
cin>>n>>m>>k;
vector<vector<long long>>in; //input pancackes
vector<int>last(n,-1); //indeks ostatniego z danego stosu
for(int i=0;i<n;i++){
vector<long long>wyp;
in.push_back(wyp);
for(int j=0;j<m;j++){
long long a;
cin>>a;
in[i].push_back(a);
}
}
set<pancake>s;//wszystkie ostatnie naleśniki
int nr=0;
for(int i=0;i<n;i++){
int j=0;
while(j<m){
while(nr<k&&j<m){
nr++;
j++;
last[i]++;
}
if(nr==k&&s.empty()==false){ //teraz porównujemy
pancake it=*s.begin();
while(j<k&&j<m&&it.val<=in[i][j]){
last[it.stos]--;
if(it.ix>0){
pancake p8(in[it.stos][it.ix-1],it.stos,it.ix-1);
s.erase(it);
s.insert(p8);
} //mamy usunięty najgorszy naleśnik który był do usunięcia
else{
s.erase(it);
}
it=*s.begin();
last[i]=j;
j++;
/*for(auto it=s.begin();it!=s.end();it++){
cout<<(*it).stos<<" "<<(*it).ix<<" "<<(*it).val<<endl;
}
cout<<endl;*/
}
stack<pancake>pot;//potencjalnie do usunięcia
long long sum_pot=0; //ich suma
long long zysk=0; //zysk w zamian
while(s.empty()==false&&j<m&&j<k){
it=*s.begin();
last[it.stos]--;
if(it.ix>0){
pancake p8(in[it.stos][it.ix-1],it.stos,it.ix-1);
pot.push(it);
sum_pot+=it.val;
s.erase(it);
s.insert(p8);
}
else{
pot.push(it);
sum_pot+=it.val;
s.erase(it);
}
zysk+=in[i][j];
if(zysk>=sum_pot){
while(pot.empty()==false){
pot.pop();
}
last[i]=j;
}
j++;
}
while(pot.empty()==false){
pancake p7 = pot.top();
pot.pop();
if(p7.ix>0){
pancake p6(in[p7.stos][p7.ix-1],p7.stos,p7.ix-1);
s.erase(p6);
s.insert(p7);
last[p7.stos]=p7.ix;
}
else{
s.insert(p7);
last[p7.stos]=p7.ix;
}
}
}
j++;
}
if(last[i]!=-1){
pancake p5(in[i][last[i]],i,last[i]);
if(s.find(p5)==s.end()){
s.insert(p5);
}
}
/*for(auto it=s.begin();it!=s.end();it++){
cout<<(*it).stos<<" "<<(*it).ix<<" "<<(*it).val<<endl;
}
cout<<endl;*/
}
set<pancake>final; //FINAL PART
for(int i=0;i<n;i++){
if(last[i]>=0){
final.insert(pancake(in[i][last[i]],i,last[i]));
}
}
for(int i=0;i<n;i++){
pancake it=*final.begin();
if(it.stos==i){
auto it1=final.begin();
it1++;
if(it1!=final.end()){
it=*it1;
}
}
while(it.stos!=i&&last[i]!=m-1&&in[i][last[i]+1]>it.val){
last[it.stos]--;
if(it.ix>0){
final.insert(pancake(in[it.stos][it.ix-1],it.stos,it.ix-1));
}
final.erase(it);
last[i]++;
final.insert(pancake(in[i][last[i]],i,last[i]));
it=*final.begin();
if(it.stos==i){
auto it1=final.begin();
it1++;
if(it1!=final.end()){
it=*it1;
}
}
}
/*vector<long long>pref_sumy;
for(int j=last[i];j>=0;j--){
if(pref_sumy.size()==0){
pref_sumy.push_back(in[i][j]);
}
else{
pref_sumy.push_back(in[i][j]+pref_sumy.back());
}
}
queue<pancake>pot;
vector<long long>pot_sumy;
while(s.empty()==false&&pot_sumy.size()<pref_sumy.size()){
pancake it=*s.begin();
if(it.stos==i){
auto it1=final.begin();
it1++;
if(it1!=final.end()){
it=*it1;
}
}
if(it.stos!=i){
if(pot_sumy.empty()){
pot_sumy.push_back(it.val);
}
else{
pot_sumy.push_back(it.val+pot_sumy.back());
}
pot.push(it);
last[it.stos]--;
if(it.ix>0){
s.insert(pancake(in[it.stos][it.ix-1],it.stos,it.ix-1));
}
s.erase(it);
}
else{
pot_sumy.push_back(INF);
}
}
while(pot_sumy.size()<pref_sumy.size()){
pot_sumy.push_back(INF);
}
pair<long long,long long>sz;//szukana para <maksymalna różnica, indeks>
sz.first=0;
sz.second=0;
for(int j=0;j<pref_sumy.size();j++){
long long roz=pref_sumy[j]-pot_sumy[j];
if(roz>sz.first){
sz.first=roz;
sz.second=j+1;
}
}
while(pot.size()>sz.second){
if(pot.front().ix>0){
s.erase(pancake(in[pot.front().stos][pot.front().ix-1],pot.front().stos,pot.front().ix-1));
}
s.insert(pot.front());
last[pot.front().stos]=pot.front().ix;
//cout<<pot.front().stos<<" "<<pot.front().ix<<" "<<pot.front().val<<endl;
pot.pop();
}
last[i]+=sz.second;
/*for(auto it=s.begin();it!=s.end();it++){
cout<<(*it).stos<<" "<<(*it).ix<<" "<<(*it).val<<endl;
}
cout<<endl;*/
}
long long sum=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(j<=last[i]){
sum+=in[i][j];
}
}
//cout<<i<<" "<<last[i]<<endl;
}
cout<<sum<<"\n";
}
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 | #include <bits/stdc++.h> using namespace std; struct pancake{ long long val; int stos; int ix; pancake(long long val_, int stos_, int ix_){ this->val=val_; this->stos=stos_; this->ix=ix_; } }; long long INF = 1e17; bool operator<(pancake a,pancake b){ return (a.val<b.val); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,m,k; cin>>n>>m>>k; vector<vector<long long>>in; //input pancackes vector<int>last(n,-1); //indeks ostatniego z danego stosu for(int i=0;i<n;i++){ vector<long long>wyp; in.push_back(wyp); for(int j=0;j<m;j++){ long long a; cin>>a; in[i].push_back(a); } } set<pancake>s;//wszystkie ostatnie naleśniki int nr=0; for(int i=0;i<n;i++){ int j=0; while(j<m){ while(nr<k&&j<m){ nr++; j++; last[i]++; } if(nr==k&&s.empty()==false){ //teraz porównujemy pancake it=*s.begin(); while(j<k&&j<m&&it.val<=in[i][j]){ last[it.stos]--; if(it.ix>0){ pancake p8(in[it.stos][it.ix-1],it.stos,it.ix-1); s.erase(it); s.insert(p8); } //mamy usunięty najgorszy naleśnik który był do usunięcia else{ s.erase(it); } it=*s.begin(); last[i]=j; j++; /*for(auto it=s.begin();it!=s.end();it++){ cout<<(*it).stos<<" "<<(*it).ix<<" "<<(*it).val<<endl; } cout<<endl;*/ } stack<pancake>pot;//potencjalnie do usunięcia long long sum_pot=0; //ich suma long long zysk=0; //zysk w zamian while(s.empty()==false&&j<m&&j<k){ it=*s.begin(); last[it.stos]--; if(it.ix>0){ pancake p8(in[it.stos][it.ix-1],it.stos,it.ix-1); pot.push(it); sum_pot+=it.val; s.erase(it); s.insert(p8); } else{ pot.push(it); sum_pot+=it.val; s.erase(it); } zysk+=in[i][j]; if(zysk>=sum_pot){ while(pot.empty()==false){ pot.pop(); } last[i]=j; } j++; } while(pot.empty()==false){ pancake p7 = pot.top(); pot.pop(); if(p7.ix>0){ pancake p6(in[p7.stos][p7.ix-1],p7.stos,p7.ix-1); s.erase(p6); s.insert(p7); last[p7.stos]=p7.ix; } else{ s.insert(p7); last[p7.stos]=p7.ix; } } } j++; } if(last[i]!=-1){ pancake p5(in[i][last[i]],i,last[i]); if(s.find(p5)==s.end()){ s.insert(p5); } } /*for(auto it=s.begin();it!=s.end();it++){ cout<<(*it).stos<<" "<<(*it).ix<<" "<<(*it).val<<endl; } cout<<endl;*/ } set<pancake>final; //FINAL PART for(int i=0;i<n;i++){ if(last[i]>=0){ final.insert(pancake(in[i][last[i]],i,last[i])); } } for(int i=0;i<n;i++){ pancake it=*final.begin(); if(it.stos==i){ auto it1=final.begin(); it1++; if(it1!=final.end()){ it=*it1; } } while(it.stos!=i&&last[i]!=m-1&&in[i][last[i]+1]>it.val){ last[it.stos]--; if(it.ix>0){ final.insert(pancake(in[it.stos][it.ix-1],it.stos,it.ix-1)); } final.erase(it); last[i]++; final.insert(pancake(in[i][last[i]],i,last[i])); it=*final.begin(); if(it.stos==i){ auto it1=final.begin(); it1++; if(it1!=final.end()){ it=*it1; } } } /*vector<long long>pref_sumy; for(int j=last[i];j>=0;j--){ if(pref_sumy.size()==0){ pref_sumy.push_back(in[i][j]); } else{ pref_sumy.push_back(in[i][j]+pref_sumy.back()); } } queue<pancake>pot; vector<long long>pot_sumy; while(s.empty()==false&&pot_sumy.size()<pref_sumy.size()){ pancake it=*s.begin(); if(it.stos==i){ auto it1=final.begin(); it1++; if(it1!=final.end()){ it=*it1; } } if(it.stos!=i){ if(pot_sumy.empty()){ pot_sumy.push_back(it.val); } else{ pot_sumy.push_back(it.val+pot_sumy.back()); } pot.push(it); last[it.stos]--; if(it.ix>0){ s.insert(pancake(in[it.stos][it.ix-1],it.stos,it.ix-1)); } s.erase(it); } else{ pot_sumy.push_back(INF); } } while(pot_sumy.size()<pref_sumy.size()){ pot_sumy.push_back(INF); } pair<long long,long long>sz;//szukana para <maksymalna różnica, indeks> sz.first=0; sz.second=0; for(int j=0;j<pref_sumy.size();j++){ long long roz=pref_sumy[j]-pot_sumy[j]; if(roz>sz.first){ sz.first=roz; sz.second=j+1; } } while(pot.size()>sz.second){ if(pot.front().ix>0){ s.erase(pancake(in[pot.front().stos][pot.front().ix-1],pot.front().stos,pot.front().ix-1)); } s.insert(pot.front()); last[pot.front().stos]=pot.front().ix; //cout<<pot.front().stos<<" "<<pot.front().ix<<" "<<pot.front().val<<endl; pot.pop(); } last[i]+=sz.second; /*for(auto it=s.begin();it!=s.end();it++){ cout<<(*it).stos<<" "<<(*it).ix<<" "<<(*it).val<<endl; } cout<<endl;*/ } long long sum=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(j<=last[i]){ sum+=in[i][j]; } } //cout<<i<<" "<<last[i]<<endl; } cout<<sum<<"\n"; } |
English