#include <iostream>
#include <vector>
#include <algorithm>
#define int int64_t
using namespace std;
struct orch1
{
vector<vector<int>> orchard;
int n, m;
int calc()
{
for (int i = 0; i < n; i++)
{
int currsum = 0;
int mmin = 0;
int mp = -1;
int mdiff = 0;
int msp = 0;
int me = 0;
for (int j = 0; j < m; j++)
{
currsum += (orchard[i][j]) - (!orchard[i][j]);
if (currsum < mmin)
{
mmin = currsum;
mp = j;
}
if (currsum - mmin > mdiff)
{
msp = mp;
mdiff = currsum - mmin;
me = j;
}
}
currsum = 0;
for (int j = 0; j < m; j++)
{
if (j <= msp || j > me) currsum += (orchard[i][j]);
else currsum += (!orchard[i][j]);
}
return currsum;
}
}
};
struct orchard
{
vector<vector<int>> orchard;
vector<vector<int>> table;
int n, m;
void createtable()
{
table.resize(n + 1, vector<int>(m + 1));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
table[i + 1][j + 1] = table[i + 1][j] + table[i][j + 1] - table[i][j] + orchard[i][j];
}
}
//printtable();
int mmin = 10000000000;
for (int x1 = 1; x1 <= n; x1++)
{
for (int y1 = 0; y1 <= m; y1++)
{
for (int x2 = 0; x2 < x1; x2++)
{
for (int y2 = 0; y2 < y1; y2++)
{
int in = prefsum(x1, y1, x2, y2);
int wrongin = (x1 - x2) * (y1 - y2) - in;
int wrongout = prefsum(n, m, 0, 0) - in;
mmin = min(mmin, wrongin + wrongout);
}
}
}
}
cout << mmin << "\n";
}
void printtable()
{
for (auto t : table)
{
for (auto i : t)
{
cout << i << " ";
}
cout << "\n";
}
cout << "\n";
}
int prefsum(int x1, int y1, int x2, int y2)
{
return table[x1][y1] + table[x2][y2] - table[x1][y2] - table[x2][y1];
}
};
signed main()
{
int n, m;
cin >> n >> m;
if (n > 1)
{
orchard orch;
orch.n = n;
orch.m = m;
orch.orchard.resize(n, vector<int>(m));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> orch.orchard[i][j];
}
}
orch.createtable();
}
else
{
orch1 orch;
orch.n = n;
orch.m = m;
orch.orchard.resize(n, vector<int>(m));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> orch.orchard[i][j];
}
}
cout << orch.calc() << "\n";
}
}
Compilation message
orchard.cpp: In member function 'int64_t orch1::calc()':
orchard.cpp:48:5: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
6 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
270 ms |
15992 KB |
Output is correct |
2 |
Correct |
272 ms |
15996 KB |
Output is correct |
3 |
Correct |
273 ms |
15992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1078 ms |
5012 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
504 KB |
Output is correct |
2 |
Correct |
256 ms |
732 KB |
Output is correct |
3 |
Correct |
256 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1083 ms |
12152 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |