/*
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;
}
}
if(r>=i)s2=sum(i,x,r,y);
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;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
604 KB |
Output is correct |
11 |
Correct |
10 ms |
940 KB |
Output is correct |
12 |
Correct |
10 ms |
940 KB |
Output is correct |
13 |
Correct |
10 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
10 ms |
860 KB |
Output is correct |
16 |
Correct |
10 ms |
856 KB |
Output is correct |
17 |
Correct |
10 ms |
944 KB |
Output is correct |
18 |
Correct |
11 ms |
860 KB |
Output is correct |
19 |
Correct |
11 ms |
944 KB |
Output is correct |
20 |
Correct |
11 ms |
860 KB |
Output is correct |
21 |
Correct |
11 ms |
940 KB |
Output is correct |
22 |
Correct |
13 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
604 KB |
Output is correct |
11 |
Correct |
10 ms |
940 KB |
Output is correct |
12 |
Correct |
10 ms |
940 KB |
Output is correct |
13 |
Correct |
10 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
10 ms |
860 KB |
Output is correct |
16 |
Correct |
10 ms |
856 KB |
Output is correct |
17 |
Correct |
10 ms |
944 KB |
Output is correct |
18 |
Correct |
11 ms |
860 KB |
Output is correct |
19 |
Correct |
11 ms |
944 KB |
Output is correct |
20 |
Correct |
11 ms |
860 KB |
Output is correct |
21 |
Correct |
11 ms |
940 KB |
Output is correct |
22 |
Correct |
13 ms |
1116 KB |
Output is correct |
23 |
Correct |
1541 ms |
4432 KB |
Output is correct |
24 |
Correct |
1547 ms |
6992 KB |
Output is correct |
25 |
Correct |
1587 ms |
6820 KB |
Output is correct |
26 |
Correct |
1532 ms |
6820 KB |
Output is correct |
27 |
Correct |
691 ms |
5332 KB |
Output is correct |
28 |
Correct |
1601 ms |
6824 KB |
Output is correct |
29 |
Correct |
1605 ms |
6820 KB |
Output is correct |
30 |
Correct |
1532 ms |
6928 KB |
Output is correct |
31 |
Correct |
41 ms |
2648 KB |
Output is correct |
32 |
Correct |
1565 ms |
6824 KB |
Output is correct |
33 |
Correct |
1521 ms |
6824 KB |
Output is correct |
34 |
Correct |
1572 ms |
6824 KB |
Output is correct |
35 |
Correct |
1497 ms |
6880 KB |
Output is correct |
36 |
Correct |
1538 ms |
6828 KB |
Output is correct |
37 |
Correct |
1543 ms |
6996 KB |
Output is correct |
38 |
Correct |
1551 ms |
6992 KB |
Output is correct |
39 |
Correct |
1579 ms |
6820 KB |
Output is correct |
40 |
Correct |
1542 ms |
6824 KB |
Output is correct |
41 |
Correct |
1656 ms |
5456 KB |
Output is correct |
42 |
Correct |
1651 ms |
5624 KB |
Output is correct |
43 |
Correct |
1651 ms |
5716 KB |
Output is correct |
44 |
Correct |
1652 ms |
5560 KB |
Output is correct |