이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define inf 1e18
#define int long long
const int mod=1e9+7;
const int N=501;
int mat[N][N], dp[N][N];
inline int toplam(int x1,int y1,int x2,int y2){
return dp[x2][y2]-dp[x1-1][y2]-dp[x2][y1-1]+dp[x1-1][y1-1];
}
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
int x,y,a,b;
cin>>x>>y>>a>>b;
if(a>b)
swap(a,b);
for(int i=1;i<=x;i++)
for(int j=1;j<=y;j++){
cin>>mat[i][j];
dp[i][j]=mat[i][j]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
}
int ans=inf;
for(int i=1;i<=x;i++)
for(int j=1;j<=x;j++){
int l=1,r=1;
while(r<=y){
int suan=toplam(i,l,j,r);
ans=min(ans,abs(a-suan)+abs(b-suan));
if(suan>b){
l++;
if(l>r)
r++;
}
else if(suan>=a){
cout<<b-a<<"\n";
return 0;
}
else
r++;
}
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |