結果

問題 No.5002 stick xor
ユーザー motimoti
提出日時 2018-05-28 01:26:33
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,223 bytes
コンパイル時間 20,936 ms
実行使用メモリ 3,780 KB
スコア 0
最終ジャッジ日時 2020-08-23 04:37:17
ジャッジサーバーID
(参考情報)
judge9 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 AC 542 ms
3,704 KB
testcase_19 AC 547 ms
3,744 KB
testcase_20 AC 544 ms
3,664 KB
testcase_21 AC 539 ms
3,752 KB
testcase_22 AC 541 ms
3,672 KB
testcase_23 AC 539 ms
3,756 KB
testcase_24 AC 541 ms
3,716 KB
testcase_25 AC 546 ms
3,668 KB
testcase_26 AC 536 ms
3,644 KB
testcase_27 AC 539 ms
3,676 KB
testcase_28 AC 535 ms
3,752 KB
testcase_29 AC 537 ms
3,600 KB
testcase_30 AC 539 ms
3,584 KB
testcase_31 AC 543 ms
3,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
#if defined DEBUG
#define DBG(...) printf(__VA_ARGS__)
#define PAUSE for (char c; ~scanf("%c", &c) && c != '\n';);
#else
#define DBG(...)
#define PAUSE
#endif

using namespace std;

int64_t INF = 1LL<<60;

int a[66][66], a_tmp[66][66];
vector<pair<int, int>> lraw;
int l[666], lrr[666];
int lsum[66][66], lsum_tmp[66][66];
int usum[66][66], usum_tmp[66][66];
int N, K;
int64_t max_diff = -INF;
vector<tuple<int, int, int, int>> max_placement;

void calc_xlsum(int i) {
  for (int j = N - 1; j >= 0; j--) {
    lsum[i][j] = (a[i][j] ? 1 : -1) + (j < N - 1 ? lsum[i][j + 1] : 0);
  }
}

void calc_yusum(int j) {
  for (int i = N - 1; i >= 0; i--) {
    usum[i][j] = (a[i][j] ? 1 : -1) + (i < N - 1 ? usum[i + 1][j] : 0);
  }
}

tuple<int, int, int, int, int> get_max_xcand(int L) {
  int64_t mx = -INF;
  int idx_i = -1, idx_j = -1;
  rep(i, N) rep(j, N - L + 1) {
    int64_t cand = lsum[i][j] - ( j + L < N ? lsum[i][j + L] : 0 );
    if (mx < cand) {
      mx = cand;
      idx_i = i, idx_j = j;
    }
  }
  return make_tuple(mx, idx_i, idx_j, idx_i, idx_j + L - 1);
}

tuple<int, int, int, int, int> get_max_ycand(int L) {
  int64_t mx = -INF;
  int idx_i = -1, idx_j = -1;
  rep(i, N - L + 1) rep(j, N) {
    int64_t cand = usum[i][j] - ( i + L < N ? usum[i + L][j] : 0 );
    if (mx < cand) {
      mx = cand;
      idx_i = i, idx_j = j;
    }
  }
  return make_tuple(mx, idx_i, idx_j, idx_i + L - 1, idx_j);
}

void solve() {
  int64_t raw = 0;
  rep(i, N) rep(j, N) raw += !a[i][j];
  vector<tuple<int, int, int, int>> vec;
  int64_t ret = raw;
  rep(k, K) {
    auto xcand = get_max_xcand(l[k]);
    auto ycand = get_max_ycand(l[k]);
    auto selected = max(xcand, ycand);
    int64_t mx; int top, left, bottom, right;
    tie(mx, top, left, bottom, right) = selected;
    //*
    ret += mx;
    //*/
    vec.emplace_back(top, left, bottom, right);
    if (left == right) {
      REP(i, top, bottom + 1) a[i][left] ^= 1;
      calc_yusum(left);
    }
    if (top == bottom) {
      REP(j, left, right + 1) a[top][j] ^= 1;
      calc_xlsum(top);
    }
    /*
    DBG("-------------------\n");
    DBG("k (/ K) = %d / %d\n", k, K);
    rep(i, N) {
      rep(j, N) {
        if (a[i][j]) {
          DBG("\x1b[40m%3d\x1b[49m", a[i][j]);
        } else {
          DBG("\x1b[47m%3d\x1b[49m", a[i][j]);
        }
      }
      DBG("\n");
    }
    DBG("===================\n");
    PAUSE
    //*/
  }

  int64_t diff = ret - raw;
  /*
  DBG("ret = %d\n", ret);
  DBG("diff = %d\n", diff);
  PAUSE
  //*/
  if (max_diff < diff) {
    max_diff = diff;
    int k = 0;
    max_placement.clear();
    max_placement.resize(K);
    for (auto e: vec) {
      max_placement[lraw[k++].second] = e;
    }
  }
}

int main() {
  cin >> N >> K;
  rep(i, K) {
    int l; cin >> l;
    lraw.emplace_back(l, i);
    lrr[i] = l;
  }

  rep(i, N) {
    string s; cin >> s;
    rep(j, N) a[i][j] = a_tmp[i][j] = s[j] - '0';
  }

  rep(i, N) {
    calc_xlsum(i);
    calc_yusum(i);
    rep(j, N) lsum_tmp[i][j] = lsum[i][j], usum_tmp[i][j] = usum[i][j];
  }

  std::random_device rdev{};
  std::mt19937 mt(rdev());
  // k [1 - 100] 区切りでシャッフルする
  for (int pt = 1; pt < min(K, 101); pt++) {
    sort(lraw.rbegin(), lraw.rend());
    for (int k = 0; ; k++) {
      if (k * pt >= K) break;
      int last = (k + 1) * pt;
      if (last > K) last = K;
      std::shuffle(lraw.begin() + k * pt, lraw.begin() + last, mt);
    }
/*
    DBG("\npartition: %d\n", pt);
    for (auto e: lraw) {
      DBG("%d ", e.first);
    }
    DBG("\n");
//*/
    rep(i, K) l[i] = lraw[i].first;

    /*
    DBG("-------------------\n");
    rep(i, N) {
      rep(j, N) {
        DBG("%3d", a[i][j]);
      }
      DBG("\n");
    }
    DBG("===================\n");
    //*/

    solve();

    rep(i, N) rep(j, N)
      a[i][j] = a_tmp[i][j], lsum[i][j] = lsum_tmp[i][j];
  }

  int i = 0;
  for (auto e: max_placement) {
    int x, y, z, w; tie(x, y, z, w) = e;
    printf("%d %d %d %d\n", x + 1, y + 1, z + 1, w + 1);
//    assert((x == z && y + lrr[i] - 1 == w) || (y == w && x + lrr[i] - 1 == z));
    i++;
  }
  DBG("MAX = %lld\n", max_diff);
}
0