#include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=2e5+5, INF=1e9+5, mod=1e9+7; vector<vector<ll> > licz(vector<vector<ll> > V){ int n=V.size(), m=V[0].size(); vector<vector<ll>> ans(n, vector<ll>(n)); for(int s=0; s<n; s++){ vector<vector<ll> > V2(n, vector<ll>(m, 1e18)); V2[s][0]=V[s][0]; for(int i=s; i<n; i++){ for(int j=1; j<m; j++){ if(i==s)V2[i][j]=V2[i][j-1]+V[i][j]; else{ V2[i][j]=min(V2[i][j-1], V2[i-1][j]-V[i-1][j])+V[i][j]; } } ans[s][i]=V2[i][m-1]; } //deb(V2); } //deb(ans); return ans; } pair<int, vector<vector<ll> > > prob(vector<vector<ll>> ans, int qq){ int n=ans.size(); vector<vector<ll> > V2(n, vector<ll>(n)); ll M=100000000; for(int i=0; i<n; i++){ for(int j=i; j<n; j++){ V2[j][n-1-i]=ans[i][j]; if(i)V2[j][n-i-1]-=ans[i-1][j]; } } for(int i=0; i<n; i++){ ll t=V2[n-1][n-1-i]; for(int j=i; j<n; j++){ V2[j][n-1-i]-=t; V2[j][0]+=t; } } /*for(vi v:V2){ for(int i:v){ cout<<i<<" "; } cout<<"\n"; } cout<<"\n";*/ vi usun(n); for(int i=1; i<n-1; i++){ usun[i]=1; for(int j=0; j<n; j++){ if(V2[j][i])usun[i]=0; } } for(int i=0; i<n-1; i++){ V2[i][0]+=(n-i)*M; for(int j=n-i-2; j>=0; j--){ if(!usun[j]){ V2[i][j]-=(n-i)*M; break; } } } int res=n; for(int i:usun)res-=i; if(qq==0)return {res, {}}; else{ vector<vector<ll> > V3(n); for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(!usun[j]){ V3[i].pb(V2[i][j]); } } } V2=V3; return {res, V2}; } } int main(){ //BOOST; int n, m, qq; cin>>n>>m>>qq; vector<vector<ll>> V(n, vector<ll>(m)); for(auto &v:V){ for(ll &i:v)cin>>i; } auto ans=licz(V); auto a=prob(ans, qq); for(int i=0; i<n; i++){ for(int j=0; i+j<n; j++){ swap(ans[i][j], ans[n-1-j][n-1-i]); } } auto b=prob(ans, qq); for(int i=0; i<n; i++){ for(int j=0; i+j<n; j++){ swap(ans[i][j], ans[n-1-j][n-1-i]); } } int res=min(a.st, b.st); //deb(b.nd); for(int i=0; i<b.nd.size(); i++){ reverse(all(b.nd[i])); } if(b.nd.size()){ for(int i=0; i<b.nd[0].size(); i++){ for(int j=0; j+j<b.nd.size(); j++){ swap(b.nd[j][i], b.nd[b.nd.size()-1-j][i]); } } } //reverse(all(b.nd)); vector<vector<ll> > V3; if(b.st<=a.st)V3=b.nd; else V3=a.nd; deb(res); if(res>m){ res=m; V3=V; } if(qq==0){ cout<<res<<"\n"; } else{ if(qq==1){ cout<<res<<"\n"; for(vector<ll> v:V3){ for(ll i:v){ cout<<i<<" "; } cout<<"\n"; } } else{ //deb(V2); auto ans2=licz(V3); //assert(ans==ans2); for(vector<ll> v:ans2){ for(ll i:v){ cout<<i<<" "; } cout<<"\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 | #include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=2e5+5, INF=1e9+5, mod=1e9+7; vector<vector<ll> > licz(vector<vector<ll> > V){ int n=V.size(), m=V[0].size(); vector<vector<ll>> ans(n, vector<ll>(n)); for(int s=0; s<n; s++){ vector<vector<ll> > V2(n, vector<ll>(m, 1e18)); V2[s][0]=V[s][0]; for(int i=s; i<n; i++){ for(int j=1; j<m; j++){ if(i==s)V2[i][j]=V2[i][j-1]+V[i][j]; else{ V2[i][j]=min(V2[i][j-1], V2[i-1][j]-V[i-1][j])+V[i][j]; } } ans[s][i]=V2[i][m-1]; } //deb(V2); } //deb(ans); return ans; } pair<int, vector<vector<ll> > > prob(vector<vector<ll>> ans, int qq){ int n=ans.size(); vector<vector<ll> > V2(n, vector<ll>(n)); ll M=100000000; for(int i=0; i<n; i++){ for(int j=i; j<n; j++){ V2[j][n-1-i]=ans[i][j]; if(i)V2[j][n-i-1]-=ans[i-1][j]; } } for(int i=0; i<n; i++){ ll t=V2[n-1][n-1-i]; for(int j=i; j<n; j++){ V2[j][n-1-i]-=t; V2[j][0]+=t; } } /*for(vi v:V2){ for(int i:v){ cout<<i<<" "; } cout<<"\n"; } cout<<"\n";*/ vi usun(n); for(int i=1; i<n-1; i++){ usun[i]=1; for(int j=0; j<n; j++){ if(V2[j][i])usun[i]=0; } } for(int i=0; i<n-1; i++){ V2[i][0]+=(n-i)*M; for(int j=n-i-2; j>=0; j--){ if(!usun[j]){ V2[i][j]-=(n-i)*M; break; } } } int res=n; for(int i:usun)res-=i; if(qq==0)return {res, {}}; else{ vector<vector<ll> > V3(n); for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(!usun[j]){ V3[i].pb(V2[i][j]); } } } V2=V3; return {res, V2}; } } int main(){ //BOOST; int n, m, qq; cin>>n>>m>>qq; vector<vector<ll>> V(n, vector<ll>(m)); for(auto &v:V){ for(ll &i:v)cin>>i; } auto ans=licz(V); auto a=prob(ans, qq); for(int i=0; i<n; i++){ for(int j=0; i+j<n; j++){ swap(ans[i][j], ans[n-1-j][n-1-i]); } } auto b=prob(ans, qq); for(int i=0; i<n; i++){ for(int j=0; i+j<n; j++){ swap(ans[i][j], ans[n-1-j][n-1-i]); } } int res=min(a.st, b.st); //deb(b.nd); for(int i=0; i<b.nd.size(); i++){ reverse(all(b.nd[i])); } if(b.nd.size()){ for(int i=0; i<b.nd[0].size(); i++){ for(int j=0; j+j<b.nd.size(); j++){ swap(b.nd[j][i], b.nd[b.nd.size()-1-j][i]); } } } //reverse(all(b.nd)); vector<vector<ll> > V3; if(b.st<=a.st)V3=b.nd; else V3=a.nd; deb(res); if(res>m){ res=m; V3=V; } if(qq==0){ cout<<res<<"\n"; } else{ if(qq==1){ cout<<res<<"\n"; for(vector<ll> v:V3){ for(ll i:v){ cout<<i<<" "; } cout<<"\n"; } } else{ //deb(V2); auto ans2=licz(V3); //assert(ans==ans2); for(vector<ll> v:ans2){ for(ll i:v){ cout<<i<<" "; } cout<<"\n"; } } } } |