// Problem: #3 - Izbori
// Contest: DMOJ - COCI '21 Contest 4
// URL: https://dmoj.ca/problem/coci21c4p3
// Memory Limit: 512 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#define lg long long
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const lg N = 505;
lg v[N][N];
lg get_sum(lg a, lg b, lg c, lg d)
{
if(c > d) swap(c, d);
if(a > b) swap(a, b);
return v[b][d]-v[a-1][d]-v[b][c-1]+v[a-1][c-1];
}
int main()
{
fastio;
lg n, m, a, b;
cin >> n >> m >> a >> b;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> v[i][j];
v[i][j] += v[i-1][j]+v[i][j-1]-v[i-1][j-1];
}
}
lg ans = 1e18;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
for(int k = 1; k <= i; k++)
{
lg l = 1, r = j, cur = j;
while(l <= r)
{
lg mid = (l+r)/2;
if(get_sum(i, k, j, mid) <= b)
{
r = mid-1;
cur = mid;
}
else{
l = mid+1;
}
}
lg y = get_sum(i, k, j, cur);
ans = min(ans, abs(a-y)+abs(b-y));
l = 1, r = j, cur = j;
while(l <= r)
{
lg mid = (l+r)/2;
if(get_sum(i, k, j, mid) >= a)
{
l = mid+1;
cur = mid;
}
else{
r = mid-1;
}
}
y = get_sum(i, k, j, cur);
ans = min(ans, abs(a-y)+abs(b-y));
}
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
604 KB |
Output is correct |
11 |
Correct |
14 ms |
960 KB |
Output is correct |
12 |
Correct |
14 ms |
860 KB |
Output is correct |
13 |
Correct |
14 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
14 ms |
860 KB |
Output is correct |
16 |
Correct |
14 ms |
960 KB |
Output is correct |
17 |
Correct |
14 ms |
860 KB |
Output is correct |
18 |
Correct |
12 ms |
904 KB |
Output is correct |
19 |
Correct |
13 ms |
860 KB |
Output is correct |
20 |
Incorrect |
12 ms |
1116 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
604 KB |
Output is correct |
11 |
Correct |
14 ms |
960 KB |
Output is correct |
12 |
Correct |
14 ms |
860 KB |
Output is correct |
13 |
Correct |
14 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
14 ms |
860 KB |
Output is correct |
16 |
Correct |
14 ms |
960 KB |
Output is correct |
17 |
Correct |
14 ms |
860 KB |
Output is correct |
18 |
Correct |
12 ms |
904 KB |
Output is correct |
19 |
Correct |
13 ms |
860 KB |
Output is correct |
20 |
Incorrect |
12 ms |
1116 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |