CodeChef LogoCodeChef Logo
Courses

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

PracticeCompeteCompiler
Upgrade to Pro
Courses

Programming and DSA

Learn to think like a programmer. Develop your problem-solving skills with essential data structures and algorithms.

Career Paths

From beginner to job-ready. Explore our curated career paths designed to help you succeed in the tech industry.

Other Courses

PracticeCompeteCompiler
Home  »  START181D  »  GOODMATRIX  »  Submissions  »  1151001767

The Best Matrix

Status:

Correct Answer

Submission by:

2
leal_al

Submitted:

2 months ago

Problem:

GOODMATRIX

Contest:

START181D
Language: C++
#ifndef INCLUDED_MAIN
#define INCLUDED_MAIN
#include __FILE__
int main() {
ll t; cin>>t;
while(t--) {
solve();
}
}
/////// library zone ///////
#else
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define srep(i,l,r) for(ll i=l;i<=r;i++)
using ll = long long;
const ll mod=998244353;
#define vout(v) for(auto i :v) cout<<i<<" ";
#define INF 9223300000000000000ll
#define Winf 5e12
#define nl "\n"
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define vl vector<ll>
void solve() {
ll n,m; cin>>n>>m;
vector<vl> mat(n,vl(m));
rep(i,n) rep(j,m) cin>>mat[i][j];
ll freqmax=-10000000;
for(auto b:{-1,1}) {
for(auto c:{-1,1}) {
ll curmax=0;
map<ll,ll> a;
rep(i,n) {
rep(j,m) {
a[mat[i][j]-b*(i+1)-c*(j+1)]++;
if(a[mat[i][j]-b*(i+1)-c*(j+1)]>=curmax) curmax=a[mat[i][j]-b*(i+1)-c*(j+1)];
}
}
freqmax=max(freqmax,curmax);
}
}
cout<<n*m-freqmax<<nl;
return;
}
string sp(string &s) { //manacher;
string t="*#";
rep(i,(ll)s.size()) {
t+=s[i];
t+="#";
}
t+="~";
ll n=t.size();
vector<ll> rad(n);
int i=0,j=0;
while(i<n) {
while(i-j>=0 && i+j<n && t[i-j]==t[i+j]) ++j;
rad[i]=j;
int k=1;
while(i-k>=0 && k+rad[i-k]<j) rad[i+k]=rad[i-k],++k;
i+=k; j-=k;
}
rep(i,n) rad[i]--;
int besti;
rep(i,n) {
if(i+rad[i]==n-2) {
besti=i;break;
}
if(i==n-1) return "owari";
}
string add;
ll truerad=(rad[besti]+1)/2;
ll truei=(besti-rad[besti]+1-2)/2;
add=s.substr(0,truei);
reverse(all(add));
return s+add;
}
vector<ll> pfact(ll n) {
vector<ll> resp;
vector<bool> prefact(sqrtl(n)+10);
for(ll i=2;i*i<=n;i++) {
while(n%i==0) {
n/=i;
if(!prefact[i]) resp.push_back(i);
prefact[i]=true;
}
}
if(n!=1) resp.push_back(n);
return resp;
}
ll vecmax(vector<ll> &a) {
ll ans=-1*INF;
rep(i,(ll)a.size()) {
ans=max(ans,a[i]);
}
return ans;
}
ll vecmin(vector<ll> &a) {
ll ans=INF;
rep(i,(ll)a.size()) {
ans=min(ans,a[i]);
}
return ans;
}
ll safemod(ll num,ll rule) {
return (num%rule+rule)%rule;
}
ll modinv(ll a, ll mod) { //Euclidmod, a*u+mod*v=1
ll b=mod,u=1,v=0;
while (b) {
ll t=a/b;
a-=t*b;
swap(a,b);
u-=t*v;
swap(u,v);
}
u%=mod;
if (u < 0) u+=mod;
return u;
}
vector<ll> Erasieve(ll n) {
vector<bool> is_prime(n+1, true);
is_prime[0] = is_prime[1] = false;
for (int i=2;i*i<=n;++i) {
if (is_prime[i]) {
for (int j=i*i;j<=n;j+=i) is_prime[j] = false;
}
}
vector<ll> primes;
srep(i,2,n) {
if (is_prime[i]) primes.push_back(i);
}
return primes;
}
ll divcount(ll n) {
ll ans=0;
for(int i=1;i*i<=n;i++) {
if(n%i==0) ans+=(i*i==n ? 1:2);
}
return ans;
}
struct UnionFind { //Potentialized
vector<int> par;
vector<int> rank;//size
UnionFind(int N) : par(N), rank(N,0) {
rep(i,N) {
par[i] = i;
}
}
//
int root(int x) {
if (par[x]==x) return x;
return par[x]=root(par[x]);
}
void unite(int x,int y) {
int rx=root(x);
int ry=root(y);
if (rx==ry) return;
if (rank[rx]<rank[ry]) {
par[rx]=ry;
}
else {
par[ry]=rx;
if (rank[rx]==rank[ry]) {
rank[rx]++;
}
}
}
bool same(int x,int y) {
return root(x)==root(y);
}
};
struct XORPotentialUF {// 0-indexed!!...ABEL......
vector<int> par;
vector<int> wgt; // XOR
vector<int> rank; //
XORPotentialUF(int size) : par(size),wgt(size,0),rank(size,0) {
rep(i,size) par[i]=i;
}
pair<int, int> find(int u) {
if (par[u]!=u) {
int orig_par=par[u];
auto [root,diff]=find(orig_par);
par[u]=root;
wgt[u]^=diff;
return {root,wgt[u]};
}
return {u,0};
}
bool limitunite(int u,int v,int z) {
auto [pu,ru]=find(u);
auto [pv,rv]=find(v);
if (pu==pv) {
return (ru^rv)==z;
}
if (rank[pu]<rank[pv]) {
swap(pu,pv);
swap(ru,rv);
}
par[pv]=pu;
wgt[pv]=ru^rv^z;
if (rank[pu]==rank[pv]) rank[pu]++;
return true;
}
int weight(int vertex) {
find(vertex);
return wgt[vertex];
}
int diff_ytox(int x,int y) {//使
return weight(x)^weight(y);
}
};
template<class ABEL> struct ADDPotentialUF { //0-indexed!! reference: https://qiita.com/drken/items/cce6fc5c579051e64fab super
vector<int> par;
vector<int> rank;
vector<ABEL> wgt;
ADDPotentialUF(int size) : par(size),wgt(size,0),rank(size,0) {
rep(i,size) par[i]=i;
}
pair<int, ABEL> find(int x) {
if (par[x]==x) {
return {x,0};
} else {
auto [r,w]=find(par[x]);
par[x]=r;
wgt[x]+=w;
return {r,wgt[x]};
}
}
ABEL weight(int x) {
find(x);
return wgt[x];
}
bool issame(int x,int y) {
return find(x).first==find(y).first;
}
bool unite(int x,int y,ABEL w) {
w+=weight(x); w-=weight(y);
x=find(x).first; y=find(y).first;
if (x==y) {
return (diff_ytox(x,y)==w);
}
if (rank[x]<rank[y]) swap(x,y), w*=-1;
if (rank[x]==rank[y]) ++rank[x];
par[y]=x;
wgt[y]=w;
return true;
}
ABEL diff_ytox(int x,int y) { //
return weight(y)-weight(x);
}
};
template <class T> void vecpr(T first,T last) {
ll tmp=0;
for(auto it=first;it!=last;++it) {
if(tmp) cout<<" ";
cout<<*it;
tmp++;
}
cout<<endl;
}
template <class T> void vvpr(vector<vector<T>> a) {
for(int i=0;i<(ll)a.size();i++) {
for(int j=0;j<(ll)a[i].size();j++) {
if(j) cout<<" ";
cout<<a[i][j];
}
cout<<endl;
}
}
vector<ll> slmax(vector<ll> a, ll limit, ll breadth) {//slidewindowbreadth,limit=a.size()
deque<ll> deq;
vector<ll> max_numbers(limit, (-1)*INF);
rep(i, limit) {
while (!deq.empty() && deq.front() <= i - breadth) deq.pop_front();
while (!deq.empty() && a[deq.back()] <= a[i]) deq.pop_back();
deq.push_back(i);
max_numbers[i] = a[deq.front()];
}
return max_numbers;
}
vector<ll> slmin(vector<ll> a, ll limit, ll breadth) {//slidewindowbreadth,limit=a.size()
deque<ll> deq;
vector<ll> min_numbers(limit, INF);
rep(i, limit) {
while (!deq.empty() && deq.front() <= i - breadth) deq.pop_front();
while (!deq.empty() && a[deq.back()] >= a[i]) deq.pop_back();
deq.push_back(i);
min_numbers[i] = a[deq.front()];
}
return min_numbers;
}
ll modpow(ll fl, ll po, ll mode) { // mode: 0=mod, 1=mod
ll ret=1;
if (mode) {
while (po>0) {
if (po&1) ret=(ret*fl)%mod;
fl=(fl*fl)%mod;
po>>=1;
}
} else {
while (po>0) {
if(po&1) ret*=fl;
fl*=fl;
po>>=1;
}
}
return ret;
}
ll gcd(ll a,ll b) { //Euclid
if(a<b) return gcd(b,a);
if(a%b==0) return b;
return gcd(b,a%b);
}
vector<ll> lis_min(vector<ll> &a) {
ll n=(ll)a.size();
vector<ll> dp(n,INF);
rep(i,n){
auto itr = lower_bound(dp.begin(),dp.end(), a[i]);
*itr = a[i];
}
return dp;
}//使
vector<ll> lis_size(vector<ll>& a) { // a indexdp[a];
ll n=a.size();
vector<ll> dp(n,1);
vector<ll> seq;
rep(i,n) {
auto it=lower_bound(seq.begin(), seq.end(), a[i]);
if (it==seq.end()) seq.push_back(a[i]);
else *it=a[i];
dp[i]=distance(seq.begin(), lower_bound(seq.begin(), seq.end(), a[i]))+1;
}
srep(i,1,n-1) {
if(dp[i]<dp[i-1]) dp[i]=dp[i-1];
}
return dp;
}
#endif
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Correct Answer
Submission ID: 1151001767
Score:100
Memory:4.2M
Sub-Task Task # Result
(time)
10Correct
(0.00)
11Correct
(0.00)
12Correct
(0.00)
13Correct
(0.00)
14Correct
(0.01)
15Correct
(0.00)
16Correct
(0.00)
17Correct
(0.01)
18Correct
(0.01)
19Correct
(0.00)
110Correct
(0.00)
Subtask Score: 100% Result - Correct
Total Score = 100%