/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
cout<<(x?"YES":"NO")<<endl;
}
ll dp[505][505];
ll sum(int i,int j,int a,int b){
return dp[a][b]-dp[a][j-1]-dp[i-1][b]+dp[i-1][j-1];
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#ifndef ONLINE_JUDGE
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
fast
int n,m;
ll a,b;
cin>>n>>m>>a>>b;
if(a>b)swap(a,b);
vector<vector<ll>>arr(n+1,vector<ll>(m+1));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>arr[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+arr[i][j];
}
}
ll ans=4e18;
for(int x=1;x<=m;x++){
for(int y=x;y<=m;y++){
for(int i=1;i<=n;i++){
int l=i,r=n;
ll s1=0,s2=0;
while(l<=r){
int mid=l+r>>1;
if(sum(i,x,mid,y)>=a){
s1=sum(i,x,mid,y);
r=mid-1;
}
else{
l=mid+1;
}
}
l=i;r=n;
while(l<=r){
int mid=l+r>>1;
if(sum(i,x,mid,y)<a){
s2=sum(i,x,mid,y);
l=mid+1;
}
else{
r=mid-1;
}
}
ans=min(ans,abs(s1-a)+abs(s1-b));
ans=min(ans,abs(s2-a)+abs(s2-b));
}
}
}
cout<<ans<<endl;
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:73:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
73 | int mid=l+r>>1;
| ~^~
Main.cpp:84:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
84 | int mid=l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
16 ms |
1044 KB |
Output is correct |
12 |
Correct |
15 ms |
856 KB |
Output is correct |
13 |
Correct |
16 ms |
1048 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
16 ms |
860 KB |
Output is correct |
16 |
Correct |
16 ms |
968 KB |
Output is correct |
17 |
Correct |
22 ms |
860 KB |
Output is correct |
18 |
Correct |
18 ms |
848 KB |
Output is correct |
19 |
Correct |
22 ms |
860 KB |
Output is correct |
20 |
Correct |
18 ms |
860 KB |
Output is correct |
21 |
Correct |
18 ms |
992 KB |
Output is correct |
22 |
Correct |
17 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
604 KB |
Output is correct |
11 |
Correct |
16 ms |
1044 KB |
Output is correct |
12 |
Correct |
15 ms |
856 KB |
Output is correct |
13 |
Correct |
16 ms |
1048 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
16 ms |
860 KB |
Output is correct |
16 |
Correct |
16 ms |
968 KB |
Output is correct |
17 |
Correct |
22 ms |
860 KB |
Output is correct |
18 |
Correct |
18 ms |
848 KB |
Output is correct |
19 |
Correct |
22 ms |
860 KB |
Output is correct |
20 |
Correct |
18 ms |
860 KB |
Output is correct |
21 |
Correct |
18 ms |
992 KB |
Output is correct |
22 |
Correct |
17 ms |
860 KB |
Output is correct |
23 |
Execution timed out |
2064 ms |
6648 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |