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