/*
* @Author: Twynzz
* @Last Modified by: Twynzz
* @Note For This Problem: https://wbd.ms/share/v2/aHR0cHM6Ly93aGl0ZWJvYXJkLm1pY3Jvc29mdC5jb20vYXBpL3YxLjAvd2hpdGVib2FyZHMvcmVkZWVtLzIxYTA3NjI4Y2ZjOTRlYjRhYTRiYjVhZGE1Nzk3YmRkX0JCQTcxNzYyLTEyRTAtNDJFMS1CMzI0LTVCMTMxRjQyNEUzRF8zZTFhNWZlZC05N2RmLTQxNGYtODUyOS1kZDY4N2JhMDU3NTg=
*/
#include <bits/stdc++.h>
using namespace std;
// ------------ Define ------------
#define el "\n"
#define int long long
#define ld long double
#define str string
#define file(name) {freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
#define IOS {std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define _TBN_ signed main()
#define setp(n) setprecision(n)<< fixed
#define fi first
#define se second
#define ii pair<int,int>
#define gcd(a,b) __gcd(a,b)
#define tup pair<int,ii>
#define pb push_back
#define pf push_front
#define sz(v) (int)v.size()
const int maxn = 1e6+9;
const long long oo = 1e18;
const int MOD = 1e9+7, Mod = 998244353;
const int base = 311;
template<class X>
bool maximize(X &a, const X &b)
{
if (a<b)
{
a = b;
return 1;
}
return 0;
}
template<class X>
bool minimize(X &a, const X &b)
{
if (a>b)
{
a = b;
return 1;
}
return 0;
}
void sub1();
void sub2();
void sub3();
int lcm(int a, int b)
{
return a/gcd(a,b)*b;
}
int sqr(int x) {return x*x;}
inline bool MASK(int mask, int bit) {return ((mask>>bit)&1);}
long long F(int n) {return n*(n+1)/2;}
inline int add(int a, int b, int m = MOD) {return ((a%m+b%m)%m+m)%m;}
inline int sub(int a, int b, int m = MOD) {return ((a-b)%m+m)%m;}
inline int mul(int a, int b, int m = MOD) {return ((a%m)*(b%m))%m;}
int dx[] = {-1,1,0,0};
int dy[] = {0,0,-1,1};
int n, m, a, b, c[509][509], pref[509][509];
int pre[509];
int f(int mid, int i)
{
int S = pre[i] - pre[mid - 1];
return abs(S - a) + abs(S - b);
}
_TBN_
{
IOS;
if (fopen("iloveyourmom.inp","r")) file("iloveyourmom");
cin >>n >>m >>a >>b;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >>c[i][j];
pref[i][j] = pref[i - 1][j] + c[i][j];
}
}
for(int i = 1; i <= 1; i++)
{
if (n <= 50 && m <= 50) sub1();
else sub2();
}
return 0;
}
int d[509];
void sub1()
{
int mini = oo;
for(int row1 = 1; row1 <= n; row1++)
{
for(int row2 = row1; row2 <= n; row2++)
{
for(int i = 1; i <= m; i++)
d[i] = pref[row2][i] - pref[row1 - 1][i];
for(int l = 1; l <= m; l++)
{
int sum = 0;
for(int r = l; r <= n; r++)
{
sum += d[r];
minimize(mini, abs(sum - a) + abs(sum - b));
}
}
}
}
cout<< mini<< el;
}
void sub2()
{
int mini = oo;
for(int row1 = 1; row1 <= n; row1++)
{
for(int row2 = row1; row2 <= n; row2++)
{
for(int i = 1; i <= m; i++)
d[i] = pref[row2][i] - pref[row1 - 1][i];
int j = 1;
for(int i = 1; i <= m; i++)
{
pre[i] = pre[i - 1] + d[i];
while(j < i && f(j, i) > f(j + 1, i))
j++;
minimize(mini, f(j, i));
}
}
}
cout<< mini<< el;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:17:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | #define file(name) {freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:89:40: note: in expansion of macro 'file'
89 | if (fopen("iloveyourmom.inp","r")) file("iloveyourmom");
| ^~~~
Main.cpp:17:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | #define file(name) {freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:89:40: note: in expansion of macro 'file'
89 | if (fopen("iloveyourmom.inp","r")) file("iloveyourmom");
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |