#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
int c[501][501];
int sum(int r1,int c1,int r2,int c2){
return c[r2][c2]-c[r2][c1-1]-c[r1-1][c2]+c[r1-1][c1-1];
}
void solve(int tc) {
int r,s,a,b;
cin>>r>>s>>a>>b;
for(int i=0;i<=r;i++)for(int j=0;j<=s;j++)c[i][j]=0;
for(int i=1;i<=r;i++){
for(int j=1;j<=s;j++){
int x;
cin>>x;
c[i][j]=c[i-1][j]+c[i][j-1]-c[i-1][j-1]+x;
}
}
if(a>b)swap(a,b);
int ans=1e18;
for(int u=1;u<=r;u++){
for(int d=u;d<=r;d++){
int j=0;
for(int i=1;i<=s;i++){
j=max(j,i);
ans=min(ans,abs(a - sum(u,i,d,j)) + abs(b - sum(u,i,d,j)));
while(j+1<=s && abs(a-sum(u,i,d,j+1))+abs(b-sum(u,i,d,j))<abs(a-sum(u,i,d,j))+abs(b-sum(u,i,d,j))){
j++;
ans=min(ans,abs(a-sum(u,i,d,j))+abs(b-sum(u,i,d,j)));
}
}
}
}
cout<<ans<<"\n";
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
4 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
728 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
4 ms |
728 KB |
Output is correct |
18 |
Correct |
4 ms |
728 KB |
Output is correct |
19 |
Correct |
4 ms |
724 KB |
Output is correct |
20 |
Correct |
4 ms |
768 KB |
Output is correct |
21 |
Correct |
5 ms |
804 KB |
Output is correct |
22 |
Correct |
4 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
4 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
728 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
4 ms |
728 KB |
Output is correct |
18 |
Correct |
4 ms |
728 KB |
Output is correct |
19 |
Correct |
4 ms |
724 KB |
Output is correct |
20 |
Correct |
4 ms |
768 KB |
Output is correct |
21 |
Correct |
5 ms |
804 KB |
Output is correct |
22 |
Correct |
4 ms |
724 KB |
Output is correct |
23 |
Correct |
335 ms |
4696 KB |
Output is correct |
24 |
Correct |
329 ms |
4692 KB |
Output is correct |
25 |
Correct |
329 ms |
4812 KB |
Output is correct |
26 |
Correct |
320 ms |
4692 KB |
Output is correct |
27 |
Correct |
219 ms |
3920 KB |
Output is correct |
28 |
Correct |
330 ms |
4692 KB |
Output is correct |
29 |
Correct |
323 ms |
4692 KB |
Output is correct |
30 |
Correct |
315 ms |
4704 KB |
Output is correct |
31 |
Correct |
43 ms |
2392 KB |
Output is correct |
32 |
Correct |
326 ms |
4692 KB |
Output is correct |
33 |
Correct |
355 ms |
4704 KB |
Output is correct |
34 |
Correct |
322 ms |
4692 KB |
Output is correct |
35 |
Correct |
318 ms |
4692 KB |
Output is correct |
36 |
Correct |
316 ms |
4688 KB |
Output is correct |
37 |
Correct |
338 ms |
4692 KB |
Output is correct |
38 |
Correct |
326 ms |
4696 KB |
Output is correct |
39 |
Correct |
320 ms |
4812 KB |
Output is correct |
40 |
Correct |
333 ms |
4692 KB |
Output is correct |
41 |
Correct |
391 ms |
3428 KB |
Output is correct |
42 |
Correct |
340 ms |
3428 KB |
Output is correct |
43 |
Correct |
372 ms |
3432 KB |
Output is correct |
44 |
Correct |
336 ms |
3424 KB |
Output is correct |