#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("sse4,abm,avx,popcnt")
#include<bits/stdc++.h>
#define int long long
#define quick ios::sync_with_stdio(0);cin.tie(0);
#define rep(x,a,b) for(int x=a;x<=b;x++)
#define repd(x,a,b) for(int x=a;x>=b;x--)
#define F first
#define S second
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
#define lowbit(x) (x&-x)
using namespace std;
typedef pair<int,int> pii;
void debug(){
cout<<"\n";
}
template<class T,class ...U>
void debug(T a,U ...b){
cout<<a<<" ",debug(b...);
}
const int N=5e2+7;
const int INF=1e18L;
int c[N][N];
inline int q(int x1,int x2,int y1,int y2){
return c[x2][y2]-c[x1-1][y2]-c[x2][y1-1]+c[x1-1][y1-1];
}
signed main(){
quick
int r,s,a,b;
cin>>r>>s>>a>>b;
rep(i,1,r){
rep(j,1,s){
cin>>c[i][j];
c[i][j]+=c[i-1][j]+c[i][j-1]-c[i-1][j-1];
}
}
int ans=INF;
rep(x1,1,r){
rep(x2,x1,r){
int l=1;
rep(y2,1,s){
int S=q(x1,x2,l,y2);
ans=min(ans,abs(S-a)+abs(S-b));
while(S>b){
S=q(x1,x2,++l,y2);
ans=min(ans,abs(S-a)+abs(S-b));
}
}
}
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
860 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
4 ms |
832 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
4 ms |
860 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
724 KB |
Output is correct |
16 |
Correct |
4 ms |
832 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
860 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
286 ms |
2536 KB |
Output is correct |
24 |
Correct |
279 ms |
4840 KB |
Output is correct |
25 |
Correct |
316 ms |
4840 KB |
Output is correct |
26 |
Correct |
270 ms |
4696 KB |
Output is correct |
27 |
Correct |
179 ms |
4016 KB |
Output is correct |
28 |
Correct |
284 ms |
4836 KB |
Output is correct |
29 |
Correct |
299 ms |
4836 KB |
Output is correct |
30 |
Correct |
274 ms |
4836 KB |
Output is correct |
31 |
Correct |
41 ms |
2392 KB |
Output is correct |
32 |
Correct |
279 ms |
4836 KB |
Output is correct |
33 |
Correct |
274 ms |
4840 KB |
Output is correct |
34 |
Correct |
271 ms |
4836 KB |
Output is correct |
35 |
Correct |
295 ms |
4844 KB |
Output is correct |
36 |
Correct |
273 ms |
4944 KB |
Output is correct |
37 |
Correct |
271 ms |
4700 KB |
Output is correct |
38 |
Correct |
281 ms |
4700 KB |
Output is correct |
39 |
Correct |
282 ms |
4688 KB |
Output is correct |
40 |
Correct |
279 ms |
4952 KB |
Output is correct |
41 |
Correct |
154 ms |
3580 KB |
Output is correct |
42 |
Correct |
157 ms |
3416 KB |
Output is correct |
43 |
Correct |
160 ms |
3584 KB |
Output is correct |
44 |
Correct |
174 ms |
3580 KB |
Output is correct |