QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#333513#4834. Trijectionnhuang6850 1ms3628kbC++208.2kb2024-02-20 04:08:412024-02-20 04:08:42

Judging History

你现在查看的是最新测评结果

  • [2024-02-20 04:08:42]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3628kb
  • [2024-02-20 04:08:41]
  • 提交

answer

/**
 * @file qoj4834-1.cpp
 * @author n685
 * @brief
 * @date 2024-02-18
 *
 *
 */
#include <bits/stdc++.h>

#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbgR(...) 4242
#define dbgP(...) 420
#define dbgRP(...) 420420
void nline() {}
#endif

constexpr int MAXN = 35;
// constexpr int MAXN = 5;

int n;
std::array<std::array<int64_t, 2 * MAXN + 1>, 2 * MAXN + 1> dp;
struct BSeq {
  std::vector<int> seq;
  BSeq() {}
  BSeq(std::vector<int> _seq) : seq(_seq) {}
  BSeq(int64_t num) {
    seq.resize(2 * n + 1);
    int dep = 0;
    for (int i = 1; i <= 2 * n - 1; ++i) {
      if (num >= dp[2 * n - i][dep + 1]) {
        num -= dp[2 * n - i][dep + 1];
        seq[i] = -1;
        --dep;
      } else {
        seq[i] = 1;
        ++dep;
      }
    }
    seq.back() = -1;
  }
  int64_t toNum() {
    int64_t ans = 0;
    int dep = 0;
    for (int i = 1; i <= 2 * n; ++i) {
      if (seq[i] == 1) {
        ++dep;
      } else {
        if (i < 2 * n) {
          ans += dp[2 * n - i][dep + 1];
        }
        --dep;
      }
    }
    return ans;
  }
  friend std::ostream &operator<<(std::ostream &out, const BSeq &b) {
    for (int i = 1; i <= 2 * n; ++i) {
      if (b.seq[i] == 1) {
        out << '(';
      } else {
        out << ')';
      }
    }
    return out;
  }
};
struct Poly {
  int r, c;
  std::vector<std::vector<bool>> grid;
  Poly() {}
  Poly(BSeq b) {
    c = 0;
    for (int i = 2; i < (int)b.seq.size() - 1; i += 2) {
      c += (b.seq[i] == 1);
    }
    ++c;
    r = (n + 1) - c;
    grid.assign(r, std::vector<bool>(c));
    int i = 0, j = 0;
    int pos = 2;
    while (i <= r - 1 || j <= c - 1) {
      grid[i][j] = true;
      if (i == r - 1 && j == c - 1) {
        break;
      }
      if (b.seq[pos] == 1) {
        ++j;
      } else {
        ++i;
      }
      pos += 2;
    }
    i = 0, j = 0;
    pos = 3;
    while (i <= r - 1 || j <= c - 1) {
      grid[i][j] = true;
      for (int k = i - 1; k >= 0; --k) {
        if (grid[k][j]) {
          break;
        }
        grid[k][j] = true;
      }
      if (i == r - 1 && j == c - 1) {
        break;
      }
      if (b.seq[pos] == 1) {
        ++i;
      } else {
        ++j;
      }
      pos += 2;
    }
  }
  BSeq toBSeq() {
    std::vector<int> seq(2 * n + 1);
    seq[1] = 1;
    seq.back() = -1;
    int i = 0, j = 0;
    int pos = 2;
    while (i < r - 1 || j < c - 1) {
      if (j < c - 1 && grid[i][j + 1]) {
        ++j;
        seq[pos] = 1;
      } else {
        ++i;
        seq[pos] = -1;
      }
      pos += 2;
    }
    i = 0, j = 0;
    pos = 3;
    while (i < r - 1 || j < c - 1) {
      if (i < r - 1 && grid[i + 1][j]) {
        ++i;
        seq[pos] = 1;
      } else {
        ++j;
        seq[pos] = -1;
      }
      pos += 2;
    }
    return seq;
  }
  friend std::istream &operator>>(std::istream &in, Poly &p) {
    in >> p.r >> p.c;
    p.grid.assign(p.r, std::vector<bool>(p.c));
    for (int i = p.r - 1; i >= 0; --i) {
      for (int j = 0; j < p.c; ++j) {
        char c;
        in >> c;
        p.grid[i][j] = (c == '#');
      }
    }
    return in;
  }
  friend std::ostream &operator<<(std::ostream &out, const Poly &p) {
    out << "poly\n";
    out << p.r << ' ' << p.c << '\n';
    for (int i = p.r - 1; i >= 0; --i) {
      for (int j = 0; j < p.c; ++j) {
        if (p.grid[i][j]) {
          out << "#";
        } else {
          out << ".";
        }
      }
      if (i > 0) {
        out << '\n';
      }
    }
    return out;
  }
};
struct Perm {
  std::vector<int> p;
  Perm() {}
  Perm(std::vector<int> _p) : p(_p) {}
  Perm(BSeq b) {
    std::vector<bool> vis(n + 1);
    auto fromStart = [&](int num) {
      for (int i = 1; i <= n; ++i) {
        if (vis[i]) {
          continue;
        }
        --num;
        if (num == 0) {
          return i;
        }
      }
      return n + 1;
    };
    p.push_back(0);
    int dep = 0;
    for (int i = 1; i <= 2 * n; ++i) {
      dep += b.seq[i];
      if (i < 2 * n && b.seq[i] == 1 && b.seq[i + 1] == -1) {
        int num = fromStart(dep);
        p.push_back(num);
        vis[num] = true;
      } else if (i > 0 && b.seq[i - 1] == -1 && b.seq[i] == -1) {
        int num = fromStart(1);
        p.push_back(num);
        vis[num] = true;
      }
    }
  }
  BSeq toBSeq() {
    BSeq b;
    b.seq.push_back(0);
    int mx = 0;
    int dep = 0;
    for (int i = 1; i <= n; ++i) {
      if (p[i] > mx) {
        while (dep < p[i]) {
          b.seq.push_back(1);
          ++dep;
        }
        mx = p[i];
      }
      b.seq.push_back(-1);
    }
    return b;
  }
  friend std::istream &operator>>(std::istream &in, Perm &p) {
    p.p.resize(n + 1);
    for (int i = 1; i <= n; ++i) {
      in >> p.p[i];
    }
    return in;
  }
  friend std::ostream &operator<<(std::ostream &out, const Perm &p) {
    out << "perm\n";
    for (int i = 1; i <= n; ++i) {
      out << p.p[i];
      if (i < n) {
        out << ' ';
      }
    }
    return out;
  }
};
struct Triang {
  std::vector<std::array<int, 3>> t;
  void fromBSeq(std::vector<int> seq, int l, int r) {
    if (r - l == 1) {
      return;
    }
    int dep = 0;
    int m = (int)seq.size() - 1;
    for (int i = 1; i <= m; ++i) {
      dep += seq[i];
      if (dep == 0) {
        // index 2...i - 1 and i + 1...m
        t.push_back(std::array{l, l + i / 2, r});
        std::vector<int> ll{0};
        ll.insert(ll.end(), seq.begin() + 2, seq.begin() + i);
        fromBSeq(ll, l, l + i / 2);
        std::vector<int> rr{0};
        rr.insert(rr.end(), seq.begin() + i + 1, seq.end());
        fromBSeq(rr, l + i / 2, r);
        return;
      }
    }
  }
  Triang() {}
  Triang(BSeq b) {
    fromBSeq(b.seq, 1, n + 2);
    std::sort(t.begin(), t.end());
  }
  std::vector<int> toBSeq(int l, int r) {
    if (r - l <= 1) {
      return {0};
    }
    for (auto [a, b, c] : t) {
      if (a == l && c == r && l <= b && b <= r) {
        auto ll = toBSeq(l, b);
        auto rr = toBSeq(b, r);
        std::vector<int> sol{0, 1};
        sol.insert(sol.end(), ll.begin() + 1, ll.end());
        sol.push_back(-1);
        sol.insert(sol.end(), rr.begin() + 1, rr.end());
        return sol;
      }
    }
    assert(false);
  }
  BSeq toBSeq() { return toBSeq(1, n + 2); }
  friend std::istream &operator>>(std::istream &in, Triang &t) {
    t.t.resize(n);
    for (auto &[a, b, c] : t.t) {
      in >> a >> b >> c;
    }
    return in;
  }
  friend std::ostream &operator<<(std::ostream &out, const Triang &t) {
    out << "triang\n";
    for (int i = 0; i < n; ++i) {
      out << t.t[i][0] << ' ' << t.t[i][1] << ' ' << t.t[i][2];
      if (i < n - 1) {
        out << '\n';
      }
    }
    return out;
  }
};

