이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// 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 = 2e18;
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 = 1;
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |