結果
| 問題 | No.5002 stick xor |
| コンテスト | |
| ユーザー |
zenito9970
|
| 提出日時 | 2018-05-27 02:50:38 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 66 ms / 1,000 ms |
| コード長 | 6,014 bytes |
| 記録 | |
| コンパイル時間 | 5,317 ms |
| 実行使用メモリ | 1,612 KB |
| スコア | 36,325 |
| 最終ジャッジ日時 | 2018-05-27 02:50:46 |
|
ジャッジサーバーID (参考情報) |
judge9 / |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 32 |
ソースコード
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
using namespace std;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }
#define imosPW(imos,x,y,l) do{\
(imos)[(y)][(x)]++; \
(imos)[(y)][(x)+(l)]--; \
(imos)[(y)+1][(x)]--; \
(imos)[(y)+1][(x)+(l)]++; } while(0)
#define imosMW(imos,x,y,l) do{\
(imos)[(y)][(x)]--; \
(imos)[(y)][(x)+(l)]++; \
(imos)[(y)+1][(x)]++; \
(imos)[(y)+1][(x)+(l)]--; } while(0)
#define imosPH(imos,x,y,l) do{\
(imos)[(y)][(x)]++; \
(imos)[(y)+(l)][(x)]--; \
(imos)[(y)][(x)+1]--; \
(imos)[(y)+(l)][(x)+1]++; } while(0)
#define imosMH(imos,x,y,l) do{\
(imos)[(y)][(x)]--; \
(imos)[(y)+(l)][(x)]++; \
(imos)[(y)][(x)+1]++; \
(imos)[(y)+(l)][(x)+1]--; } while(0)
namespace Timer {
using namespace std::chrono;
high_resolution_clock::time_point start_time;
inline void init() {
start_time = high_resolution_clock::now();
}
inline int now() {
auto now = high_resolution_clock::now();
auto t = now - start_time;
return duration_cast<milliseconds>(t).count();
}
inline bool check(int limit) {
return now() < limit;
}
}
struct Data {
bool isH;
int x, y;
};
void output(int l, Data d) {
int eX = d.isH ? d.x : (d.x + l - 1);
int eY = d.isH ? (d.y + l - 1) : d.y;
cout << d.y+1 << " " << d.x+1 << " " << eY+1 << " " << eX+1 << endl;
}
// vector<vint> imosX2grid(vector<vint> imos) {
// const int n = imos.size();
// rep(y, n) rep(x, n-1) {
// imos[y][x+1] += imos[y][x];
// }
// return imos;
// }
// int calcScore(const vector<vint>& grid) {
// const int n = grid.size();
// int cnt = 0;
// rep(y, n) rep(x, n) if(grid[y][x] % 2 == 0) cnt++;
// return cnt;
// }
int calcScore(const vector<vint>& A, vector<vint> imos) {
const int n = A.size();
rep(y, n) rep(x, n) imos[y][x+1] += imos[y][x];
rep(x, n) rep(y, n) imos[y+1][x] += imos[y][x];
int cnt = 0;
rep(y, n) rep(x, n) if((A[y][x] + imos[y][x]) % 2 == 0) cnt++;
return cnt;
}
int main() {
Timer::init();
int n, k; cin >> n >> k;
vint L(k);
rep(i, k) cin >> L[i];
vector<vint> A(n, vint(n));
rep(y, n) rep(x, n) { char c; cin >> c; A[y][x] = c == '1'; }
vector<vint> imos(n+1, vint(n+1, 0));
int initialScore = calcScore(A, imos);
dump(initialScore);
cerr << "random" << endl;
mt19937 mt(random_device{}());
vector<Data> data(k);
rep(i, k) {
data[i].isH = false;
data[i].x = 0;
data[i].y = 0;
}
int cycle = 0, cntAccept = 0;
int beforeScore = calcScore(A, imos);
vector<Data> bestData = data;
cerr << "while" << endl;
rep(ck, k) {
const int l = L[ck];
int bestScore = -1e9;
int bestX = 0, bestY = 0;
bool bestR = false;
// imosPW(imos, bestX, bestY, l);
// rep(y, n) {
// rep(x, n - l) {
// imosMW(imos, bestX, bestY, l);
// imosPW(imos, x, y, l);
// int score = calcScore(A, imos);
// if(bestScore < score) {
// bestScore = score;
// bestX = x; bestY = y;
// bestR = false;
// } else {
// imosMW(imos, x, y, l);
// imosPW(imos, bestX, bestY, l);
// }
// }
// }
// dump(ck); dump(bestX); dump(bestY);
// imosMW(imos, bestX, bestY, l);
FOR(x, bestX, bestX + l) A[bestY][x]++;
rep(y, n) {
rep(x, n - l) {
int dscore = 0;
FOR(tx, bestX, bestX + l) { A[bestY][tx]--; if(A[bestY][tx] % 2 == 0) dscore++; else dscore--; }
FOR(tx, x, x + l) { A[y][tx]++; if(A[y][tx] % 2 == 0) dscore++; else dscore--; }
if(0 < dscore) {
bestX = x; bestY = y;
bestR = false;
} else {
FOR(tx, x, x + l) A[y][tx]--;
FOR(tx, bestX, bestX + l) A[bestY][tx]++;
}
}
}
// dump(ck); dump(bestX); dump(bestY);
FOR(tx, bestX, bestX + l) A[bestY][tx]--;
// imosPH(imos, bestX, bestY, l);
// rep(y, n - l) {
// rep(x, n) {
// imosMH(imos, bestX, bestY, l);
// imosPH(imos, x, y, l);
// int score = calcScore(A, imos);
// if(bestScore < score) {
// bestScore = score;
// bestX = x; bestY = y;
// bestR = true;
// } else {
// imosMH(imos, x, y, l);
// imosPH(imos, bestX, bestY, l);
// }
// }
// }
// dump(ck); dump(bestX); dump(bestY);
// imosMH(imos, bestX, bestY, l);
data[ck].x = bestX;
data[ck].y = bestY;
data[ck].isH = bestR;
if(bestR) {
// imosPH(imos, bestX, bestY, l);
FOR(ty, bestY, bestY + l) A[ty][bestX]++;
} else {
// imosPW(imos, bestX, bestY, l);
FOR(tx, bestX, bestX + l) A[bestY][tx]++;
}
}
bestData = data;
cerr << "output" << endl;
rep(i, k) output(L[i], bestData[i]);
int lastScore = [&]() {
int score = 0;
rep(y, n) rep(x, n) if(A[y][x] % 2 == 0) score++;
return score;
}();
dump(cycle);
dump(cntAccept);
cerr << "score: " << (lastScore - initialScore) << endl;
dump(Timer::now());
return 0;
}
zenito9970