This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int n,m,a[2005][2005],b[2005][2005],st,md,ed,ans1,ans2,num,dp[2005][2005];
int cnt;
vector<int> v;
vector<pair<int,int> > p;
int find_ans()
{
st=0;
ed=p.size()-1;
while(ed>=st)
{
md=(st+ed)/2;
if(md==p.size()-1)
{
num=0;
}else
{
num=p[md+1].second;
}
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)dp[i][j]=-1e9;
dp[1][1]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(i>1)dp[i][j]=max(dp[i][j],dp[i-1][j]+(abs(a[i-1][j]-a[i][j])>p[md].first));
if(j>1)dp[i][j]=max(dp[i][j],dp[i][j-1]+(abs(a[i][j-1]-a[i][j])>p[md].first));
}
}
if(dp[n][m]==num)
{
ed=md-1;
}else
{
st=md+1;
}
}
//printf("%d %d %d\n",st,md,ed);
return p[st].first;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<=m;j++)
{
v.push_back(abs(a[i][j]-a[i+1][j]));
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<m;j++)
{
v.push_back(abs(a[i][j]-a[i][j+1]));
}
}
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
{
if(i==0)
{
cnt=1;
continue;
}
if(v[i]==v[i-1])
{
++cnt;
}else
{
p.push_back({v[i-1],cnt});
cnt=1;
}
}
p.push_back({v[v.size()-1],cnt});
for(int i=p.size()-2;i>=0;i--)
{
p[i].second=p[i].second+p[i+1].second;
}
/*for(int i=0;i<p.size();i++)
{
printf("%d %d\n",p[i].first,p[i].second);
}*/
ans1=find_ans();
for(int i=1;i<=m;i++)
{
for(int j=n;j>=1;j--)
{
b[i][n-j+1]=a[j][i];
}
}
swap(n,m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
a[i][j]=b[i][j];
}
}
ans2=find_ans();
printf("%d\n",min(ans1,ans2));
}
Compilation message (stderr)
joioi.cpp: In function 'int find_ans()':
joioi.cpp:14:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | if(md==p.size()-1)
| ~~^~~~~~~~~~~~
joioi.cpp: In function 'int main()':
joioi.cpp:67:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int i=0;i<v.size();i++)
| ~^~~~~~~~~
joioi.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
joioi.cpp:49:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d",&a[i][j]);
| ~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |