# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
236223 | Aldas25 | Coins (LMIO19_monetos) | C++14 | 484 ms | 69112 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
int t, n, k1, k2;
bool grid[310][310];
bool nGrid[310][310];
bool ans[310][310];
int bst;
int checkGrid () {
int ret = 0;
FOR(i, 1, n) FOR(j, 1, n)
if (grid[i][j] != nGrid[i][j]) ret++;
return (ret/2);
}
void setGrid() {
int nBst = checkGrid();
if (nBst < bst) {
bst = nBst;
FOR(i, 1, n) FOR(j, 1, n) ans[i][j] = nGrid[i][j];
}
}
void printAns (){
//cout << " bst=" << bst << endl;
FOR(i,1 ,n) {
FOR(j, 1, n) cout << ans[i][j] << " ";
cout << "\n";
}
}
void checkRectangle (int x, int y) {
FOR(i, 1, n) FOR(j, 1, n) nGrid[i][j] = 0;
FOR(i, n-x+1, n) FOR(j, n-y+1, n) nGrid[i][j] = 1;
setGrid();
}
int dp[110][110][65*65];
int p[110][110][65*65];
int stulp[110][115];
int cnt[110][110];
bool temp[110][110];
int orN;
void solveDP() {
orN = n;
int kx = 5;
FOR(i, 1, n) FOR(j, 1, n) {
int nI = (i+kx-1)/kx;
int nJ = (j+kx-1)/kx;
if (orN <= 50) nI = i, nJ = j;
cnt[nI][nJ] += !(grid[i][j]);
}
if (orN > 50) n /= kx;
//FOR(i, 1, n) FOR(j, 1, n) cerr << " cnt (" << i << ", " << j << ") = " << cnt[i][j] << endl;
FOR(j, 1, n) for (int i = n; i > 0; i--) stulp[i][j] = stulp[i+1][j] + cnt[i][j];
FOR(i, 0, n) FOR(j, 0, n) FOR(k, 0, n*n/2) dp[i][j][k] = 500*500;
dp[0][0][0] = 0;
int mnAns = 500*500;
int curJ = 0;
FOR(j, 1, n) FOR(i, 0, n) FOR(k, 0, n*n/2) {
FOR(prJ, 0, n) {
if (prJ > i) break;
int prS = k-i;
if (dp[j][i][k] > dp[j-1][prJ][prS] + stulp[n-i+1][j]) {
dp[j][i][k] = dp[j-1][prJ][prS] + stulp[n-i+1][j];
p[j][i][k] = prJ;
}
}
//if (i == n && k == n*n/2 && dp) mnAns = min(mnAns, dp[j][i][k]);
}
FOR(j, 0, n) {
if (dp[n][j][n*n/2] < mnAns) {
mnAns = dp[n][j][n*n/2];
curJ = j;
}
}
int curS = n*n/2;
for (int j = n; j > 0; j--) {
FOR(i, 1, n) temp[i][j] = 0;
FOR(i, n-curJ+1, n) temp[i][j] = 1;
int nSum = curS - curJ;
curJ = p[j][curJ][curS];
curS = nSum;
}
//FOR(i, 1, n) FOR(j, 1, n) cerr << " temp[" << i << "][" << j << "] = " << temp[i][j] << endl;
FOR(i, 1, n) FOR(j, 1,n) {
if (orN> 50) FOR(di, i*kx - kx+1, i*kx) FOR(dj, j*kx - kx+1, j*kx) ans[di][dj] = temp[i][j];
else ans[i][j] = temp[i][j];
}
if (orN > 50) n *= kx;
}
int main()
{
FAST_IO;
//freopen("monetos-vyr.03.in","r",stdin);
//freopen("monetos-vyr.03.out","w",stdout);
cin >> t >> n >> k1 >> k2;
FOR(i, 1, n) FOR(j, 1, n) cin >> grid[i][j];
//if (n <= 50) {
solveDP();
//bst = checkGrid();
printAns();
// return 0;
// }
/*FOR(i, 1, n) {
FOR(j, 1, n/2) nGrid[i][j] = ans[i][j] = 0;
FOR(j, n/2+1, n) nGrid[i][j] = ans[i][j] = 1;
}
setGrid();
//bst = checkGrid();
FOR(i, 1, n/2) FOR(j, 1, n) nGrid[i][j] = 0;
FOR(i, n/2+1, n) FOR(j, 1, n) nGrid[i][j] = 1;
setGrid();
FOR(i, 1, n) {
int rp = n-i+1;
if (i > n/2) rp--;
FOR(j, 1, rp) nGrid[i][j] = 0;
FOR(j, rp+1, n) nGrid[i][j] = 1;
}
setGrid();
FOR(i, 1, n) {
if ((n*n/2)%i == 0) checkRectangle(i, (n*n/2)/i);
}
printAns();
*/
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |