結果

問題 No.5002 stick xor
ユーザー ふっぴーふっぴー
提出日時 2018-05-26 01:08:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 888 ms / 1,000 ms
コード長 3,229 bytes
コンパイル時間 32,186 ms
実行使用メモリ 1,652 KB
スコア 39,393
最終ジャッジ日時 2018-05-26 01:09:28
ジャッジサーバーID
(参考情報)
judge6 /
純コード判定しない問題か言語
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 881 ms
1,652 KB
testcase_01 AC 839 ms
1,652 KB
testcase_02 AC 877 ms
1,652 KB
testcase_03 AC 863 ms
1,648 KB
testcase_04 AC 833 ms
1,652 KB
testcase_05 AC 844 ms
1,652 KB
testcase_06 AC 820 ms
1,652 KB
testcase_07 AC 855 ms
1,652 KB
testcase_08 AC 866 ms
1,652 KB
testcase_09 AC 855 ms
1,648 KB
testcase_10 AC 864 ms
1,652 KB
testcase_11 AC 879 ms
1,648 KB
testcase_12 AC 872 ms
1,652 KB
testcase_13 AC 855 ms
1,652 KB
testcase_14 AC 852 ms
1,652 KB
testcase_15 AC 842 ms
1,652 KB
testcase_16 AC 881 ms
1,648 KB
testcase_17 AC 843 ms
1,648 KB
testcase_18 AC 857 ms
1,648 KB
testcase_19 AC 832 ms
1,652 KB
testcase_20 AC 834 ms
1,648 KB
testcase_21 AC 881 ms
1,652 KB
testcase_22 AC 838 ms
1,652 KB
testcase_23 AC 871 ms
1,652 KB
testcase_24 AC 851 ms
1,652 KB
testcase_25 AC 830 ms
1,652 KB
testcase_26 AC 888 ms
1,648 KB
testcase_27 AC 860 ms
1,652 KB
testcase_28 AC 885 ms
1,652 KB
testcase_29 AC 866 ms
1,648 KB
testcase_30 AC 845 ms
1,652 KB
testcase_31 AC 845 ms
1,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
const ll inf = 1000000001;
const ll INF = 2e18;
const ll MOD = 1000000007;
//const ll mod = 1000000009;
const double pi = 3.14159265358979323846;
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0, -1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);

#include<random>


struct Point {
	int i0, j0, i1, j1;
	int len;
	Point(int i0, int j0, int i1, int j1) :i0(i0), j0(j0), i1(i1), j1(j1) {
		len = max(abs(i0 - i1), abs(j0 - j1)) + 1;
	}
	Point() {}
};

int ma = -inf;
int n, k;
vector<Point> ans;

void solve(vi &l, vii a) {
	int idx = 0;
	int sum = 0;
	for (int i = l.size() - 1; i >= 0; i--) {
		sum += l[i];
		if (sum >= n * n) {
			idx = i;
			break;
		}
	}
	vector<Point> res;
	int tar;
	for (int i = 0; i < k; i++) {
		if (i < idx) {
			tar = 1;
		}
		else {
			tar = 0;
		}
		pair<int, Point> p;
		p.first = -inf;
		rep(j, n) {
			if (j + l[i] - 1 >= n) {
				break;
			}
			rep(k, n) {
				int cnt = 0;
				rep(k2, l[i]) {
					if (a[j + k2][k] == tar) {
						cnt--;
					}
					else {
						cnt++;
					}
				}
				if (cnt > p.first) {
					p = make_pair(cnt, Point(j, k, j + l[i] - 1, k));
				}
			}
		}
		rep(j, n) {
			rep(k, n) {
				if (k + l[i] - 1 >= n) {
					break;
				}
				int cnt = 0;
				rep(k2, l[i]) {
					if (a[j][k + k2] == tar) {
						cnt--;
					}
					else {
						cnt++;
					}
				}
				if (cnt > p.first) {
					p = make_pair(cnt, Point(j, k, j, k + l[i] - 1));
				}
			}
		}
		for (int j = p.second.i0; j <= p.second.i1; j++) {
			for (int k = p.second.j0; k <= p.second.j1; k++) {
				a[j][k] = 1 - a[j][k];
			}
		}
		res.push_back(p.second);
	}
	int cnt = 0;
	rep(i, n) {
		rep(j, n) {
			if (a[i][j] == 0) {
				cnt++;
			}
		}
	}
	//DEBUG(cnt);
	if (cnt > ma) {
		ma = cnt;
		ans = res;
	}
}

int main() {
	auto start = clock();
	random_device seed_gen;
	mt19937 rand(seed_gen());
	cin >> n >> k;
	vi lori(k);
	rep(i, k) {
		cin >> lori[i];
	}
	vii aori(n, vi(n));
	rep(i, n) {
		rep(j, n) {
			char c;
			scanf(" %c", &c);
			aori[i][j] = c - '0';
		}
	}


	vi l = lori;
	sort(all(l), greater<int>());
	solve(l, aori);
	l = lori;
	sort(all(l));
	solve(l, aori);
	while ((clock() - start) < 0.8 * CLOCKS_PER_SEC) {
		shuffle(all(l), rand);
		solve(l, aori);
	}
	vector<bool> used(k, false);
	rep(i, k) {
		rep(j, k) {
			if (!used[j] && ans[j].len == lori[i]) {
				used[j] = true;
				cout << ans[j].i0 + 1<< " " << ans[j].j0 + 1 << " " << ans[j].i1 + 1 << " " << ans[j].j1 + 1 << endl;
				break;
			}
		}
	}
}
0