int main() {
#ifndef LOCAL
  std::cin.tie(nullptr)->sync_with_stdio(false);
#endif

  int t;
  std::cin >> n >> t;

  dp[0][0] = 1;
  for (int i = 1; i <= 2 * n; ++i) {
    for (int j = 0; j <= i; ++j) {
      if (j > 0) {
        dp[i][j] += dp[i - 1][j - 1];
      }
      if (j < 2 * n) {
        dp[i][j] += dp[i - 1][j + 1];
      }
    }
  }

  std::cout << n << ' ' << t << '\n';
  for (int i = 0; i < t; ++i) {
    std::string s;
    std::cin >> s;
    if (s == "poly") {
      Poly p;
      std::cin >> p;
      dbg(p.toBSeq());
      int64_t num = p.toBSeq().toNum();
      dbg(num);
      if (num % 2 == 0) {
        std::cout << Perm(BSeq(num + 1)) << '\n';
      } else {
        std::cout << Triang(BSeq(num - 1)) << '\n';
      }
    } else if (s == "perm") {
      Perm p;
      std::cin >> p;
      int64_t num = p.toBSeq().toNum();
      dbg(num);
      if (num % 2 == 0) {
        std::cout << Triang(BSeq(num + 1)) << '\n';
      } else {
        std::cout << Poly(BSeq(num - 1)) << '\n';
      }
    } else {
      Triang tt;
      std::cin >> tt;
      int64_t num = tt.toBSeq().toNum();
      dbg(num);
      if (num % 2 == 0) {
        std::cout << Poly(BSeq(num + 1)) << '\n';
      } else {
        std::cout << Perm(BSeq(num - 1)) << '\n';
      }
    }
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3580kb

input:

5 4
poly
4 2
.#
##
##
#.
perm
4 1 5 2 3
triang
1 2 4
1 4 5
1 5 7
2 3 4
5 6 7
perm
2 1 3 5 4

output:

5 4
triang
1 2 7
2 3 4
2 4 6
2 6 7
4 5 6
triang
1 2 3
1 3 4
1 4 6
1 6 7
4 5 6
poly
3 3
.##
###
##.
triang
1 2 3
1 3 7
3 4 7
4 5 7
5 6 7

input:

5 4
triang
1 2 7
2 3 4
2 4 6
2 6 7
4 5 6
triang
1 2 3
1 3 4
1 4 6
1 6 7
4 5 6
poly
3 3
.##
###
##.
triang
1 2 3
1 3 7
3 4 7
4 5 7
5 6 7

output:

5 4
poly
4 2
.#
##
##
#.
perm
4 1 5 2 3
triang
1 2 4
1 4 5
1 5 7
2 3 4
5 6 7
perm
2 1 3 5 4

result:

ok good communication process (4 test cases)

Test #2:

score: 100
Accepted
time: 0ms
memory: 3628kb

input:

2 6
poly
2 1
#
#
poly
1 2
##
perm
2 1
perm
1 2
triang
1 2 3
1 3 4
triang
1 2 4
2 3 4

output:

2 6
triang
1 2 3
1 3 4
perm
1 2
triang
1 2 4
2 3 4
poly
1 2
##
poly
2 1
#
#
perm
2 1

input:

2 6
triang
1 2 3
1 3 4
perm
1 2
triang
1 2 4
2 3 4
poly
1 2
##
poly
2 1
#
#
perm
2 1

output:

2 6
poly
2 1
#
#
poly
1 2
##
perm
2 1
perm
1 2
triang
1 2 3
1 3 4
triang
1 2 4
2 3 4

result:

ok good communication process (6 test cases)

Test #3:

score: 0
Wrong Answer
time: 1ms
memory: 3580kb

input:

4 42
poly
3 2
.#
.#
##
poly
3 2
.#
##
#.
poly
3 2
##
#.
#.
poly
2 3
###
#..
poly
2 3
###
##.
poly
2 3
.##
###
poly
4 1
#
#
#
#
poly
3 2
##
##
#.
poly
1 4
####
poly
3 2
##
##
##
poly
3 2
.#
##
##
poly
2 3
.##
##.
poly
2 3
..#
###
poly
2 3
###
###
perm
1 4 2 3
perm
2 3 4 1
perm
2 4 1 3
perm
2 1 3 4
pe...

output:

4 42
perm
1 4 2 3
triang
1 2 6
2 3 5
2 5 6
3 4 5
perm
1 2 3 4
perm
1 3 2 4
perm
3 1 2 4
perm
2 3 4 1
triang
1 2 6
2 3 6
3 4 5
3 5 6
triang
1 2 3
1 3 6
3 4 6
4 5 6
triang
1 2 5
1 5 6
2 3 4
2 4 5
triang
1 2 3
1 3 4
1 4 5
1 5 6
triang
1 2 3
1 3 5
1 5 6
3 4 5
triang
1 2 4
1 4 6
2 3 4
4 5 6
perm
2 1 4 3
...

input:

4 42
perm
1 4 2 3
triang
1 2 6
2 3 5
2 5 6
3 4 5
perm
1 2 3 4
perm
1 3 2 4
perm
3 1 2 4
perm
2 3 4 1
triang
1 2 6
2 3 6
3 4 5
3 5 6
triang
1 2 3
1 3 6
3 4 6
4 5 6
triang
1 2 5
1 5 6
2 3 4
2 4 5
triang
1 2 3
1 3 4
1 4 5
1 5 6
triang
1 2 3
1 3 5
1 5 6
3 4 5
triang
1 2 4
1 4 6
2 3 4
4 5 6
perm
2 1 4 3
...

output:

4 42
poly
3 2
.#
.#
##
poly
3 2
.#
##
##
poly
3 2
##
##
##
poly
2 3
###
###
poly
2 3
###
###
poly
2 3
.##
###
poly
4 1
#
#
#
#
poly
3 2
##
##
#.
poly
1 4
####
poly
3 2
##
##
##
poly
3 2
.#
##
##
poly
2 3
.##
###
poly
2 3
..#
###
poly
2 3
###
###
perm
1 4 2 3
perm
2 3 4 1
perm
2 4 1 3
perm
2 1 3 4
pe...

result:

wrong answer wrong recovery (test case 2)