結果

問題 No.918 LISGRID
ユーザー Iván SotoIván Soto
提出日時 2021-05-13 05:37:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 4,029 bytes
コンパイル時間 2,728 ms
コンパイル使用メモリ 220,040 KB
実行使用メモリ 11,968 KB
最終ジャッジ日時 2023-10-24 23:35:04
合計ジャッジ時間 9,359 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 66 ms
8,536 KB
testcase_02 AC 21 ms
5,408 KB
testcase_03 AC 33 ms
5,952 KB
testcase_04 AC 25 ms
5,388 KB
testcase_05 AC 116 ms
9,592 KB
testcase_06 AC 102 ms
9,116 KB
testcase_07 AC 164 ms
11,704 KB
testcase_08 AC 82 ms
8,284 KB
testcase_09 AC 118 ms
9,856 KB
testcase_10 AC 117 ms
9,856 KB
testcase_11 AC 117 ms
9,856 KB
testcase_12 AC 132 ms
10,384 KB
testcase_13 AC 99 ms
9,064 KB
testcase_14 AC 151 ms
11,176 KB
testcase_15 AC 178 ms
11,968 KB
testcase_16 AC 48 ms
6,724 KB
testcase_17 AC 52 ms
6,960 KB
testcase_18 AC 49 ms
6,752 KB
testcase_19 AC 12 ms
4,700 KB
testcase_20 AC 74 ms
7,912 KB
testcase_21 AC 15 ms
5,008 KB
testcase_22 AC 10 ms
4,516 KB
testcase_23 AC 39 ms
6,252 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 3 ms
4,348 KB
testcase_35 AC 5 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 70 ms
7,768 KB
testcase_38 AC 42 ms
6,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *  author: ivanzuki   
 *  created: Wed May 12 2021
**/
#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
template<typename T> void dout(string name, int idx, T arg) {
  cerr << name << " = " << to_string(arg);
}

template<typename T1, typename... T2> void dout(string names, int idx, T1 arg, T2... args) {
  cerr << names.substr(0, names.find(',')) << " = " << to_string(arg) << ", ";
  dout(names.substr(names.find(',') + 2), idx + 1, args...);
}

#ifdef LOCAL
#define debug(...) cerr << "[", dout(#__VA_ARGS__, 0, __VA_ARGS__), cerr << "]" << endl;
#else
#define debug(...) 42
#endif

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  vector<int> oa = a, ob = b;
  vector<vector<int>> ans(n, vector<int>(m));
  set<int> unused;
  for (int k = 0; k < n * m; k++) {
    unused.insert(k);
  }
  int l = 0, r = n * m - 1;
  for (int i = 0, j = 0; i < n && j < m; ) {
    debug(i, b);
    if (b[j] > 1) {
      int pref = a[i] - 1;
      vector<int> to_reverse;
      for (int k = j; k < m; k++) {
        assert(b[k] > 0);
        if (b[k] == 1) {
          ans[i][k] = r;
          --r;
        } else {
          ans[i][k] = l;
          ++l;
          if (pref > 0) {
            --pref;
          } else {
            to_reverse.push_back(k);
          }
          --b[k];
        }
      }
      vector<int> nums;
      for (int k : to_reverse) {
        nums.push_back(ans[i][k]);
      }
      reverse(nums.begin(), nums.end());
      for (int k = 0; k < (int) to_reverse.size(); k++) {
        ans[i][to_reverse[k]] = nums[k];
      }
      ++i;
    } else if (b[j] == 1) {
      vector<int> to_reverse;
      for (int k = i; k < n; k++) {
        if (a[k] == 1) {
          ans[k][j] = r;
          --r;
        } else {
          ans[k][j] = l;
          ++l;
          --a[k];
          to_reverse.push_back(k);
        }
      }
      vector<int> nums;
      for (int k : to_reverse) {
        nums.push_back(ans[k][j]);
      }
      reverse(nums.begin(), nums.end());
      for (int k = 0; k < (int) to_reverse.size(); k++) {
        ans[to_reverse[k]][j] = nums[k];
      }
      ++j;
    } else {
      assert(0);
    }
  }
  
  vector<int> seen(n * m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      ++seen[ans[i][j]];
      cout << ans[i][j] + 1 << ' ';
    }
    cout << '\n';
  }
  a = oa, b = ob;
  for (int row = 0; row < n; row++) {
    vector<int> dp(m, 1);
    for (int j = 1; j < m; j++) {
      for (int i = 0; i < j; i++) {
        if (ans[row][i] < ans[row][j]) {
          dp[j] = max(dp[j], dp[i] + 1);
        }
      }
    }
    assert(a[row] == *max_element(dp.begin(), dp.end()));
  }
  for (int col = 0; col < m; col++) {
    vector<int> dp(n, 1);
    for (int j = 1; j < n; j++) {
      for (int i = 0; i < j; i++) {
        if (ans[i][col] < ans[j][col]) {
          dp[j] = max(dp[j], dp[i] + 1);
        }
      }
    }
    debug(dp);
    // assert(b[col] == *max_element(dp.begin(), dp.end()));
  }

  assert(all_of(seen.begin(), seen.end(), [&](int x) { return x == 1; }));
  return 0;
}
0